This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 20796
Collapse All | Expand All

(-)src/org/netbeans/mdr/persistence/btreeimpl/btreeindex/StringInfo.java (-13 / +67 lines)
Lines 20-26 Link Here
20
 */
20
 */
21
21
22
public class StringInfo extends EntryTypeInfo {
22
public class StringInfo extends EntryTypeInfo {
23
24
    public String typeName() {
23
    public String typeName() {
25
24
26
        return new String("String");
25
        return new String("String");
Lines 31-55 Link Here
31
        if (!(object instanceof String)) {
30
        if (!(object instanceof String)) {
32
	    return null;
31
	    return null;
33
	} else {
32
	} else {
34
	    String s = (String) object;
33
            String s = (String)object;
35
	    return s.getBytes();
34
            int l = s.length();
35
            char[] arr = new char[l];
36
            int utflen = 0;
37
            char c;
38
            
39
            s.getChars(0, l, arr, 0);
40
            for (int i = 0; i < l; i++) {
41
                c = arr[i];
42
                if (c < 0x7f) {
43
                    utflen++;
44
                } else if (c > 0x7ff) {
45
                    utflen += 3;
46
                } else {
47
                    utflen += 2;
48
                }
49
            }
50
            byte[] encoded = new byte[utflen];
51
            int offset = 0;
52
            for (int i = 0; i < l; i++) {
53
                c = arr[i];
54
                if (c < 0x007f) {
55
                    encoded[offset++] = (byte)c;
56
                } else if (c > 0x07ff) {
57
                    encoded[offset++] = (byte)(0xe0 | ((c >> 12) & 0x0f));
58
                    encoded[offset++] = (byte)(0x80 | ((c >> 6) & 0x3f));
59
                    encoded[offset++] = (byte)(0x80 | (c & 0x3f));
60
                } else {
61
                    encoded[offset++] = (byte)(0xc0 | ((c >> 6) & 0x1f));
62
                    encoded[offset++] = (byte)(0x80 | (c & 0x3f));
63
                }
64
            }
65
            return encoded;
36
	}
66
	}
37
    }
67
    }
68
    
69
    static int fromBufferCount = 0;
38
70
39
    public Object fromBuffer(byte[] buffer) {
71
    public Object fromBuffer(byte[] buffer) {
40
        return new String(buffer);
72
        int length = buffer.length;
73
        int offset = 0;
74
        StringBuffer sb = new StringBuffer(length);
75
        do {
76
            int b = buffer[offset++] & 0xff;
77
            if (b >= 0xe0) {
78
                b = (b & 0x0f) << 12;
79
                b |= (buffer[offset++] & 0x3f) << 6;
80
                b |= buffer[offset++] & 0x3f;
81
            } else if (b >= 0xc0) {
82
                b = (b & 0x1f) << 6;
83
                b |= buffer[offset++] & 0x3f;
84
            }
85
            sb.append((char)b);
86
        } while (--length > 0);
87
        return new String(sb);
88
    }
89
    
90
    final int compareImpl(byte[] key1buffer, byte[] key2buffer, int offset, int length) {
91
        int key1len = key1buffer.length;
92
        byte c1, c2;
93
        int key1offset = 0;
94
95
        int n = Math.min(key1len, length);
96
        while (n-- > 0) {
97
            c1 = key1buffer[key1offset++];
98
            c2 = key2buffer[offset++];
99
            if (c1 != c2)
100
                return c1 - c2;
101
        }
102
        return key1len - length;
41
    }
103
    }
42
104
43
    public byte compare(byte[] key1buffer, byte[] key2buffer, int offset, int length) {
105
    public byte compare(byte[] key1buffer, byte[] key2buffer, int offset, int length) {
44
106
	int result = compareImpl(key1buffer, key2buffer, offset, length);
45
        String key1, key2;
46
	int result;
47
48
	key1 = new String(key1buffer);
49
	key2 = new String(key2buffer, offset, length);
50
51
	result = key1.compareTo(key2);
52
53
	if (result < 0) {
107
	if (result < 0) {
54
	    return LESS;
108
	    return LESS;
55
	} else if (result > 0) {
109
	} else if (result > 0) {
(-)src/org/netbeans/mdr/util/IOUtils.java (-48 / +96 lines)
Lines 59-93 Link Here
59
        write(outputStream, object, null);
59
        write(outputStream, object, null);
60
    }
60
    }
61
    
61
    
62
    public static void writeInt(OutputStream outputStream, int val) throws IOException {
63
        if ((int) ((byte) val & 0xFF) == val) {
64
            outputStream.write(T_BYTE);
65
            outputStream.write((byte)val);
66
        } else if ((int) ((byte) (val >> 8) & 0xFF) == val >> 8) {
67
            outputStream.write(T_SHORT);
68
            outputStream.write((byte)(val & 0xff));
69
            outputStream.write((byte)((val & 0xff00) >> 8));
70
        } else {
71
            outputStream.write(T_INTEGER);
72
            outputStream.write((byte)(val & 0xff));
73
            outputStream.write((byte)((val & 0xff00) >> 8));
74
            outputStream.write((byte)((val & 0xff0000) >> 16));
75
            val = (val >> 24) & 0xff;
76
            outputStream.write((byte)val);
77
        }
78
    }
79
    
62
    public static void write(OutputStream outputStream, Object object, MdrStorage storage) throws IOException {
80
    public static void write(OutputStream outputStream, Object object, MdrStorage storage) throws IOException {
63
        if (object == null) {
81
        if (object == null) {
64
            outputStream.write(T_NULL);
82
            outputStream.write(T_NULL);
65
83
66
        } else if (object instanceof String) {
84
        } else if (object instanceof String) {
85
            String val = (String)object;
67
            outputStream.write(T_STRING);
86
            outputStream.write(T_STRING);
68
            byte val[] = ((String) object).getBytes("UTF-8");
87
            int len = val.length();
69
            write(outputStream, new Integer(val.length));
88
            writeInt(outputStream, len);
70
            outputStream.write(val);
89
            for (int i = 0; i < len; i++) {
71
90
                char ch = val.charAt(i);
72
        } else if (object instanceof Integer) {
91
                if (ch <= 0x7f) {
73
            int val = ((Integer) object).intValue();
92
                    outputStream.write((byte)ch);
74
            int size;
93
                } else if (ch <= 0x7ff) {
75
            if ((int) ((byte) val & 0xFF) == val) {
94
                    outputStream.write((byte)(0xc0 | (ch >> 6)));
76
                outputStream.write(T_BYTE);
95
                    outputStream.write((byte)(0x80 | (ch & 0x3f)));
77
                size = 1;
96
                } else {
78
            } else if ((int) ((byte) (val >> 8) & 0xFF) == val >> 8) {
97
                    outputStream.write((byte)(0xe0 | (ch >> 12)));
79
                outputStream.write(T_SHORT);
98
                    outputStream.write((byte)(0x80 | ((ch >> 6) & 0x3f)));
80
                size = 2;
99
                    outputStream.write((byte)(0x80 | (ch & 0x3f)));
81
            } else {
100
                }
82
                outputStream.write(T_INTEGER);
83
                size = 4;
84
            }
85
            byte res[] = new byte[size];
86
            for (int i = 0; i < size; i++) {
87
                res[i] = (byte) ((val >> (i * 8)) & 0xFF);
88
            }
101
            }
89
            outputStream.write(res);
102
        } else if (object instanceof Integer) {
90
103
            int val = ((Integer)object).intValue();
104
            writeInt(outputStream, val);
91
        } else if (object instanceof Boolean) {
105
        } else if (object instanceof Boolean) {
92
            outputStream.write(T_BOOLEAN);
106
            outputStream.write(T_BOOLEAN);
93
            outputStream.write(((Boolean) object).booleanValue() ? 1 : 0);
107
            outputStream.write(((Boolean) object).booleanValue() ? 1 : 0);
Lines 174-211 Link Here
174
        return read(inputStream, storable, null);
188
        return read(inputStream, storable, null);
175
    }
189
    }
176
    
190
    
191
    public static int readInt(InputStream is) throws IOException {
192
        return readInt(is, is.read());
193
    }
194
    
195
    public static int readInt(InputStream inputStream, int type) throws IOException {
196
        switch (type) {
197
            case T_BYTE: {
198
                int ch = inputStream.read();
199
                if (ch < 0)
200
                    throw new IOException("EOF");
201
                return ch;
202
            }
203
            case T_SHORT: {
204
                int ch1, ch2;
205
                ch1 = inputStream.read();
206
                ch2 = inputStream.read();
207
                if ((ch1 | ch2) < 0) {
208
                    throw new IOException("EOF");
209
                }
210
                return (ch2 << 8) | ch1;
211
            }
212
            case T_INTEGER: {
213
                int ch1, ch2, ch3, ch4;
214
                ch1 = inputStream.read();
215
                ch2 = inputStream.read();
216
                ch3 = inputStream.read();
217
                ch4 = inputStream.read();
218
                if ((ch1 | ch2 | ch3 | ch4) < 0) {
219
                    throw new IOException("EOF");
220
                }
221
                return (ch4 << 24) + (ch3 << 16) + (ch2 << 8) + ch1;
222
            }
223
            default:
224
                throw new IOException("Unknown int format: " + type);
225
        }
226
    }
227
    
177
    public static Object read(InputStream inputStream, StorableBaseObject storable, String className) throws IOException {
228
    public static Object read(InputStream inputStream, StorableBaseObject storable, String className) throws IOException {
178
        int type;
229
        int type;
179
        switch (type = inputStream.read()) {
230
        switch (type = inputStream.read()) {
180
            case T_NULL: {
231
            case T_NULL: {
181
                return null;
232
                return null;
182
            } case T_STRING: {
233
            } case T_STRING: {
183
                int length = ((Integer) read(inputStream, storable)).intValue();
234
                int length = readInt(inputStream);
184
                byte val[] = new byte[length];
235
                if (length == 0)
185
                inputStream.read(val);
236
                    return "";
186
                return new String(val, "UTF-8");
237
                StringBuffer sb = new StringBuffer(length);
187
            } case T_INTEGER: 
238
                do {
188
              case T_BYTE:
239
                    int b = inputStream.read() & 0xff;
189
              case T_SHORT: {
240
                    if (b >= 0xe0) {
190
                int size;
241
                        b = (b & 0x0f) << 12;
191
                if (type == T_BYTE) {
242
                        b |= (inputStream.read() & 0x3f) << 6;
192
                    size = 1;
243
                        b |= inputStream.read() & 0x3f;
193
                } else if (type == T_SHORT) {
244
                    } else if (b >= 0xc0) {
194
                    size = 2;
245
                        b = (b & 0x1f) << 6;
195
                } else {
246
                        b |= inputStream.read() & 0x3f;
196
                    size = 4;
247
                    }
197
                }
248
                    sb.append((char)b);
198
                byte val[] = new byte[size];
249
                } while (--length > 0);
199
                inputStream.read(val);
250
                return new String(sb);
200
                int res = 0;
251
            }
201
//                System.out.println("length:");
252
            case T_BYTE: 
202
                for (int i = 0; i < size; i++) {
253
            case T_SHORT:
203
//                    System.out.println("" + ((256 + val[i]) % 256));
254
            case T_INTEGER:
204
                    res |= ((256 + val[i]) % 256) << (i * 8);
255
                return new Integer(readInt(inputStream, type));
205
                }
256
            case T_BOOLEAN: {
206
//                System.out.println("" + res);
207
                return new Integer(res);
208
            } case T_BOOLEAN: {
209
                return new Boolean(inputStream.read() == 1);
257
                return new Boolean(inputStream.read() == 1);
210
            } case T_MAP: {
258
            } case T_MAP: {
211
                //if (storable == null) Thread.dumpStack();
259
                //if (storable == null) Thread.dumpStack();

Return to bug 20796