|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| package org.apache.xindice.core.data; |
|
21 |
| |
|
22 |
| import org.apache.xindice.util.XindiceRuntimeException; |
|
23 |
| |
|
24 |
| import java.io.ByteArrayInputStream; |
|
25 |
| import java.io.IOException; |
|
26 |
| import java.io.InputStream; |
|
27 |
| import java.io.OutputStream; |
|
28 |
| import java.io.UnsupportedEncodingException; |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| public class Value implements Comparable { |
|
38 |
| |
|
39 |
| private final byte[] data; |
|
40 |
| private final int pos; |
|
41 |
| private final int len; |
|
42 |
| private int hash; |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
42987
| public Value(Value value) {
|
|
53 |
42987
| this.data = value.data;
|
|
54 |
42987
| this.pos = value.pos;
|
|
55 |
42987
| this.len = value.len;
|
|
56 |
| } |
|
57 |
| |
|
58 |
92167
| public Value(byte[] data) {
|
|
59 |
92167
| this.data = data;
|
|
60 |
92167
| this.pos = 0;
|
|
61 |
92167
| this.len = data.length;
|
|
62 |
| } |
|
63 |
| |
|
64 |
80811
| public Value(byte[] data, int pos, int len) {
|
|
65 |
80811
| if (pos >= data.length || pos < 0 || pos + len > data.length) {
|
|
66 |
0
| throw new ArrayIndexOutOfBoundsException("Value cannot be created");
|
|
67 |
| } |
|
68 |
| |
|
69 |
80811
| this.data = data;
|
|
70 |
80811
| this.pos = pos;
|
|
71 |
80811
| this.len = len;
|
|
72 |
| } |
|
73 |
| |
|
74 |
60372
| public Value(String data) {
|
|
75 |
60368
| try {
|
|
76 |
60376
| this.data = data.getBytes("utf-8");
|
|
77 |
60363
| this.pos = 0;
|
|
78 |
60387
| this.len = this.data.length;
|
|
79 |
| } catch (UnsupportedEncodingException e) { |
|
80 |
0
| throw new XindiceRuntimeException("Java doesn't support UTF-8 encoding", e);
|
|
81 |
| } |
|
82 |
| } |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
1426658
| public final int getLength() {
|
|
90 |
1426653
| return len;
|
|
91 |
| } |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
36206
| public final byte[] getData() {
|
|
103 |
36206
| byte[] b = new byte[len];
|
|
104 |
36206
| System.arraycopy(data, pos, b, 0, len);
|
|
105 |
36206
| return b;
|
|
106 |
| } |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
103371
| public final byte byteAt(int index) {
|
|
121 |
103371
| if (index < 0 || index >= len) {
|
|
122 |
0
| throw new ArrayIndexOutOfBoundsException(index);
|
|
123 |
| } |
|
124 |
103371
| return data[pos + index];
|
|
125 |
| } |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
17768
| public final short shortAt(int index) {
|
|
136 |
17768
| return (short) ((data[index += pos] << 8) | data[index + 1]);
|
|
137 |
| } |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
342
| public final int intAt(int index) {
|
|
148 |
342
| return (short) ((data[index += pos] << 24) | (data[index + 1] << 16) | (data[index + 2] << 8) | data[index + 3]);
|
|
149 |
| } |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
68904
| public final Value valueAt(int start, int len) {
|
|
161 |
68904
| return new Value(data, pos + start, len);
|
|
162 |
| } |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
8884
| public final Key keyAt(int start, int len) {
|
|
174 |
8884
| return new Key(data, pos + start, len);
|
|
175 |
| } |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
38967
| public final InputStream getInputStream() {
|
|
187 |
38967
| return new ByteArrayInputStream(data, pos, len);
|
|
188 |
| } |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
1217915
| public final void streamTo(OutputStream out) throws IOException {
|
|
197 |
1217914
| out.write(data, pos, len);
|
|
198 |
| } |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
3118
| public final void copyTo(byte[] tdata, int tpos) {
|
|
207 |
3118
| System.arraycopy(data, pos, tdata, tpos, len);
|
|
208 |
| } |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
4095
| public final void copyTo(byte[] tdata, int tpos, int len) {
|
|
219 |
4095
| System.arraycopy(data, pos, tdata, tpos, len);
|
|
220 |
| } |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
69
| public final boolean startsWith(Value value) {
|
|
227 |
69
| if (len < value.len) {
|
|
228 |
0
| return false;
|
|
229 |
| } |
|
230 |
| |
|
231 |
69
| byte[] ddata = value.data;
|
|
232 |
69
| int dpos = value.pos;
|
|
233 |
| |
|
234 |
69
| for (int i = 0; i < value.len; i++) {
|
|
235 |
119
| if (data[i + pos] != ddata[i + dpos]) {
|
|
236 |
5
| return false;
|
|
237 |
| } |
|
238 |
| } |
|
239 |
| |
|
240 |
64
| return true;
|
|
241 |
| } |
|
242 |
| |
|
243 |
838047
| public final int compareTo(Value value) {
|
|
244 |
838047
| byte[] ddata = value.data;
|
|
245 |
838047
| int dpos = value.pos;
|
|
246 |
838047
| int dlen = value.len;
|
|
247 |
| |
|
248 |
838047
| int stop = len > dlen ? dlen : len;
|
|
249 |
| |
|
250 |
838047
| for (int i = 0; i < stop; i++) {
|
|
251 |
4606006
| byte b1 = data[pos + i];
|
|
252 |
4606007
| byte b2 = ddata[dpos + i];
|
|
253 |
| |
|
254 |
4606007
| if (b1 != b2) {
|
|
255 |
| |
|
256 |
663003
| int s1 = ((int) b1) & 0xFF;
|
|
257 |
663003
| int s2 = ((int) b2) & 0xFF;
|
|
258 |
663003
| return s1 > s2 ? (i + 1) : -(i + 1);
|
|
259 |
| } |
|
260 |
| } |
|
261 |
| |
|
262 |
175044
| if (len == dlen) {
|
|
263 |
143111
| return 0;
|
|
264 |
| } else { |
|
265 |
31933
| return len > dlen ? stop + 1 : -(stop + 1);
|
|
266 |
| } |
|
267 |
| } |
|
268 |
| |
|
269 |
739883
| public final int compareTo(Object obj) {
|
|
270 |
739883
| if (obj instanceof Value) {
|
|
271 |
739883
| return compareTo((Value) obj);
|
|
272 |
| } |
|
273 |
| |
|
274 |
0
| return compareTo(new Value(obj.toString()));
|
|
275 |
| } |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
275097
| public int hashCode() {
|
|
282 |
| |
|
283 |
275097
| if (hash == 0) {
|
|
284 |
77389
| int tempHash = 0;
|
|
285 |
77389
| for (int i = 0; i < len; i++) {
|
|
286 |
569863
| tempHash = 31 * tempHash + data[pos + i];
|
|
287 |
| } |
|
288 |
77389
| this.hash = Math.abs(tempHash);
|
|
289 |
| } |
|
290 |
275096
| return hash;
|
|
291 |
| } |
|
292 |
| |
|
293 |
7839
| public boolean equals(Value value) {
|
|
294 |
7839
| return len == value.len && compareTo(value) == 0;
|
|
295 |
| } |
|
296 |
| |
|
297 |
75982
| public boolean equals(Object obj) {
|
|
298 |
75982
| if (this == obj) {
|
|
299 |
5864
| return true;
|
|
300 |
| } |
|
301 |
| |
|
302 |
70118
| if (obj instanceof Value) {
|
|
303 |
70116
| return equals((Value) obj);
|
|
304 |
| } |
|
305 |
| |
|
306 |
2
| return obj != null && equals(new Value(obj.toString()));
|
|
307 |
| } |
|
308 |
| |
|
309 |
192331
| public final String toString() {
|
|
310 |
192331
| try {
|
|
311 |
192331
| return new String(data, pos, len, "utf-8");
|
|
312 |
| } catch (UnsupportedEncodingException e) { |
|
313 |
0
| throw new XindiceRuntimeException("Java doesn't seem to support UTF-8!", e);
|
|
314 |
| } |
|
315 |
| } |
|
316 |
| } |