Projekt

Obecné

Profil

Stáhnout (2.88 KB) Statistiky
| Větev: | Tag: | Revize:
1
package jdeserialize;
2

    
3
import java.io.*;
4
import java.util.Map;
5

    
6
/**
7
 * Represents a serialized string object.  This is primarily used in serialized streams;
8
 * however, it is also used internally by other objects, since string objects have
9
 * handles as well.
10
 */
11
public class stringobj extends contentbase {
12
    public String value;
13
    private int readorthrow(ByteArrayInputStream bais) throws EOFException {
14
        int x = bais.read();
15
        if(x == -1) {
16
            throw new EOFException("unexpected eof in modified utf-8 string");
17
        }
18
        return x;
19
    }
20

    
21
    public @Override String toJson(String indetation, Map<classdesc, Map<field, Object>> fielddata, Map<String, String> formatting)
22
    {
23
        return "\"" + this.value + "\"";
24
    }
25

    
26
    public String toString() {
27
        return "[String " + jdeserialize.hex(handle) + ": \"" + value + "\"]";
28
    }
29
    /**
30
     * Constructor.
31
     *
32
     * @param handle the string object's handle
33
     * @param data the bytes corresponding to the string 
34
     * @throws IOException if an I/O or validity error occurs
35
     */
36
    public stringobj(int handle, byte[] data) throws IOException {
37
        super(contenttype.STRING);
38
        this.handle = handle;
39
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
40
        StringBuffer sb = new StringBuffer();
41
        while(true) {
42
            int ba = bais.read();
43
            if(ba == -1) {
44
                break;
45
            }
46
            if((ba & 0x80) == 0) {                  /* U+0001..U+007F */
47
                if(ba == 0) {
48
                    throw new IOException("improperly-encoded null in modified UTF8 string!");
49
                }
50
                sb.append((char)ba);
51
            } else if((ba & 0xf0) == 0xe0) {        /* U+0800..U+FFFF */
52
                int bb = readorthrow(bais);
53
                if((bb & 0xc0) != 0x80) {
54
                    throw new IOException("byte b in 0800-FFFF seq doesn't begin with correct prefix");
55
                }
56
                int bc = readorthrow(bais);
57
                if((bc & 0xc0) != 0x80) {
58
                    throw new IOException("byte c in 0800-FFFF seq doesn't begin with correct prefix");
59
                }
60
                int cp = 
61
                    ((ba & 0xf) << 12)
62
                    | ((bb & 0x3f) << 6)
63
                    | (bc & 0x3f);
64
                sb.append((char)cp);
65
            } else if((ba & 0xe0) == 0xc0) {        /* U+0080..U+07FF */
66
                int bb = readorthrow(bais);
67
                if((bb & 0xc0) != 0x80) {
68
                    throw new IOException("byte b in 0080-07FF seq doesn't begin with correct prefix");
69
                }
70
                int cp = ((ba & 0x1f) << 6) | (bb & 0x3f);
71
                sb.append((char)cp);
72
            } else {
73
                throw new IOException("invalid byte in modified utf-8 string: " + jdeserialize.hex(ba));
74
            }
75
        }
76
        this.value = sb.toString();
77
    }
78
}
(20-20/20)