Projekt

Obecné

Profil

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