Projekt

Obecné

Profil

Stáhnout (2.78 KB) Statistiky
| Větev: | Tag: | Revize:
1
package jdeserialize;
2
import java.io.*;
3

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

    
19
    public @Override String toJson(String indetation)
20
    {
21
        return "\"" + this.value + "\"";
22
    }
23

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