forked from airlift/slice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slice.java
1299 lines (1148 loc) · 41.7 KB
/
Slice.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.airlift.slice;
import org.openjdk.jol.info.ClassLayout;
import sun.misc.Unsafe;
import javax.annotation.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import static io.airlift.slice.JvmUtils.bufferAddress;
import static io.airlift.slice.JvmUtils.unsafe;
import static io.airlift.slice.Preconditions.checkArgument;
import static io.airlift.slice.Preconditions.checkPositionIndexes;
import static io.airlift.slice.SizeOf.SIZE_OF_BYTE;
import static io.airlift.slice.SizeOf.SIZE_OF_DOUBLE;
import static io.airlift.slice.SizeOf.SIZE_OF_FLOAT;
import static io.airlift.slice.SizeOf.SIZE_OF_INT;
import static io.airlift.slice.SizeOf.SIZE_OF_LONG;
import static io.airlift.slice.SizeOf.SIZE_OF_SHORT;
import static io.airlift.slice.SizeOf.sizeOf;
import static io.airlift.slice.SizeOf.sizeOfBooleanArray;
import static io.airlift.slice.SizeOf.sizeOfDoubleArray;
import static io.airlift.slice.SizeOf.sizeOfFloatArray;
import static io.airlift.slice.SizeOf.sizeOfIntArray;
import static io.airlift.slice.SizeOf.sizeOfLongArray;
import static io.airlift.slice.SizeOf.sizeOfShortArray;
import static java.lang.Math.min;
import static java.lang.Math.multiplyExact;
import static java.lang.Math.toIntExact;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;
import static sun.misc.Unsafe.ARRAY_BOOLEAN_INDEX_SCALE;
import static sun.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET;
import static sun.misc.Unsafe.ARRAY_DOUBLE_INDEX_SCALE;
import static sun.misc.Unsafe.ARRAY_FLOAT_INDEX_SCALE;
import static sun.misc.Unsafe.ARRAY_INT_INDEX_SCALE;
import static sun.misc.Unsafe.ARRAY_LONG_INDEX_SCALE;
import static sun.misc.Unsafe.ARRAY_SHORT_INDEX_SCALE;
public final class Slice
implements Comparable<Slice>
{
private static final int INSTANCE_SIZE = ClassLayout.parseClass(Slice.class).instanceSize();
private static final Object COMPACT = new byte[0];
private static final Object NOT_COMPACT = null;
/**
* @deprecated use {@link Slices#wrappedBuffer(java.nio.ByteBuffer)}
*/
@Deprecated
public static Slice toUnsafeSlice(ByteBuffer byteBuffer)
{
return Slices.wrappedBuffer(byteBuffer);
}
/**
* Base object for relative addresses. If null, the address is an
* absolute location in memory.
*/
private final Object base;
/**
* If base is null, address is the absolute memory location of data for
* this slice; otherwise, address is the offset from the base object.
* This base plus relative offset addressing is taken directly from
* the Unsafe interface.
* <p>
* Note: if base object is a byte array, this address ARRAY_BYTE_BASE_OFFSET,
* since the byte array data starts AFTER the byte array object header.
*/
private final long address;
/**
* Size of the slice
*/
private final int size;
/**
* Bytes retained by the slice
*/
private final long retainedSize;
/**
* Reference has two use cases:
* <p>
* 1. It can be an object this slice must hold onto to assure that the
* underlying memory is not freed by the garbage collector.
* It is typically a ByteBuffer object, but can be any object.
* This is not needed for arrays, since the array is referenced by {@code base}.
* <p>
* 2. If reference is not used to prevent garbage collector from freeing the
* underlying memory, it will be used to indicate if the slice is compact.
* When {@code reference == COMPACT}, the slice is considered as compact.
* Otherwise, it will be null.
* <p>
* A slice is considered compact if the base object is an heap array and
* it contains the whole array.
* Thus, for the first use case, the slice is always considered as not compact.
*/
private final Object reference;
private int hash;
/**
* Creates an empty slice.
*/
Slice()
{
this.base = null;
this.address = 0;
this.size = 0;
this.retainedSize = INSTANCE_SIZE;
this.reference = COMPACT;
}
/**
* Creates a slice over the specified array.
*/
Slice(byte[] base)
{
requireNonNull(base, "base is null");
this.base = base;
this.address = ARRAY_BYTE_BASE_OFFSET;
this.size = base.length;
this.retainedSize = INSTANCE_SIZE + sizeOf(base);
this.reference = COMPACT;
}
/**
* Creates a slice over the specified array range.
*
* @param offset the array position at which the slice begins
* @param length the number of array positions to include in the slice
*/
Slice(byte[] base, int offset, int length)
{
requireNonNull(base, "base is null");
checkPositionIndexes(offset, offset + length, base.length);
this.base = base;
this.address = ARRAY_BYTE_BASE_OFFSET + offset;
this.size = length;
this.retainedSize = INSTANCE_SIZE + sizeOf(base);
this.reference = (offset == 0 && length == base.length) ? COMPACT : NOT_COMPACT;
}
/**
* Creates a slice over the specified array range.
*
* @param offset the array position at which the slice begins
* @param length the number of array positions to include in the slice
*/
Slice(boolean[] base, int offset, int length)
{
requireNonNull(base, "base is null");
checkPositionIndexes(offset, offset + length, base.length);
this.base = base;
this.address = sizeOfBooleanArray(offset);
this.size = multiplyExact(length, ARRAY_BOOLEAN_INDEX_SCALE);
this.retainedSize = INSTANCE_SIZE + sizeOf(base);
this.reference = (offset == 0 && length == base.length) ? COMPACT : NOT_COMPACT;
}
/**
* Creates a slice over the specified array range.
*
* @param offset the array position at which the slice begins
* @param length the number of array positions to include in the slice
*/
Slice(short[] base, int offset, int length)
{
requireNonNull(base, "base is null");
checkPositionIndexes(offset, offset + length, base.length);
this.base = base;
this.address = sizeOfShortArray(offset);
this.size = multiplyExact(length, ARRAY_SHORT_INDEX_SCALE);
this.retainedSize = INSTANCE_SIZE + sizeOf(base);
this.reference = (offset == 0 && length == base.length) ? COMPACT : NOT_COMPACT;
}
/**
* Creates a slice over the specified array range.
*
* @param offset the array position at which the slice begins
* @param length the number of array positions to include in the slice
*/
Slice(int[] base, int offset, int length)
{
requireNonNull(base, "base is null");
checkPositionIndexes(offset, offset + length, base.length);
this.base = base;
this.address = sizeOfIntArray(offset);
this.size = multiplyExact(length, ARRAY_INT_INDEX_SCALE);
this.retainedSize = INSTANCE_SIZE + sizeOf(base);
this.reference = (offset == 0 && length == base.length) ? COMPACT : NOT_COMPACT;
}
/**
* Creates a slice over the specified array range.
*
* @param offset the array position at which the slice begins
* @param length the number of array positions to include in the slice
*/
Slice(long[] base, int offset, int length)
{
requireNonNull(base, "base is null");
checkPositionIndexes(offset, offset + length, base.length);
this.base = base;
this.address = sizeOfLongArray(offset);
this.size = multiplyExact(length, ARRAY_LONG_INDEX_SCALE);
this.retainedSize = INSTANCE_SIZE + sizeOf(base);
this.reference = (offset == 0 && length == base.length) ? COMPACT : NOT_COMPACT;
}
/**
* Creates a slice over the specified array range.
*
* @param offset the array position at which the slice begins
* @param length the number of array positions to include in the slice
*/
Slice(float[] base, int offset, int length)
{
requireNonNull(base, "base is null");
checkPositionIndexes(offset, offset + length, base.length);
this.base = base;
this.address = sizeOfFloatArray(offset);
this.size = multiplyExact(length, ARRAY_FLOAT_INDEX_SCALE);
this.retainedSize = INSTANCE_SIZE + sizeOf(base);
this.reference = (offset == 0 && length == base.length) ? COMPACT : NOT_COMPACT;
}
/**
* Creates a slice over the specified array range.
*
* @param offset the array position at which the slice begins
* @param length the number of array positions to include in the slice
*/
Slice(double[] base, int offset, int length)
{
requireNonNull(base, "base is null");
checkPositionIndexes(offset, offset + length, base.length);
this.base = base;
this.address = sizeOfDoubleArray(offset);
this.size = multiplyExact(length, ARRAY_DOUBLE_INDEX_SCALE);
this.retainedSize = INSTANCE_SIZE + sizeOf(base);
this.reference = (offset == 0 && length == base.length) ? COMPACT : NOT_COMPACT;
}
/**
* Creates a slice for directly accessing the base object.
*/
Slice(@Nullable Object base, long address, int size, long retainedSize, @Nullable Object reference)
{
if (address <= 0) {
throw new IllegalArgumentException(format("Invalid address: %s", address));
}
if (size <= 0) {
throw new IllegalArgumentException(format("Invalid size: %s", size));
}
checkArgument((address + size) >= size, "Address + size is greater than 64 bits");
this.reference = reference;
this.base = base;
this.address = address;
this.size = size;
// INSTANCE_SIZE is not included, as the caller is responsible for including it.
this.retainedSize = retainedSize;
}
/**
* Returns the base object of this Slice, or null. This is appropriate for use
* with {@link Unsafe} if you wish to avoid all the safety belts e.g. bounds checks.
*/
public Object getBase()
{
return base;
}
/**
* Return the address offset of this Slice. This is appropriate for use
* with {@link Unsafe} if you wish to avoid all the safety belts e.g. bounds checks.
*/
public long getAddress()
{
return address;
}
/**
* Length of this slice.
*/
public int length()
{
return size;
}
/**
* Approximate number of bytes retained by this slice.
*/
public long getRetainedSize()
{
return retainedSize;
}
/**
* A slice is considered compact if the base object is an array and it contains the whole array.
* As a result, it cannot be a view of a bigger slice.
*/
public boolean isCompact()
{
return reference == COMPACT;
}
/**
* Fill the slice with the specified value;
*/
public void fill(byte value)
{
int offset = 0;
int length = size;
long longValue = fillLong(value);
while (length >= SIZE_OF_LONG) {
unsafe.putLong(base, address + offset, longValue);
offset += SIZE_OF_LONG;
length -= SIZE_OF_LONG;
}
while (length > 0) {
unsafe.putByte(base, address + offset, value);
offset++;
length--;
}
}
/**
* Fill the slice with zeros;
*/
public void clear()
{
clear(0, size);
}
public void clear(int offset, int length)
{
while (length >= SIZE_OF_LONG) {
unsafe.putLong(base, address + offset, 0);
offset += SIZE_OF_LONG;
length -= SIZE_OF_LONG;
}
while (length > 0) {
unsafe.putByte(base, address + offset, (byte) 0);
offset++;
length--;
}
}
/**
* Gets a byte at the specified absolute {@code index} in this buffer.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
* {@code index + 1} is greater than {@code this.length()}
*/
public byte getByte(int index)
{
checkIndexLength(index, SIZE_OF_BYTE);
return getByteUnchecked(index);
}
byte getByteUnchecked(int index)
{
return unsafe.getByte(base, address + index);
}
/**
* Gets an unsigned byte at the specified absolute {@code index} in this
* buffer.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
* {@code index + 1} is greater than {@code this.length()}
*/
public short getUnsignedByte(int index)
{
return (short) (getByte(index) & 0xFF);
}
/**
* Gets a 16-bit short integer at the specified absolute {@code index} in
* this slice.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
* {@code index + 2} is greater than {@code this.length()}
*/
public short getShort(int index)
{
checkIndexLength(index, SIZE_OF_SHORT);
return getShortUnchecked(index);
}
short getShortUnchecked(int index)
{
return unsafe.getShort(base, address + index);
}
/**
* Gets an unsigned 16-bit short integer at the specified absolute {@code index}
* in this slice.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
* {@code index + 2} is greater than {@code this.length()}
*/
public int getUnsignedShort(int index)
{
return getShort(index) & 0xFFFF;
}
/**
* Gets a 32-bit integer at the specified absolute {@code index} in
* this buffer.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
* {@code index + 4} is greater than {@code this.length()}
*/
public int getInt(int index)
{
checkIndexLength(index, SIZE_OF_INT);
return getIntUnchecked(index);
}
int getIntUnchecked(int index)
{
return unsafe.getInt(base, address + index);
}
/**
* Gets an unsigned 32-bit integer at the specified absolute {@code index} in
* this buffer.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
* {@code index + 4} is greater than {@code this.length()}
*/
public long getUnsignedInt(int index)
{
return getInt(index) & 0xFFFFFFFFL;
}
/**
* Gets a 64-bit long integer at the specified absolute {@code index} in
* this buffer.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
* {@code index + 8} is greater than {@code this.length()}
*/
public long getLong(int index)
{
checkIndexLength(index, SIZE_OF_LONG);
return getLongUnchecked(index);
}
long getLongUnchecked(int index)
{
return unsafe.getLong(base, address + index);
}
/**
* Gets a 32-bit float at the specified absolute {@code index} in
* this buffer.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
* {@code index + 4} is greater than {@code this.length()}
*/
public float getFloat(int index)
{
checkIndexLength(index, SIZE_OF_FLOAT);
return unsafe.getFloat(base, address + index);
}
/**
* Gets a 64-bit double at the specified absolute {@code index} in
* this buffer.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
* {@code index + 8} is greater than {@code this.length()}
*/
public double getDouble(int index)
{
checkIndexLength(index, SIZE_OF_DOUBLE);
return unsafe.getDouble(base, address + index);
}
/**
* Transfers portion of data from this slice into the specified destination starting at
* the specified absolute {@code index}.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0}, or
* if {@code index + destination.length()} is greater than {@code this.length()}
*/
public void getBytes(int index, Slice destination)
{
getBytes(index, destination, 0, destination.length());
}
/**
* Transfers portion of data from this slice into the specified destination starting at
* the specified absolute {@code index}.
*
* @param destinationIndex the first index of the destination
* @param length the number of bytes to transfer
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0},
* if the specified {@code destinationIndex} is less than {@code 0},
* if {@code index + length} is greater than
* {@code this.length()}, or
* if {@code destinationIndex + length} is greater than
* {@code destination.length()}
*/
public void getBytes(int index, Slice destination, int destinationIndex, int length)
{
destination.setBytes(destinationIndex, this, index, length);
}
/**
* Transfers portion of data from this slice into the specified destination starting at
* the specified absolute {@code index}.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0}, or
* if {@code index + destination.length} is greater than {@code this.length()}
*/
public void getBytes(int index, byte[] destination)
{
getBytes(index, destination, 0, destination.length);
}
/**
* Transfers portion of data from this slice into the specified destination starting at
* the specified absolute {@code index}.
*
* @param destinationIndex the first index of the destination
* @param length the number of bytes to transfer
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0},
* if the specified {@code destinationIndex} is less than {@code 0},
* if {@code index + length} is greater than
* {@code this.length()}, or
* if {@code destinationIndex + length} is greater than
* {@code destination.length}
*/
public void getBytes(int index, byte[] destination, int destinationIndex, int length)
{
checkIndexLength(index, length);
checkPositionIndexes(destinationIndex, destinationIndex + length, destination.length);
copyMemory(base, address + index, destination, (long) ARRAY_BYTE_BASE_OFFSET + destinationIndex, length);
}
/**
* Returns a copy of this buffer as a byte array.
*/
public byte[] getBytes()
{
return getBytes(0, length());
}
/**
* Returns a copy of this buffer as a byte array.
*
* @param index the absolute index to start at
* @param length the number of bytes to return
* @throws IndexOutOfBoundsException if the specified {@code index} is less then {@code 0},
* or if the specified {@code index + length} is greater than {@code this.length()}
*/
public byte[] getBytes(int index, int length)
{
byte[] bytes = new byte[length];
getBytes(index, bytes, 0, length);
return bytes;
}
/**
* Transfers a portion of data from this slice into the specified stream starting at the
* specified absolute {@code index}.
*
* @param length the number of bytes to transfer
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
* if {@code index + length} is greater than
* {@code this.length()}
* @throws java.io.IOException if the specified stream threw an exception during I/O
*/
public void getBytes(int index, OutputStream out, int length)
throws IOException
{
checkIndexLength(index, length);
if (base instanceof byte[]) {
out.write((byte[]) base, (int) ((address - ARRAY_BYTE_BASE_OFFSET) + index), length);
return;
}
byte[] buffer = new byte[4096];
while (length > 0) {
int size = min(buffer.length, length);
getBytes(index, buffer, 0, size);
out.write(buffer, 0, size);
length -= size;
index += size;
}
}
/**
* Sets the specified byte at the specified absolute {@code index} in this
* buffer. The 24 high-order bits of the specified value are ignored.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
* {@code index + 1} is greater than {@code this.length()}
*/
public void setByte(int index, int value)
{
checkIndexLength(index, SIZE_OF_BYTE);
setByteUnchecked(index, value);
}
void setByteUnchecked(int index, int value)
{
unsafe.putByte(base, address + index, (byte) (value & 0xFF));
}
/**
* Sets the specified 16-bit short integer at the specified absolute
* {@code index} in this buffer. The 16 high-order bits of the specified
* value are ignored.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
* {@code index + 2} is greater than {@code this.length()}
*/
public void setShort(int index, int value)
{
checkIndexLength(index, SIZE_OF_SHORT);
setShortUnchecked(index, value);
}
void setShortUnchecked(int index, int value)
{
unsafe.putShort(base, address + index, (short) (value & 0xFFFF));
}
/**
* Sets the specified 32-bit integer at the specified absolute
* {@code index} in this buffer.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
* {@code index + 4} is greater than {@code this.length()}
*/
public void setInt(int index, int value)
{
checkIndexLength(index, SIZE_OF_INT);
setIntUnchecked(index, value);
}
void setIntUnchecked(int index, int value)
{
unsafe.putInt(base, address + index, value);
}
/**
* Sets the specified 64-bit long integer at the specified absolute
* {@code index} in this buffer.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
* {@code index + 8} is greater than {@code this.length()}
*/
public void setLong(int index, long value)
{
checkIndexLength(index, SIZE_OF_LONG);
setLongUnchecked(index, value);
}
void setLongUnchecked(int index, long value)
{
unsafe.putLong(base, address + index, value);
}
/**
* Sets the specified 32-bit float at the specified absolute
* {@code index} in this buffer.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
* {@code index + 4} is greater than {@code this.length()}
*/
public void setFloat(int index, float value)
{
checkIndexLength(index, SIZE_OF_FLOAT);
unsafe.putFloat(base, address + index, value);
}
/**
* Sets the specified 64-bit double at the specified absolute
* {@code index} in this buffer.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0} or
* {@code index + 8} is greater than {@code this.length()}
*/
public void setDouble(int index, double value)
{
checkIndexLength(index, SIZE_OF_DOUBLE);
unsafe.putDouble(base, address + index, value);
}
/**
* Transfers data from the specified slice into this buffer starting at
* the specified absolute {@code index}.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0}, or
* if {@code index + source.length()} is greater than {@code this.length()}
*/
public void setBytes(int index, Slice source)
{
setBytes(index, source, 0, source.length());
}
/**
* Transfers data from the specified slice into this buffer starting at
* the specified absolute {@code index}.
*
* @param sourceIndex the first index of the source
* @param length the number of bytes to transfer
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0},
* if the specified {@code sourceIndex} is less than {@code 0},
* if {@code index + length} is greater than
* {@code this.length()}, or
* if {@code sourceIndex + length} is greater than
* {@code source.length()}
*/
public void setBytes(int index, Slice source, int sourceIndex, int length)
{
checkIndexLength(index, length);
checkPositionIndexes(sourceIndex, sourceIndex + length, source.length());
copyMemory(source.base, source.address + sourceIndex, base, address + index, length);
}
/**
* Transfers data from the specified slice into this buffer starting at
* the specified absolute {@code index}.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0}, or
* if {@code index + source.length} is greater than {@code this.length()}
*/
public void setBytes(int index, byte[] source)
{
setBytes(index, source, 0, source.length);
}
/**
* Transfers data from the specified array into this buffer starting at
* the specified absolute {@code index}.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0},
* if the specified {@code sourceIndex} is less than {@code 0},
* if {@code index + length} is greater than
* {@code this.length()}, or
* if {@code sourceIndex + length} is greater than {@code source.length}
*/
public void setBytes(int index, byte[] source, int sourceIndex, int length)
{
checkIndexLength(index, length);
checkPositionIndexes(sourceIndex, sourceIndex + length, source.length);
copyMemory(source, (long) ARRAY_BYTE_BASE_OFFSET + sourceIndex, base, address + index, length);
}
/**
* Transfers data from the specified input stream into this slice starting at
* the specified absolute {@code index}.
*
* @throws IndexOutOfBoundsException if the specified {@code index} is less than {@code 0}, or
* if {@code index + source.length} is greater than {@code this.length()}
*/
public void setBytes(int index, InputStream in, int length)
throws IOException
{
checkIndexLength(index, length);
if (base instanceof byte[]) {
byte[] bytes = (byte[]) base;
int offset = (int) ((address - ARRAY_BYTE_BASE_OFFSET) + index);
while (length > 0) {
int bytesRead = in.read(bytes, offset, length);
if (bytesRead < 0) {
throw new IndexOutOfBoundsException("End of stream");
}
length -= bytesRead;
offset += bytesRead;
}
return;
}
byte[] bytes = new byte[4096];
while (length > 0) {
int bytesRead = in.read(bytes, 0, min(bytes.length, length));
if (bytesRead < 0) {
throw new IndexOutOfBoundsException("End of stream");
}
copyMemory(bytes, ARRAY_BYTE_BASE_OFFSET, base, address + index, bytesRead);
length -= bytesRead;
index += bytesRead;
}
}
/**
* Returns a slice of this buffer's sub-region. Modifying the content of
* the returned buffer or this buffer affects each other's content.
*/
public Slice slice(int index, int length)
{
if ((index == 0) && (length == length())) {
return this;
}
checkIndexLength(index, length);
if (length == 0) {
return Slices.EMPTY_SLICE;
}
if (reference == COMPACT) {
return new Slice(base, address + index, length, retainedSize, NOT_COMPACT);
}
return new Slice(base, address + index, length, retainedSize, reference);
}
public int indexOfByte(int b)
{
b = b & 0xFF;
for (int i = 0; i < size; i++) {
if (getByteUnchecked(i) == b) {
return i;
}
}
return -1;
}
/**
* Returns the index of the first occurrence of the pattern with this slice.
* If the pattern is not found -1 is returned. If patten is empty, zero is
* returned.
*/
public int indexOf(Slice slice)
{
return indexOf(slice, 0);
}
/**
* Returns the index of the first occurrence of the pattern with this slice.
* If the pattern is not found -1 is returned If patten is empty, the offset
* is returned.
*/
public int indexOf(Slice pattern, int offset)
{
if (size == 0 || offset >= size) {
return -1;
}
if (pattern.length() == 0) {
return offset;
}
// Do we have enough characters
if (pattern.length() < SIZE_OF_INT || size < SIZE_OF_LONG) {
return indexOfBruteForce(pattern, offset);
}
// Using first four bytes for faster search. We are not using eight bytes for long
// because we want more strings to get use of fast search.
int head = pattern.getIntUnchecked(0);
// Take the first byte of head for faster skipping
int firstByteMask = head & 0xff;
firstByteMask |= firstByteMask << 8;
firstByteMask |= firstByteMask << 16;
int lastValidIndex = size - pattern.length();
int index = offset;
while (index <= lastValidIndex) {
// Read four bytes in sequence
int value = getIntUnchecked(index);
// Compare all bytes of value with first byte of search data
// see https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord
int valueXor = value ^ firstByteMask;
int hasZeroBytes = (valueXor - 0x01010101) & ~valueXor & 0x80808080;
// If valueXor doesn't not have any zero byte then there is no match and we can advance
if (hasZeroBytes == 0) {
index += SIZE_OF_INT;
continue;
}
// Try fast match of head and the rest
if (value == head && equalsUnchecked(index, pattern, 0, pattern.length())) {
return index;
}
index++;
}
return -1;
}
int indexOfBruteForce(Slice pattern, int offset)
{
if (size == 0 || offset >= size) {
return -1;
}
if (pattern.length() == 0) {
return offset;
}
byte firstByte = pattern.getByteUnchecked(0);
int lastValidIndex = size - pattern.length();
int index = offset;
while (true) {
// seek to first byte match
while (index < lastValidIndex && getByteUnchecked(index) != firstByte) {
index++;
}
if (index > lastValidIndex) {
break;
}
if (equalsUnchecked(index, pattern, 0, pattern.length())) {
return index;
}
index++;
}
return -1;
}
/**
* Compares the content of the specified buffer to the content of this
* buffer. This comparison is performed byte by byte using an unsigned
* comparison.
*/
@SuppressWarnings("ObjectEquality")
@Override
public int compareTo(Slice that)
{
if (this == that) {
return 0;
}
return compareTo(0, size, that, 0, that.size);
}
/**
* Compares a portion of this slice with a portion of the specified slice. Equality is
* solely based on the contents of the slice.
*/
@SuppressWarnings("ObjectEquality")
public int compareTo(int offset, int length, Slice that, int otherOffset, int otherLength)
{
if ((this == that) && (offset == otherOffset) && (length == otherLength)) {
return 0;
}
checkIndexLength(offset, length);
that.checkIndexLength(otherOffset, otherLength);
long thisAddress = address + offset;
long thatAddress = that.address + otherOffset;
int compareLength = min(length, otherLength);
while (compareLength >= SIZE_OF_LONG) {
long thisLong = unsafe.getLong(base, thisAddress);
long thatLong = unsafe.getLong(that.base, thatAddress);
if (thisLong != thatLong) {
return longBytesToLong(thisLong) < longBytesToLong(thatLong) ? -1 : 1;
}
thisAddress += SIZE_OF_LONG;