KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > btreeimpl > btreeindex > StringInfo


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.persistence.btreeimpl.btreeindex;
20
21 /**
22  * EntryTypeInfo implementation for type String
23  *
24  * @author Dana Bergen
25  * @version 1.0
26  */

27
28 public class StringInfo extends EntryTypeInfo {
29     public String JavaDoc typeName() {
30         return "String";
31     }
32
33     public byte[] toBuffer(Object JavaDoc object) {
34
35         if (!(object instanceof String JavaDoc)) {
36         return null;
37     } else {
38             String JavaDoc s = (String JavaDoc)object;
39             int l = s.length();
40             char[] arr = new char[l];
41             int utflen = 0;
42             char c;
43             
44             s.getChars(0, l, arr, 0);
45             for (int i = 0; i < l; i++) {
46                 c = arr[i];
47                 if (c < 0x7f) {
48                     utflen++;
49                 } else if (c > 0x7ff) {
50                     utflen += 3;
51                 } else {
52                     utflen += 2;
53                 }
54             }
55             byte[] encoded = new byte[utflen];
56             int offset = 0;
57             for (int i = 0; i < l; i++) {
58                 c = arr[i];
59                 if (c < 0x007f) {
60                     encoded[offset++] = (byte)c;
61                 } else if (c > 0x07ff) {
62                     encoded[offset++] = (byte)(0xe0 | ((c >> 12) & 0x0f));
63                     encoded[offset++] = (byte)(0x80 | ((c >> 6) & 0x3f));
64                     encoded[offset++] = (byte)(0x80 | (c & 0x3f));
65                 } else {
66                     encoded[offset++] = (byte)(0xc0 | ((c >> 6) & 0x1f));
67                     encoded[offset++] = (byte)(0x80 | (c & 0x3f));
68                 }
69             }
70             return encoded;
71     }
72     }
73     
74     static int fromBufferCount = 0;
75
76     public Object JavaDoc fromBuffer(byte[] buffer) {
77         int length = buffer.length;
78         int offset = 0;
79         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(length);
80         if (length > 0) {
81             do {
82                 int b = buffer[offset++] & 0xff;
83                 length--;
84                 if (b >= 0xe0) {
85                     b = (b & 0x0f) << 12;
86                     b |= (buffer[offset++] & 0x3f) << 6;
87                     b |= buffer[offset++] & 0x3f;
88                     length--;
89                     length--;
90                 } else if (b >= 0xc0) {
91                     b = (b & 0x1f) << 6;
92                     b |= buffer[offset++] & 0x3f;
93                     length--;
94                 }
95                 sb.append((char)b);
96             } while (length > 0);
97         }
98         return new String JavaDoc(sb);
99     }
100     
101     final int compareImpl(byte[] key1buffer, byte[] key2buffer, int offset, int length) {
102         int key1len = key1buffer.length;
103         byte c1, c2;
104         int key1offset = 0;
105
106         int n = Math.min(key1len, length);
107         while (n-- > 0) {
108             c1 = key1buffer[key1offset++];
109             c2 = key2buffer[offset++];
110             if (c1 != c2)
111                 return c1 - c2;
112         }
113         return key1len - length;
114     }
115
116     public byte compare(byte[] key1buffer, byte[] key2buffer, int offset, int length) {
117     int result = compareImpl(key1buffer, key2buffer, offset, length);
118     if (result < 0) {
119         return LESS;
120     } else if (result > 0) {
121         return GREATER;
122     } else {
123         return EQUAL;
124     }
125     }
126
127     public int getLength() {
128         // length is variable
129
return 0;
130     }
131
132     public boolean isFixedLength() {
133         return false;
134     }
135 }
136
Popular Tags