Debian Codegraph @

src/main/java/io/airlift/slice/Slice.java

raw
1 /*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14 package io.airlift.slice;
15
16 import sun.misc.Unsafe;
17
18 import javax.annotation.Nullable;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.nio.ByteBuffer;
24 import java.nio.charset.Charset;
25
26 import static io.airlift.slice.JvmUtils.newByteBuffer;
27 import static io.airlift.slice.JvmUtils.unsafe;
28 import static io.airlift.slice.Preconditions.checkArgument;
29 import static io.airlift.slice.Preconditions.checkNotNull;
30 import static io.airlift.slice.Preconditions.checkPositionIndexes;
31 import static io.airlift.slice.SizeOf.SIZE_OF_BYTE;
32 import static io.airlift.slice.SizeOf.SIZE_OF_DOUBLE;
33 import static io.airlift.slice.SizeOf.SIZE_OF_FLOAT;
34 import static io.airlift.slice.SizeOf.SIZE_OF_INT;
35 import static io.airlift.slice.SizeOf.SIZE_OF_LONG;
36 import static io.airlift.slice.SizeOf.SIZE_OF_SHORT;
37 import static io.airlift.slice.StringDecoder.decodeString;
38 import static java.lang.Math.min;
39 import static java.lang.String.format;
40 import static java.nio.charset.StandardCharsets.UTF_8;
41 import static sun.misc.Unsafe.ARRAY_BOOLEAN_BASE_OFFSET;
42 import static sun.misc.Unsafe.ARRAY_BOOLEAN_INDEX_SCALE;
43 import static sun.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET;
44 import static sun.misc.Unsafe.ARRAY_DOUBLE_BASE_OFFSET;
45 import static sun.misc.Unsafe.ARRAY_DOUBLE_INDEX_SCALE;
46 import static sun.misc.Unsafe.ARRAY_FLOAT_BASE_OFFSET;
47 import static sun.misc.Unsafe.ARRAY_FLOAT_INDEX_SCALE;
48 import static sun.misc.Unsafe.ARRAY_INT_BASE_OFFSET;
49 import static sun.misc.Unsafe.ARRAY_INT_INDEX_SCALE;
50 import static sun.misc.Unsafe.ARRAY_LONG_BASE_OFFSET;
51 import static sun.misc.Unsafe.ARRAY_LONG_INDEX_SCALE;
52 import static sun.misc.Unsafe.ARRAY_SHORT_BASE_OFFSET;
53 import static sun.misc.Unsafe.ARRAY_SHORT_INDEX_SCALE;
54
55 public final class Slice
56 implements Comparable<Slice>
57 {
58 /**
59 * @deprecated use {@link Slices#wrappedBuffer(java.nio.ByteBuffer)}
60 */
61 @Deprecated
62 public static Slice toUnsafeSlice(ByteBuffer byteBuffer)
63 {
64 return Slices.wrappedBuffer(byteBuffer);
65 }
66
67 /**
68 * Base object for relative addresses. If null, the address is an
69 * absolute location in memory.
70 */
71 private final Object base;
72
73 /**
74 * If base is null, address is the absolute memory location of data for
75 * this slice; otherwise, address is the offset from the base object.
76 * This base plus relative offset addressing is taken directly from
77 * the Unsafe interface.
78 * <p>
79 * Note: if base object is a byte array, this address ARRAY_BYTE_BASE_OFFSET,
80 * since the byte array data starts AFTER the byte array object header.
81 */
82 private final long address;
83
84 /**
85 * Size of the slice
86 */
87 private final int size;
88
89 /**
90 * Bytes retained by the slice
91 */
92 private final int retainedSize;
93
94 /**
95 * Reference is typically a ByteBuffer object, but can be any object this
96 * slice must hold onto to assure that the underlying memory is not
97 * freed by the garbage collector.
98 */
99 private final Object reference;
100
101 private int hash;
102
103 /**
104 * Creates an empty slice.
105 */
106 Slice()
107 {
108 this.base = null;
109 this.address = 0;
110 this.size = 0;
111 this.retainedSize = 0;
112 this.reference = null;
113 }
114
115 /**
116 * Creates a slice over the specified array.
117 */
118 Slice(byte[] base)
119 {
120 checkNotNull(base, "base is null");
121 this.base = base;
122 this.address = ARRAY_BYTE_BASE_OFFSET;
123 this.size = base.length;
124 this.retainedSize = base.length;
125 this.reference = null;
126 }
127
128 /**
129 * Creates a slice over the specified array range.
130 */
131 Slice(byte[] base, int offset, int length)
132 {
133 checkNotNull(base, "base is null");
134 checkPositionIndexes(offset, offset + length, base.length);
135
136 this.base = base;
137 this.address = ARRAY_BYTE_BASE_OFFSET + offset;
138 this.size = length;
139 this.retainedSize = base.length;
140 this.reference = null;
141 }
142
143 /**
144 * Creates a slice over the specified array range.
145 */
146 Slice(boolean[] base, int offset, int length)
147 {
148 checkNotNull(base, "base is null");
149 checkPositionIndexes(offset, offset + length, base.length);
150
151 this.base = base;
152 this.address = ARRAY_BOOLEAN_BASE_OFFSET + offset;
153 this.size = length * ARRAY_BOOLEAN_INDEX_SCALE;
154 this.retainedSize = base.length * ARRAY_BOOLEAN_INDEX_SCALE;
155 this.reference = null;
156 }
157
158 /**
159 * Creates a slice over the specified array range.
160 */
161 Slice(short[] base, int offset, int length)
162 {
163 checkNotNull(base, "base is null");
164 checkPositionIndexes(offset, offset + length, base.length);
165
166 this.base = base;
167 this.address = ARRAY_SHORT_BASE_OFFSET + offset;
168 this.size = length * ARRAY_SHORT_INDEX_SCALE;
169 this.retainedSize = base.length * ARRAY_SHORT_INDEX_SCALE;
170 this.reference = null;
171 }
172
173 /**
174 * Creates a slice over the specified array range.
175 */
176 Slice(int[] base, int offset, int length)
177 {
178 checkNotNull(base, "base is null");
179 checkPositionIndexes(offset, offset + length, base.length);
180
181 this.base = base;
182 this.address = ARRAY_INT_BASE_OFFSET + offset;
183 this.size = length * ARRAY_INT_INDEX_SCALE;
184 this.retainedSize = base.length * ARRAY_INT_INDEX_SCALE;
185 this.reference = null;
186 }
187
188 /**
189 * Creates a slice over the specified array range.
190 */
191 Slice(long[] base, int offset, int length)
192 {
193 checkNotNull(base, "base is null");
194 checkPositionIndexes(offset, offset + length, base.length);
195
196 this.base = base;
197 this.address = ARRAY_LONG_BASE_OFFSET + offset;
198 this.size = length * ARRAY_LONG_INDEX_SCALE;
199 this.retainedSize = base.length * ARRAY_LONG_INDEX_SCALE;
200 this.reference = null;
201 }
202
203 /**
204 * Creates a slice over the specified array range.
205 */
206 Slice(float[] base, int offset, int length)
207 {
208 checkNotNull(base, "base is null");
209 checkPositionIndexes(offset, offset + length, base.length);
210
211 this.base = base;
212 this.address = ARRAY_FLOAT_BASE_OFFSET + offset;
213 this.size = length * ARRAY_FLOAT_INDEX_SCALE;
214 this.retainedSize = base.length * ARRAY_FLOAT_INDEX_SCALE;
215 this.reference = null;
216 }
217
218 /**
219 * Creates a slice over the specified array range.
220 */
221 Slice(double[] base, int offset, int length)
222 {
223 checkNotNull(base, "base is null");
224 checkPositionIndexes(offset, offset + length, base.length);
225
226 this.base = base;
227 this.address = ARRAY_DOUBLE_BASE_OFFSET + offset;
228 this.size = length * ARRAY_DOUBLE_INDEX_SCALE;
229 this.retainedSize = base.length * ARRAY_DOUBLE_INDEX_SCALE;
230 this.reference = null;
231 }
232
233 /**
234 * Creates a slice for directly accessing the base object.
235 */
236 Slice(@Nullable Object base, long address, int size, int retainedSize, @Nullable Object reference)
237 {
238 if (address <= 0) {
239 throw new IllegalArgumentException(format("Invalid address: %s", address));
240 }
241 if (size <= 0) {
242 throw new IllegalArgumentException(format("Invalid size: %s", size));
243 }
244 checkArgument((address + size) >= size, "Address + size is greater than 64 bits");
245
246 this.reference = reference;
247 this.base = base;
248 this.address = address;
249 this.size = size;
250 this.retainedSize = retainedSize;
251 }
252
253 /**
254 * Returns the base object of this Slice, or null. This is appropriate for use
255 * with {@link Unsafe} if you wish to avoid all the safety belts e.g. bounds checks.
256 */
257 public Object getBase()
258 {
259 return base;
260 }
261
262 /**
263 * Return the address offset of this Slice. This is appropriate for use
264 * with {@link Unsafe} if you wish to avoid all the safety belts e.g. bounds checks.
265 */
266 public long getAddress()
267 {
268 return address;
269 }
270
271 /**
272 * Length of this slice.
273 */
274 public int length()
275 {
276 return size;
277 }
278
279 /**
280 * Approximate number of bytes retained by this slice.
281 */
282 public int getRetainedSize()
283 {
284 return retainedSize;
285 }
286
287 /**
288 * Fill the slice with the specified value;
289 */
290 public void fill(byte value)
291 {
292 int offset = 0;
293 int length = size;
294 long longValue = fillLong(value);
295 while (length >= SIZE_OF_LONG) {
296 unsafe.putLong(base, address + offset, longValue);
297 offset += SIZE_OF_LONG;
298 length -= SIZE_OF_LONG;
299 }
300
301 while (length > 0) {
302 unsafe.putByte(base, address + offset, value);
303 offset++;
304 length--;
305 }
306 }
307
308 /**
309 * Fill the slice with zeros;
310 */
311 public void clear()
312 {
313 clear(0, size);
314 }
315
316 public void clear(int offset, int length)
317 {
318 while (length >= SIZE_OF_LONG) {
319 unsafe.putLong(base, address + offset, 0);
320 offset += SIZE_OF_LONG;
321 length -= SIZE_OF_LONG;
322 }
323
324 while (length > 0) {
325 unsafe.putByte(base, address + offset, (byte) 0);
326 offset++;
327 length--;
328 }
329 }
330
331 /**
332 * Gets a byte at the specified absolute {@code index} in this buffer.
333 *
334 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
335 * {@code index + 1} is greater than {@code this.length()}
336 */
337 public byte getByte(int index)
338 {
339 checkIndexLength(index, SIZE_OF_BYTE);
340 return getByteUnchecked(index);
341 }
342
343 byte getByteUnchecked(int index)
344 {
345 return unsafe.getByte(base, address + index);
346 }
347
348 /**
349 * Gets an unsigned byte at the specified absolute {@code index} in this
350 * buffer.
351 *
352 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
353 * {@code index + 1} is greater than {@code this.length()}
354 */
355 public short getUnsignedByte(int index)
356 {
357 return (short) (getByte(index) & 0xFF);
358 }
359
360 /**
361 * Gets a 16-bit short integer at the specified absolute {@code index} in
362 * this slice.
363 *
364 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
365 * {@code index + 2} is greater than {@code this.length()}
366 */
367 public short getShort(int index)
368 {
369 checkIndexLength(index, SIZE_OF_SHORT);
370 return getShortUnchecked(index);
371 }
372
373 short getShortUnchecked(int index)
374 {
375 return unsafe.getShort(base, address + index);
376 }
377
378 /**
379 * Gets a 32-bit integer at the specified absolute {@code index} in
380 * this buffer.
381 *
382 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
383 * {@code index + 4} is greater than {@code this.length()}
384 */
385 public int getInt(int index)
386 {
387 checkIndexLength(index, SIZE_OF_INT);
388 return getIntUnchecked(index);
389 }
390
391 public int getIntUnchecked(int index)
392 {
393 return unsafe.getInt(base, address + index);
394 }
395
396 /**
397 * Gets a 64-bit long integer at the specified absolute {@code index} in
398 * this buffer.
399 *
400 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
401 * {@code index + 8} is greater than {@code this.length()}
402 */
403 public long getLong(int index)
404 {
405 checkIndexLength(index, SIZE_OF_LONG);
406 return getLongUnchecked(index);
407 }
408
409 long getLongUnchecked(int index)
410 {
411 return unsafe.getLong(base, address + index);
412 }
413
414 /**
415 * Gets a 32-bit float at the specified absolute {@code index} in
416 * this buffer.
417 *
418 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
419 * {@code index + 4} is greater than {@code this.length()}
420 */
421 public float getFloat(int index)
422 {
423 checkIndexLength(index, SIZE_OF_FLOAT);
424 return unsafe.getFloat(base, address + index);
425 }
426
427 /**
428 * Gets a 64-bit double at the specified absolute {@code index} in
429 * this buffer.
430 *
431 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
432 * {@code index + 8} is greater than {@code this.length()}
433 */
434 public double getDouble(int index)
435 {
436 checkIndexLength(index, SIZE_OF_DOUBLE);
437 return unsafe.getDouble(base, address + index);
438 }
439
440 /**
441 * Transfers portion of data from this slice into the specified destination starting at
442 * the specified absolute {@code index}.
443 *
444 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0}, or
445 * if {@code index + destination.length()} is greater than {@code this.length()}
446 */
447 public void getBytes(int index, Slice destination)
448 {
449 getBytes(index, destination, 0, destination.length());
450 }
451
452 /**
453 * Transfers portion of data from this slice into the specified destination starting at
454 * the specified absolute {@code index}.
455 *
456 * @param destinationIndex the first index of the destination
457 * @param length the number of bytes to transfer
458 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0},
459 * if the specified {@code destinationIndex} is less than {@code 0},
460 * if {@code index + length} is greater than
461 * {@code this.length()}, or
462 * if {@code destinationIndex + length} is greater than
463 * {@code destination.length()}
464 */
465 public void getBytes(int index, Slice destination, int destinationIndex, int length)
466 {
467 destination.setBytes(destinationIndex, this, index, length);
468 }
469
470 /**
471 * Transfers portion of data from this slice into the specified destination starting at
472 * the specified absolute {@code index}.
473 *
474 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0}, or
475 * if {@code index + destination.length} is greater than {@code this.length()}
476 */
477 public void getBytes(int index, byte[] destination)
478 {
479 getBytes(index, destination, 0, destination.length);
480 }
481
482 /**
483 * Transfers portion of data from this slice into the specified destination starting at
484 * the specified absolute {@code index}.
485 *
486 * @param destinationIndex the first index of the destination
487 * @param length the number of bytes to transfer
488 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0},
489 * if the specified {@code destinationIndex} is less than {@code 0},
490 * if {@code index + length} is greater than
491 * {@code this.length()}, or
492 * if {@code destinationIndex + length} is greater than
493 * {@code destination.length}
494 */
495 public void getBytes(int index, byte[] destination, int destinationIndex, int length)
496 {
497 checkIndexLength(index, length);
498 checkPositionIndexes(destinationIndex, destinationIndex + length, destination.length);
499
500 copyMemory(base, address + index, destination, (long) ARRAY_BYTE_BASE_OFFSET + destinationIndex, length);
501 }
502
503 /**
504 * Returns a copy of this buffer as a byte array.
505 */
506 public byte[] getBytes()
507 {
508 return getBytes(0, length());
509 }
510
511 /**
512 * Returns a copy of this buffer as a byte array.
513 *
514 * @param index the absolute index to start at
515 * @param length the number of bytes to return
516 * @throws IndexOutOfBoundsException if the specified {@code index} is less then {@code 0},
517 * or if the specified {@code index + length} is greater than {@code this.length()}
518 */
519 public byte[] getBytes(int index, int length)
520 {
521 byte[] bytes = new byte[length];
522 getBytes(index, bytes, 0, length);
523 return bytes;
524 }
525
526 /**
527 * Transfers a portion of data from this slice into the specified stream starting at the
528 * specified absolute {@code index}.
529 *
530 * @param length the number of bytes to transfer
531 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
532 * if {@code index + length} is greater than
533 * {@code this.length()}
534 * @throws java.io.IOException if the specified stream threw an exception during I/O
535 */
536 public void getBytes(int index, OutputStream out, int length)
537 throws IOException
538 {
539 checkIndexLength(index, length);
540
541 if (base instanceof byte[]) {
542 out.write((byte[]) base, (int) ((address - ARRAY_BYTE_BASE_OFFSET) + index), length);
543 return;
544 }
545
546 byte[] buffer = new byte[4096];
547 while (length > 0) {
548 int size = min(buffer.length, length);
549 getBytes(index, buffer, 0, size);
550 out.write(buffer, 0, size);
551 length -= size;
552 index += size;
553 }
554 }
555
556 /**
557 * Sets the specified byte at the specified absolute {@code index} in this
558 * buffer. The 24 high-order bits of the specified value are ignored.
559 *
560 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
561 * {@code index + 1} is greater than {@code this.length()}
562 */
563 public void setByte(int index, int value)
564 {
565 checkIndexLength(index, SIZE_OF_BYTE);
566 setByteUnchecked(index, value);
567 }
568
569 void setByteUnchecked(int index, int value)
570 {
571 unsafe.putByte(base, address + index, (byte) (value & 0xFF));
572 }
573
574 /**
575 * Sets the specified 16-bit short integer at the specified absolute
576 * {@code index} in this buffer. The 16 high-order bits of the specified
577 * value are ignored.
578 *
579 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
580 * {@code index + 2} is greater than {@code this.length()}
581 */
582 public void setShort(int index, int value)
583 {
584 checkIndexLength(index, SIZE_OF_SHORT);
585 setShortUnchecked(index, value);
586 }
587
588 void setShortUnchecked(int index, int value)
589 {
590 unsafe.putShort(base, address + index, (short) (value & 0xFFFF));
591 }
592
593 /**
594 * Sets the specified 32-bit integer at the specified absolute
595 * {@code index} in this buffer.
596 *
597 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
598 * {@code index + 4} is greater than {@code this.length()}
599 */
600 public void setInt(int index, int value)
601 {
602 checkIndexLength(index, SIZE_OF_INT);
603 setIntUnchecked(index, value);
604 }
605
606 void setIntUnchecked(int index, int value)
607 {
608 unsafe.putInt(base, address + index, value);
609 }
610
611 /**
612 * Sets the specified 64-bit long integer at the specified absolute
613 * {@code index} in this buffer.
614 *
615 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
616 * {@code index + 8} is greater than {@code this.length()}
617 */
618 public void setLong(int index, long value)
619 {
620 checkIndexLength(index, SIZE_OF_LONG);
621 unsafe.putLong(base, address + index, value);
622 }
623
624 /**
625 * Sets the specified 32-bit float at the specified absolute
626 * {@code index} in this buffer.
627 *
628 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
629 * {@code index + 4} is greater than {@code this.length()}
630 */
631 public void setFloat(int index, float value)
632 {
633 checkIndexLength(index, SIZE_OF_FLOAT);
634 unsafe.putFloat(base, address + index, value);
635 }
636
637 /**
638 * Sets the specified 64-bit double at the specified absolute
639 * {@code index} in this buffer.
640 *
641 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
642 * {@code index + 8} is greater than {@code this.length()}
643 */
644 public void setDouble(int index, double value)
645 {
646 checkIndexLength(index, SIZE_OF_DOUBLE);
647 unsafe.putDouble(base, address + index, value);
648 }
649
650 /**
651 * Transfers data from the specified slice into this buffer starting at
652 * the specified absolute {@code index}.
653 *
654 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0}, or
655 * if {@code index + source.length()} is greater than {@code this.length()}
656 */
657 public void setBytes(int index, Slice source)
658 {
659 setBytes(index, source, 0, source.length());
660 }
661
662 /**
663 * Transfers data from the specified slice into this buffer starting at
664 * the specified absolute {@code index}.
665 *
666 * @param sourceIndex the first index of the source
667 * @param length the number of bytes to transfer
668 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0},
669 * if the specified {@code sourceIndex} is less than {@code 0},
670 * if {@code index + length} is greater than
671 * {@code this.length()}, or
672 * if {@code sourceIndex + length} is greater than
673 * {@code source.length()}
674 */
675 public void setBytes(int index, Slice source, int sourceIndex, int length)
676 {
677 checkIndexLength(index, length);
678 checkPositionIndexes(sourceIndex, sourceIndex + length, source.length());
679
680 copyMemory(source.base, source.address + sourceIndex, base, address + index, length);
681 }
682
683 /**
684 * Transfers data from the specified slice into this buffer starting at
685 * the specified absolute {@code index}.
686 *
687 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0}, or
688 * if {@code index + source.length} is greater than {@code this.length()}
689 */
690 public void setBytes(int index, byte[] source)
691 {
692 setBytes(index, source, 0, source.length);
693 }
694
695 /**
696 * Transfers data from the specified array into this buffer starting at
697 * the specified absolute {@code index}.
698 *
699 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0},
700 * if the specified {@code sourceIndex} is less than {@code 0},
701 * if {@code index + length} is greater than
702 * {@code this.length()}, or
703 * if {@code sourceIndex + length} is greater than {@code source.length}
704 */
705 public void setBytes(int index, byte[] source, int sourceIndex, int length)
706 {
707 checkPositionIndexes(sourceIndex, sourceIndex + length, source.length);
708 copyMemory(source, (long) ARRAY_BYTE_BASE_OFFSET + sourceIndex, base, address + index, length);
709 }
710
711 /**
712 * Transfers data from the specified input stream into this slice starting at
713 * the specified absolute {@code index}.
714 *
715 * @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0}, or
716 * if {@code index + source.length} is greater than {@code this.length()}
717 */
718 public void setBytes(int index, InputStream in, int length)
719 throws IOException
720 {
721 checkIndexLength(index, length);
722 byte[] bytes = new byte[4096];
723
724 while (length > 0) {
725 int bytesRead = in.read(bytes, 0, min(bytes.length, length));
726 if (bytesRead < 0) {
727 throw new IndexOutOfBoundsException("End of stream");
728 }
729 copyMemory(bytes, ARRAY_BYTE_BASE_OFFSET, base, address + index, bytesRead);
730 length -= bytesRead;
731 index += bytesRead;
732 }
733 }
734
735 /**
736 * Returns a slice of this buffer's sub-region. Modifying the content of
737 * the returned buffer or this buffer affects each other's content.
738 */
739 public Slice slice(int index, int length)
740 {
741 if ((index == 0) && (length == length())) {
742 return this;
743 }
744 checkIndexLength(index, length);
745 if (length == 0) {
746 return Slices.EMPTY_SLICE;
747 }
748 return new Slice(base, address + index, length, retainedSize, reference);
749 }
750
751 public int indexOfByte(int b)
752 {
753 b = b & 0xFF;
754 for (int i = 0; i < size; i++) {
755 if (getByteUnchecked(i) == b) {
756 return i;
757 }
758 }
759 return -1;
760 }
761
762 /**
763 * Returns the index of the first occurrence of the pattern with this slice.
764 * If the pattern is not found -1 is returned. If patten is empty, zero is
765 * returned.
766 */
767 public int indexOf(Slice slice)
768 {
769 return indexOf(slice, 0);
770 }
771
772 /**
773 * Returns the index of the first occurrence of the pattern with this slice.
774 * If the pattern is not found -1 is returned If patten is empty, the offset
775 * is returned.
776 */
777 public int indexOf(Slice pattern, int offset)
778 {
779 if (size == 0 || offset >= size) {
780 return -1;
781 }
782
783 if (pattern.length() == 0) {
784 return offset;
785 }
786
787 // Do we have enough characters
788 if (pattern.length() < SIZE_OF_INT || size < SIZE_OF_LONG) {
789 return indexOfBruteForce(pattern, offset);
790 }
791
792 // Using first four bytes for faster search. We are not using eight bytes for long
793 // because we want more strings to get use of fast search.
794 int head = pattern.getIntUnchecked(0);
795
796 // Take the first byte of head for faster skipping
797 int firstByteMask = head & 0xff;
798 firstByteMask |= firstByteMask << 8;
799 firstByteMask |= firstByteMask << 16;
800
801 int lastValidIndex = size - pattern.length();
802 int index = offset;
803 while (index <= lastValidIndex) {
804 // Read four bytes in sequence
805 int value = getIntUnchecked(index);
806
807 // Compare all bytes of value with first byte of search data
808 // see https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
809 int valueXor = value ^ firstByteMask;
810 int hasZeroBytes = (valueXor - 0x01010101) & ~valueXor & 0x80808080;
811
812 // If valueXor doesn't not have any zero byte then there is no match and we can advance
813 if (hasZeroBytes == 0) {
814 index += SIZE_OF_INT;
815 continue;
816 }
817
818 // Try fast match of head and the rest
819 if (value == head && equalsUnchecked(index, pattern, 0, pattern.length())) {
820 return index;
821 }
822
823 index++;
824 }
825
826 return -1;
827 }
828
829 int indexOfBruteForce(Slice pattern, int offset)
830 {
831 if (size == 0 || offset >= size) {
832 return -1;
833 }
834
835 if (pattern.length() == 0) {
836 return offset;
837 }
838
839 byte firstByte = pattern.getByteUnchecked(0);
840 int lastValidIndex = size - pattern.length();
841 int index = offset;
842 while (true) {
843 // seek to first byte match
844 while (index < lastValidIndex && getByteUnchecked(index) != firstByte) {
845 index++;
846 }
847 if (index > lastValidIndex) {
848 break;
849 }
850
851 if (equalsUnchecked(index, pattern, 0, pattern.length())) {
852 return index;
853 }
854
855 index++;
856 }
857
858 return -1;
859 }
860
861 /**
862 * Compares the content of the specified buffer to the content of this
863 * buffer. This comparison is performed byte by byte using an unsigned
864 * comparison.
865 */
866 @SuppressWarnings("ObjectEquality")
867 @Override
868 public int compareTo(Slice that)
869 {
870 if (this == that) {
871 return 0;
872 }
873 return compareTo(0, size, that, 0, that.size);
874 }
875
876 /**
877 * Compares a portion of this slice with a portion of the specified slice. Equality is
878 * solely based on the contents of the slice.
879 */
880 @SuppressWarnings("ObjectEquality")
881 public int compareTo(int offset, int length, Slice that, int otherOffset, int otherLength)
882 {
883 if ((this == that) && (offset == otherOffset) && (length == otherLength)) {
884 return 0;
885 }
886
887 checkIndexLength(offset, length);
888 that.checkIndexLength(otherOffset, otherLength);
889
890 int compareLength = min(length, otherLength);
891 while (compareLength >= SIZE_OF_LONG) {
892 long thisLong = getLongUnchecked(offset);
893 thisLong = Long.reverseBytes(thisLong);
894 long thatLong = that.getLongUnchecked(otherOffset);
895 thatLong = Long.reverseBytes(thatLong);
896
897 int v = compareUnsignedLongs(thisLong, thatLong);
898 if (v != 0) {
899 return v;
900 }
901
902 offset += SIZE_OF_LONG;
903 otherOffset += SIZE_OF_LONG;
904 compareLength -= SIZE_OF_LONG;
905 }
906
907 while (compareLength > 0) {
908 byte thisByte = getByteUnchecked(offset);
909 byte thatByte = that.getByteUnchecked(otherOffset);
910
911 int v = compareUnsignedBytes(thisByte, thatByte);
912 if (v != 0) {
913 return v;
914 }
915 offset++;
916 otherOffset++;
917 compareLength--;
918 }
919
920 return Integer.compare(length, otherLength);
921 }
922
923 /**
924 * Compares the specified object with this slice for equality. Equality is
925 * solely based on the contents of the slice.
926 */
927 @Override
928 public boolean equals(Object o)
929 {
930 if (this == o) {
931 return true;
932 }
933 if (!(o instanceof Slice)) {
934 return false;
935 }
936
937 Slice that = (Slice) o;
938 if (length() != that.length()) {
939 return false;
940 }
941
942 int offset = 0;
943 int length = size;
944 while (length >= SIZE_OF_LONG) {
945 long thisLong = getLongUnchecked(offset);
946 long thatLong = that.getLongUnchecked(offset);
947
948 if (thisLong != thatLong) {
949 return false;
950 }
951
952 offset += SIZE_OF_LONG;
953 length -= SIZE_OF_LONG;
954 }
955
956 while (length > 0) {
957 byte thisByte = getByteUnchecked(offset);
958 byte thatByte = that.getByteUnchecked(offset);
959 if (thisByte != thatByte) {
960 return false;
961 }
962 offset++;
963 length--;
964 }
965
966 return true;
967 }
968
969 /**
970 * Returns the hash code of this slice. The hash code is cached once calculated
971 * and any future changes to the slice will not effect the hash code.
972 */
973 @SuppressWarnings("NonFinalFieldReferencedInHashCode")
974 @Override
975 public int hashCode()
976 {
977 if (hash != 0) {
978 return hash;
979 }
980
981 hash = hashCode(0, size);
982 return hash;
983 }
984
985 /**
986 * Returns the hash code of a portion of this slice.
987 */
988 public int hashCode(int offset, int length)
989 {
990 return (int) XxHash64.hash(this, offset, length);
991 }
992
993 /**
994 * Compares a portion of this slice with a portion of the specified slice. Equality is
995 * solely based on the contents of the slice.
996 */
997 @SuppressWarnings("ObjectEquality")
998 public boolean equals(int offset, int length, Slice that, int otherOffset, int otherLength)
999 {
1000 if (length != otherLength) {
1001 return false;
1002 }
1003
1004 checkIndexLength(offset, length);
1005 that.checkIndexLength(otherOffset, otherLength);
1006
1007 return equalsUnchecked(offset, that, otherOffset, length);
1008 }
1009
1010 boolean equalsUnchecked(int offset, Slice that, int otherOffset, int length)
1011 {
1012 if ((this == that) && (offset == otherOffset)) {
1013 return true;
1014 }
1015
1016 while (length >= SIZE_OF_LONG) {
1017 long thisLong = getLongUnchecked(offset);
1018 long thatLong = that.getLongUnchecked(otherOffset);
1019
1020 if (thisLong != thatLong) {
1021 return false;
1022 }
1023
1024 offset += SIZE_OF_LONG;
1025 otherOffset += SIZE_OF_LONG;
1026 length -= SIZE_OF_LONG;
1027 }
1028
1029 while (length > 0) {
1030 byte thisByte = getByteUnchecked(offset);
1031 byte thatByte = that.getByteUnchecked(otherOffset);
1032 if (thisByte != thatByte) {
1033 return false;
1034 }
1035 offset++;
1036 otherOffset++;
1037 length--;
1038 }
1039
1040 return true;
1041 }
1042
1043 /**
1044 * Creates a slice input backed by this slice. Any changes to this slice
1045 * will be immediately visible to the slice input.
1046 */
1047 public BasicSliceInput getInput()
1048 {
1049 return new BasicSliceInput(this);
1050 }
1051
1052 /**
1053 * Creates a slice output backed by this slice. Any data written to the
1054 * slice output will be immediately visible in this slice.
1055 */
1056 public SliceOutput getOutput()
1057 {
1058 return new BasicSliceOutput(this);
1059 }
1060
1061 /**
1062 * Decodes the contents of this slice into a string with the specified
1063 * character set name.
1064 */
1065 public String toString(Charset charset)
1066 {
1067 return toString(0, length(), charset);
1068 }
1069
1070 /**
1071 * Decodes the contents of this slice into a string using the UTF-8
1072 * character set.
1073 */
1074 public String toStringUtf8()
1075 {
1076 return toString(UTF_8);
1077 }
1078
1079 /**
1080 * Decodes the contents of this slice into a string using the US_ASCII
1081 * character set. The low order 7 bits if each byte are converted directly
1082 * into a code point for the string.
1083 */
1084 public String toStringAscii()
1085 {
1086 return toStringAscii(0, size);
1087 }
1088
1089 public String toStringAscii(int index, int length)
1090 {
1091 checkIndexLength(index, length);
1092 if (length == 0) {
1093 return "";
1094 }
1095
1096 if (base instanceof byte[]) {
1097 //noinspection deprecation
1098 return new String((byte[]) base, 0, (int) ((address - ARRAY_BYTE_BASE_OFFSET) + index), length);
1099 }
1100
1101 char[] chars = new char[length];
1102 for (int pos = index; pos < length; pos++) {
1103 chars[pos] = (char) (getByteUnchecked(pos) & 0x7F);
1104 }
1105 return new String(chars);
1106 }
1107
1108 /**
1109 * Decodes the a portion of this slice into a string with the specified
1110 * character set name.
1111 */
1112 public String toString(int index, int length, Charset charset)
1113 {
1114 if (length == 0) {
1115 return "";
1116 }
1117 if (base instanceof byte[]) {
1118 return new String((byte[]) base, (int) ((address - ARRAY_BYTE_BASE_OFFSET) + index), length, charset);
1119 }
1120 // direct memory can only be converted to a string using a ByteBuffer
1121 return decodeString(toByteBuffer(index, length), charset);
1122 }
1123
1124 public ByteBuffer toByteBuffer()
1125 {
1126 return toByteBuffer(0, size);
1127 }
1128
1129 public ByteBuffer toByteBuffer(int index, int length)
1130 {
1131 checkIndexLength(index, length);
1132
1133 if (base instanceof byte[]) {
1134 return ByteBuffer.wrap((byte[]) base, (int) ((address - ARRAY_BYTE_BASE_OFFSET) + index), length);
1135 }
1136
1137 try {
1138 return (ByteBuffer) newByteBuffer.invokeExact(address + index, length, (Object) reference);
1139 }
1140 catch (Throwable throwable) {
1141 if (throwable instanceof Error) {
1142 throw (Error) throwable;
1143 }
1144 if (throwable instanceof RuntimeException) {
1145 throw (RuntimeException) throwable;
1146 }
1147 if (throwable instanceof InterruptedException) {
1148 Thread.currentThread().interrupt();
1149 }
1150 throw new RuntimeException(throwable);
1151 }
1152 }
1153
1154 /**
1155 * Decodes the a portion of this slice into a string with the specified
1156 * character set name.
1157 */
1158 @Override
1159 public String toString()
1160 {
1161 StringBuilder builder = new StringBuilder("Slice{");
1162 if (base != null) {
1163 builder.append("base=").append(identityToString(base)).append(", ");
1164 }
1165 builder.append("address=").append(address);
1166 builder.append(", length=").append(length());
1167 builder.append('}');
1168 return builder.toString();
1169 }
1170
1171 private static String identityToString(Object o)
1172 {
1173 if (o == null) {
1174 return null;
1175 }
1176 return o.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(o));
1177 }
1178
1179 private static void copyMemory(Object src, long srcAddress, Object dest, long destAddress, int length)
1180 {
1181 // The Unsafe Javadoc specifies that the transfer size is 8 iff length % 8 == 0
1182 // so ensure that we copy big chunks whenever possible, even at the expense of two separate copy operations
1183 int bytesToCopy = length - (length % 8);
1184 unsafe.copyMemory(src, srcAddress, dest, destAddress, bytesToCopy);
1185 unsafe.copyMemory(src, srcAddress + bytesToCopy, dest, destAddress + bytesToCopy, length - bytesToCopy);
1186 }
1187
1188 private void checkIndexLength(int index, int length)
1189 {
1190 checkPositionIndexes(index, index + length, length());
1191 }
1192
1193 //
1194 // The following methods were forked from Guava primitives
1195 //
1196
1197 private static long fillLong(byte value)
1198 {
1199 return (value & 0xFFL) << 56
1200 | (value & 0xFFL) << 48
1201 | (value & 0xFFL) << 40
1202 | (value & 0xFFL) << 32
1203 | (value & 0xFFL) << 24
1204 | (value & 0xFFL) << 16
1205 | (value & 0xFFL) << 8
1206 | (value & 0xFFL);
1207 }
1208
1209 private static int compareUnsignedBytes(byte thisByte, byte thatByte)
1210 {
1211 return unsignedByteToInt(thisByte) - unsignedByteToInt(thatByte);
1212 }
1213
1214 private static int unsignedByteToInt(byte thisByte)
1215 {
1216 return thisByte & 0xFF;
1217 }
1218
1219 private static int compareUnsignedLongs(long thisLong, long thatLong)
1220 {
1221 return Long.compare(flipUnsignedLong(thisLong), flipUnsignedLong(thatLong));
1222 }
1223
1224 private static long flipUnsignedLong(long thisLong)
1225 {
1226 return thisLong ^ Long.MIN_VALUE;
1227 }
1228 }