Projekt

Obecné

Profil

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

    
5
/**
6
 * Represents a serialized string object.  This is primarily used in serialized streams;
7
 * however, it is also used internally by other objects, since string objects have
8
 * handles as well.
9
 */
10
public class stringobj extends contentbase {
11
    public String value;
12
    private int readorthrow(ByteArrayInputStream bais) throws EOFException {
13
        int x = bais.read();
14
        if(x == -1) {
15
            throw new EOFException("unexpected eof in modified utf-8 string");
16
        }
17
        return x;
18
    }
19
    public String toString() {
20
        return "[String " + jdeserialize.hex(handle) + ": \"" + value + "\"]";
21
    }
22
    /**
23
     * Constructor.
24
     *
25
     * @param handle the string object's handle
26
     * @param data the bytes corresponding to the string 
27
     * @throws IOException if an I/O or validity error occurs
28
     */
29
    public stringobj(int handle, byte[] data) throws IOException {
30
        super(contenttype.STRING);
31
        this.handle = handle;
32
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
33
        StringBuffer sb = new StringBuffer();
34
        while(true) {
35
            int ba = bais.read();
36
            if(ba == -1) {
37
                break;
38
            }
39
            if((ba & 0x80) == 0) {                  /* U+0001..U+007F */
40
                if(ba == 0) {
41
                    throw new IOException("improperly-encoded null in modified UTF8 string!");
42
                }
43
                sb.append((char)ba);
44
            } else if((ba & 0xf0) == 0xe0) {        /* U+0800..U+FFFF */
45
                int bb = readorthrow(bais);
46
                if((bb & 0xc0) != 0x80) {
47
                    throw new IOException("byte b in 0800-FFFF seq doesn't begin with correct prefix");
48
                }
49
                int bc = readorthrow(bais);
50
                if((bc & 0xc0) != 0x80) {
51
                    throw new IOException("byte c in 0800-FFFF seq doesn't begin with correct prefix");
52
                }
53
                int cp = 
54
                    ((ba & 0xf) << 12)
55
                    | ((bb & 0x3f) << 6)
56
                    | (bc & 0x3f);
57
                sb.append((char)cp);
58
            } else if((ba & 0xe0) == 0xc0) {        /* U+0080..U+07FF */
59
                int bb = readorthrow(bais);
60
                if((bb & 0xc0) != 0x80) {
61
                    throw new IOException("byte b in 0080-07FF seq doesn't begin with correct prefix");
62
                }
63
                int cp = ((ba & 0x1f) << 6) | (bb & 0x3f);
64
                sb.append((char)cp);
65
            } else {
66
                throw new IOException("invalid byte in modified utf-8 string: " + jdeserialize.hex(ba));
67
            }
68
        }
69
        this.value = sb.toString();
70
    }
71
}
(20-20/20)