|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| package org.apache.xindice.util; |
|
21 |
| |
|
22 |
| import java.io.ByteArrayInputStream; |
|
23 |
| import java.io.IOException; |
|
24 |
| import java.io.InputStream; |
|
25 |
| import java.io.ObjectInputStream; |
|
26 |
| import java.io.ObjectOutputStream; |
|
27 |
| import java.io.OutputStream; |
|
28 |
| import java.io.Serializable; |
|
29 |
| import java.io.UnsupportedEncodingException; |
|
30 |
| |
|
31 |
| import org.apache.commons.logging.Log; |
|
32 |
| import org.apache.commons.logging.LogFactory; |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| public final class ByteBuffer extends OutputStream implements Serializable { |
|
49 |
| |
|
50 |
| private static final Log log = LogFactory.getLog(ByteBuffer.class); |
|
51 |
| |
|
52 |
| static final long serialVersionUID = -3900903004630456844L; |
|
53 |
| |
|
54 |
| private transient int length = 0; |
|
55 |
| |
|
56 |
| private byte[] buffer = null; |
|
57 |
| private boolean locked = false; |
|
58 |
| |
|
59 |
0
| public ByteBuffer(int size) {
|
|
60 |
0
| buffer = new byte[size];
|
|
61 |
| } |
|
62 |
| |
|
63 |
0
| public ByteBuffer(String value) {
|
|
64 |
| |
|
65 |
0
| try {
|
|
66 |
| |
|
67 |
0
| buffer = value.getBytes("utf-8");
|
|
68 |
0
| length = buffer.length;
|
|
69 |
| } catch (UnsupportedEncodingException e) { |
|
70 |
| |
|
71 |
0
| throw new IllegalStateException("Java doesn't seem to support UTF-8");
|
|
72 |
| } |
|
73 |
| } |
|
74 |
| |
|
75 |
0
| public ByteBuffer(StringBuffer value) {
|
|
76 |
0
| this(value.toString());
|
|
77 |
| } |
|
78 |
| |
|
79 |
0
| public ByteBuffer(byte[] value, boolean copy) {
|
|
80 |
0
| length = value.length;
|
|
81 |
0
| if (copy) {
|
|
82 |
0
| buffer = new byte[length];
|
|
83 |
0
| System.arraycopy(value, 0, buffer, 0, length);
|
|
84 |
| } else { |
|
85 |
0
| buffer = value;
|
|
86 |
| } |
|
87 |
| } |
|
88 |
| |
|
89 |
0
| public ByteBuffer(byte[] value) {
|
|
90 |
0
| length = value.length;
|
|
91 |
0
| buffer = new byte[length];
|
|
92 |
0
| System.arraycopy(value, 0, buffer, 0, length);
|
|
93 |
| } |
|
94 |
| |
|
95 |
0
| public ByteBuffer() {
|
|
96 |
0
| this(64);
|
|
97 |
| } |
|
98 |
| |
|
99 |
0
| public int capacity() {
|
|
100 |
0
| return buffer.length;
|
|
101 |
| } |
|
102 |
| |
|
103 |
0
| public void ensureCapacity(int size) {
|
|
104 |
0
| if (locked) {
|
|
105 |
0
| return;
|
|
106 |
| } |
|
107 |
0
| checkCapacity(size);
|
|
108 |
| } |
|
109 |
| |
|
110 |
0
| private void checkCapacity(int size) {
|
|
111 |
0
| if (size > buffer.length) {
|
|
112 |
0
| int newsize = (buffer.length + 1) * 2;
|
|
113 |
0
| if (size > newsize) {
|
|
114 |
0
| newsize = size;
|
|
115 |
| } |
|
116 |
0
| byte[] old = buffer;
|
|
117 |
0
| buffer = new byte[newsize];
|
|
118 |
0
| System.arraycopy(old, 0, buffer, 0, old.length);
|
|
119 |
| } |
|
120 |
| } |
|
121 |
| |
|
122 |
0
| public void pack() {
|
|
123 |
0
| if (locked) {
|
|
124 |
0
| return;
|
|
125 |
| } |
|
126 |
0
| if (buffer.length > length) {
|
|
127 |
0
| byte[] value = new byte[length];
|
|
128 |
0
| System.arraycopy(buffer, 0, value, 0, length);
|
|
129 |
0
| buffer = value;
|
|
130 |
| } |
|
131 |
| } |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
0
| public void append(byte[] value) {
|
|
136 |
0
| if (locked) {
|
|
137 |
0
| return;
|
|
138 |
| } |
|
139 |
0
| checkCapacity(length + value.length);
|
|
140 |
0
| System.arraycopy(value, 0, buffer, length, value.length);
|
|
141 |
0
| length += value.length;
|
|
142 |
| } |
|
143 |
| |
|
144 |
0
| public void append(char[] value) {
|
|
145 |
| |
|
146 |
0
| append(new String(value));
|
|
147 |
| } |
|
148 |
| |
|
149 |
0
| public void append(String value) {
|
|
150 |
| |
|
151 |
0
| try {
|
|
152 |
| |
|
153 |
0
| append(value.getBytes("utf-8"));
|
|
154 |
| } catch (UnsupportedEncodingException e) { |
|
155 |
| |
|
156 |
0
| throw new IllegalStateException("Java doesn't seem to know UTF-8...");
|
|
157 |
| } |
|
158 |
| } |
|
159 |
| |
|
160 |
0
| public void append(StringBuffer value) {
|
|
161 |
0
| append(value.toString());
|
|
162 |
| } |
|
163 |
| |
|
164 |
0
| public void append(ByteBuffer value) {
|
|
165 |
0
| try {
|
|
166 |
0
| value.writeTo(this);
|
|
167 |
| } catch (Exception e) { |
|
168 |
0
| if (log.isWarnEnabled()) {
|
|
169 |
0
| log.warn("ignored exception", e);
|
|
170 |
| } |
|
171 |
| } |
|
172 |
| } |
|
173 |
| |
|
174 |
0
| public void append(boolean value) {
|
|
175 |
0
| append(String.valueOf(value));
|
|
176 |
| } |
|
177 |
| |
|
178 |
0
| public void append(char value) {
|
|
179 |
0
| if (locked) {
|
|
180 |
0
| return;
|
|
181 |
| } |
|
182 |
0
| append(new char[]{value});
|
|
183 |
| } |
|
184 |
| |
|
185 |
0
| public void append(byte value) {
|
|
186 |
0
| if (locked) {
|
|
187 |
0
| return;
|
|
188 |
| } |
|
189 |
0
| checkCapacity(length + 1);
|
|
190 |
0
| buffer[length++] = value;
|
|
191 |
| } |
|
192 |
| |
|
193 |
0
| public void append(double value) {
|
|
194 |
0
| append(String.valueOf(value));
|
|
195 |
| } |
|
196 |
| |
|
197 |
0
| public void append(float value) {
|
|
198 |
0
| append(String.valueOf(value));
|
|
199 |
| } |
|
200 |
| |
|
201 |
0
| public void append(int value) {
|
|
202 |
0
| append(String.valueOf(value));
|
|
203 |
| } |
|
204 |
| |
|
205 |
0
| public void append(long value) {
|
|
206 |
0
| append(String.valueOf(value));
|
|
207 |
| } |
|
208 |
| |
|
209 |
0
| public void append(Object value) {
|
|
210 |
0
| append(String.valueOf(value));
|
|
211 |
| } |
|
212 |
| |
|
213 |
0
| public void insert(byte[] value, int pos) {
|
|
214 |
0
| if (locked) {
|
|
215 |
0
| return;
|
|
216 |
| } |
|
217 |
0
| if (pos < 0 || pos >= length) {
|
|
218 |
0
| throw new IndexOutOfBoundsException();
|
|
219 |
| } |
|
220 |
0
| checkCapacity(length + value.length);
|
|
221 |
0
| byte[] temp = new byte[length - pos];
|
|
222 |
0
| System.arraycopy(buffer, pos, temp, 0, temp.length);
|
|
223 |
0
| System.arraycopy(temp, 0, buffer, pos + value.length, temp.length);
|
|
224 |
0
| System.arraycopy(value, 0, buffer, pos, value.length);
|
|
225 |
0
| length += value.length;
|
|
226 |
| } |
|
227 |
| |
|
228 |
0
| public void insert(char[] value, int pos) {
|
|
229 |
0
| if (locked) {
|
|
230 |
0
| return;
|
|
231 |
| } |
|
232 |
| |
|
233 |
0
| insert(new String(value), pos);
|
|
234 |
| } |
|
235 |
| |
|
236 |
0
| public void insert(String value, int pos) {
|
|
237 |
0
| try {
|
|
238 |
| |
|
239 |
0
| insert(value.getBytes("utf-8"), pos);
|
|
240 |
| } catch (UnsupportedEncodingException e) { |
|
241 |
| |
|
242 |
0
| throw new IllegalStateException("Java doesn't seem to support UTF-8");
|
|
243 |
| } |
|
244 |
| } |
|
245 |
| |
|
246 |
0
| public void insert(StringBuffer value, int pos) {
|
|
247 |
| |
|
248 |
0
| insert(value.toString(), pos);
|
|
249 |
| } |
|
250 |
| |
|
251 |
0
| public void insert(ByteBuffer value, int pos) {
|
|
252 |
0
| insert(value.toByteArray(), pos);
|
|
253 |
| } |
|
254 |
| |
|
255 |
0
| public void insert(boolean value, int pos) {
|
|
256 |
0
| insert(String.valueOf(value), pos);
|
|
257 |
| } |
|
258 |
| |
|
259 |
0
| public void insert(char value, int pos) {
|
|
260 |
| |
|
261 |
0
| insert(new char[]{value}, pos);
|
|
262 |
| } |
|
263 |
| |
|
264 |
0
| public void insert(byte value, int pos) {
|
|
265 |
0
| if (locked) {
|
|
266 |
0
| return;
|
|
267 |
| } |
|
268 |
0
| if (pos < 0 || pos >= length) {
|
|
269 |
0
| throw new IndexOutOfBoundsException();
|
|
270 |
| } |
|
271 |
0
| checkCapacity(length + 1);
|
|
272 |
0
| byte[] temp = new byte[length - pos];
|
|
273 |
0
| System.arraycopy(buffer, pos, temp, 0, temp.length);
|
|
274 |
0
| System.arraycopy(temp, 0, buffer, pos + 1, temp.length);
|
|
275 |
0
| buffer[pos] = value;
|
|
276 |
0
| length++;
|
|
277 |
| } |
|
278 |
| |
|
279 |
0
| public void insert(double value, int pos) {
|
|
280 |
0
| insert(String.valueOf(value), pos);
|
|
281 |
| } |
|
282 |
| |
|
283 |
0
| public void insert(float value, int pos) {
|
|
284 |
0
| insert(String.valueOf(value), pos);
|
|
285 |
| } |
|
286 |
| |
|
287 |
0
| public void insert(int value, int pos) {
|
|
288 |
0
| insert(String.valueOf(value), pos);
|
|
289 |
| } |
|
290 |
| |
|
291 |
0
| public void insert(long value, int pos) {
|
|
292 |
0
| insert(String.valueOf(value), pos);
|
|
293 |
| } |
|
294 |
| |
|
295 |
0
| public void insert(Object value, int pos) {
|
|
296 |
0
| insert(String.valueOf(value), pos);
|
|
297 |
| } |
|
298 |
| |
|
299 |
0
| public void delete(int pos, int width) {
|
|
300 |
0
| if (locked) {
|
|
301 |
0
| return;
|
|
302 |
| } |
|
303 |
0
| if (pos + width > length) {
|
|
304 |
0
| width = (length - pos);
|
|
305 |
| } |
|
306 |
0
| byte[] temp = new byte[length - (pos + width)];
|
|
307 |
0
| System.arraycopy(buffer, pos + width, temp, 0, temp.length);
|
|
308 |
0
| System.arraycopy(temp, 0, buffer, pos, temp.length);
|
|
309 |
0
| length -= width;
|
|
310 |
| } |
|
311 |
| |
|
312 |
0
| public void replace(byte[] value, int pos, int width) {
|
|
313 |
0
| delete(pos, width);
|
|
314 |
0
| insert(value, pos);
|
|
315 |
| } |
|
316 |
| |
|
317 |
0
| public void replace(String value, int pos, int width) {
|
|
318 |
0
| try {
|
|
319 |
| |
|
320 |
0
| replace(value.getBytes("utf-8"), pos, width);
|
|
321 |
| } catch (UnsupportedEncodingException e) { |
|
322 |
| |
|
323 |
0
| throw new IllegalStateException("Java doesn't seem to know UTF-8");
|
|
324 |
| } |
|
325 |
| } |
|
326 |
| |
|
327 |
0
| public void replace(StringBuffer value, int pos, int width) {
|
|
328 |
| |
|
329 |
0
| replace(value.toString(), pos, width);
|
|
330 |
| } |
|
331 |
| |
|
332 |
0
| public void replace(ByteBuffer value, int pos, int width) {
|
|
333 |
| |
|
334 |
0
| replace(value.toByteArray(), pos, width);
|
|
335 |
| } |
|
336 |
| |
|
337 |
0
| public void replace(boolean value, int pos, int width) {
|
|
338 |
| |
|
339 |
0
| replace(String.valueOf(value), pos, width);
|
|
340 |
| } |
|
341 |
| |
|
342 |
0
| public void replace(char value, int pos, int width) {
|
|
343 |
| |
|
344 |
0
| replace(String.valueOf(value), pos, width);
|
|
345 |
| } |
|
346 |
| |
|
347 |
0
| public void replace(double value, int pos, int width) {
|
|
348 |
| |
|
349 |
0
| replace(String.valueOf(value), pos, width);
|
|
350 |
| } |
|
351 |
| |
|
352 |
0
| public void replace(float value, int pos, int width) {
|
|
353 |
| |
|
354 |
0
| replace(String.valueOf(value), pos, width);
|
|
355 |
| } |
|
356 |
| |
|
357 |
0
| public void replace(int value, int pos, int width) {
|
|
358 |
| |
|
359 |
0
| replace(String.valueOf(value), pos, width);
|
|
360 |
| } |
|
361 |
| |
|
362 |
0
| public void replace(long value, int pos, int width) {
|
|
363 |
| |
|
364 |
0
| replace(String.valueOf(value), pos, width);
|
|
365 |
| } |
|
366 |
| |
|
367 |
0
| public void replace(Object value, int pos, int width) {
|
|
368 |
| |
|
369 |
0
| replace(String.valueOf(value), pos, width);
|
|
370 |
| } |
|
371 |
| |
|
372 |
0
| public byte[] toByteArray() {
|
|
373 |
| |
|
374 |
0
| byte[] value = new byte[length];
|
|
375 |
0
| System.arraycopy(buffer, 0, value, 0, length);
|
|
376 |
0
| return value;
|
|
377 |
| } |
|
378 |
| |
|
379 |
0
| public String toString() {
|
|
380 |
| |
|
381 |
0
| try {
|
|
382 |
| |
|
383 |
0
| return new String(buffer, "utf-8");
|
|
384 |
| } catch (UnsupportedEncodingException e) { |
|
385 |
| |
|
386 |
0
| throw new IllegalStateException("Java doesn't seem to support UTF-8");
|
|
387 |
| } |
|
388 |
| } |
|
389 |
| |
|
390 |
0
| public int length() {
|
|
391 |
0
| return length;
|
|
392 |
| } |
|
393 |
| |
|
394 |
0
| public void setLength(int value) {
|
|
395 |
0
| if (locked) {
|
|
396 |
0
| return;
|
|
397 |
| } |
|
398 |
0
| if (value < 0 || value > length) {
|
|
399 |
0
| throw new IndexOutOfBoundsException();
|
|
400 |
| } |
|
401 |
0
| length = value;
|
|
402 |
| } |
|
403 |
| |
|
404 |
| |
|
405 |
| |
|
406 |
0
| public int size() {
|
|
407 |
0
| return length;
|
|
408 |
| } |
|
409 |
| |
|
410 |
0
| public void reset() {
|
|
411 |
0
| if (locked) {
|
|
412 |
0
| return;
|
|
413 |
| } |
|
414 |
0
| length = 0;
|
|
415 |
| } |
|
416 |
| |
|
417 |
0
| public byte byteAt(int index) {
|
|
418 |
0
| if (index < 0 || index >= length) {
|
|
419 |
0
| throw new IndexOutOfBoundsException();
|
|
420 |
| } |
|
421 |
0
| return buffer[index];
|
|
422 |
| } |
|
423 |
| |
|
424 |
0
| public void setByteAt(int index, byte value) {
|
|
425 |
0
| if (locked) {
|
|
426 |
0
| return;
|
|
427 |
| } |
|
428 |
0
| if (index < 0 || index >= length) {
|
|
429 |
0
| throw new IndexOutOfBoundsException();
|
|
430 |
| } |
|
431 |
0
| buffer[index] = value;
|
|
432 |
| } |
|
433 |
| |
|
434 |
0
| public void reverse() {
|
|
435 |
0
| if (locked) {
|
|
436 |
0
| return;
|
|
437 |
| } |
|
438 |
0
| byte b = 0;
|
|
439 |
0
| for (int i = 0; i < length / 2; i++) {
|
|
440 |
0
| b = buffer[i];
|
|
441 |
0
| buffer[i] = buffer[(length - 1) - i];
|
|
442 |
0
| buffer[(length - 1) - i] = b;
|
|
443 |
| } |
|
444 |
| } |
|
445 |
| |
|
446 |
0
| public InputStream getInputStream() {
|
|
447 |
0
| return new ByteArrayInputStream(buffer, 0, length);
|
|
448 |
| } |
|
449 |
| |
|
450 |
0
| public void writeTo(OutputStream out) throws IOException {
|
|
451 |
0
| out.write(buffer, 0, length);
|
|
452 |
| } |
|
453 |
| |
|
454 |
0
| public void chunkTo(OutputStream out, int chunkSize) throws IOException {
|
|
455 |
0
| int pos = 0;
|
|
456 |
0
| int size = 0;
|
|
457 |
0
| while (pos < length) {
|
|
458 |
0
| size = length - pos > chunkSize ? chunkSize
|
|
459 |
| : length - pos; |
|
460 |
0
| out.write(buffer, pos, size);
|
|
461 |
0
| pos += size;
|
|
462 |
0
| Thread.yield();
|
|
463 |
| } |
|
464 |
| } |
|
465 |
| |
|
466 |
0
| public void chunkTo(OutputStream out) throws IOException {
|
|
467 |
0
| chunkTo(out, 2048);
|
|
468 |
| } |
|
469 |
| |
|
470 |
0
| public void readFrom(InputStream in) throws IOException {
|
|
471 |
0
| if (locked) {
|
|
472 |
0
| throw new IOException("ByteBuffer Is Locked");
|
|
473 |
| } |
|
474 |
0
| int size = in.available();
|
|
475 |
0
| checkCapacity(length + size);
|
|
476 |
0
| in.read(buffer, length, size);
|
|
477 |
0
| length += size;
|
|
478 |
| } |
|
479 |
| |
|
480 |
0
| public void write(int b) throws IOException {
|
|
481 |
0
| if (locked) {
|
|
482 |
0
| throw new IOException("ByteBuffer Is Locked");
|
|
483 |
| } |
|
484 |
0
| checkCapacity(length + 1);
|
|
485 |
0
| buffer[length++] = (byte) b;
|
|
486 |
| } |
|
487 |
| |
|
488 |
0
| public void write(byte b[]) throws IOException {
|
|
489 |
0
| if (locked) {
|
|
490 |
0
| throw new IOException("ByteBuffer Is Locked");
|
|
491 |
| } |
|
492 |
0
| checkCapacity(length + b.length);
|
|
493 |
0
| System.arraycopy(b, 0, buffer, length, b.length);
|
|
494 |
0
| length += b.length;
|
|
495 |
| } |
|
496 |
| |
|
497 |
0
| public void write(byte b[], int off, int len) throws IOException {
|
|
498 |
0
| if (locked) {
|
|
499 |
0
| throw new IOException("ByteBuffer Is Locked");
|
|
500 |
| } |
|
501 |
0
| checkCapacity(length + len);
|
|
502 |
0
| System.arraycopy(b, off, buffer, length, len);
|
|
503 |
0
| length += len;
|
|
504 |
| } |
|
505 |
| |
|
506 |
| |
|
507 |
| |
|
508 |
0
| private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
|
|
509 |
| |
|
510 |
0
| in.defaultReadObject();
|
|
511 |
0
| length = buffer.length;
|
|
512 |
| } |
|
513 |
| |
|
514 |
0
| private void writeObject(ObjectOutputStream out) throws IOException {
|
|
515 |
| |
|
516 |
0
| pack();
|
|
517 |
0
| out.defaultWriteObject();
|
|
518 |
| } |
|
519 |
| |
|
520 |
| |
|
521 |
| |
|
522 |
0
| public void lock() {
|
|
523 |
0
| if (!locked) {
|
|
524 |
0
| pack();
|
|
525 |
0
| locked = true;
|
|
526 |
| } |
|
527 |
| } |
|
528 |
| |
|
529 |
0
| public boolean isLocked() {
|
|
530 |
0
| return locked;
|
|
531 |
| } |
|
532 |
| } |
|
533 |
| |