Repository navigation
Expand file tree
/
Copy pathArrowBuf.java
More file actions
1231 lines (1118 loc) · 42.2 KB
/
Copy pathArrowBuf.java
File metadata and controls
1231 lines (1118 loc) · 42.2 KB
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 to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.netty.buffer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ReadOnlyBufferException;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.arrow.memory.AllocationManager;
import org.apache.arrow.memory.BaseAllocator;
import org.apache.arrow.memory.BaseAllocator.Verbosity;
import org.apache.arrow.memory.BoundsChecking;
import org.apache.arrow.memory.BufferLedger;
import org.apache.arrow.memory.BufferManager;
import org.apache.arrow.memory.ReferenceManager;
import org.apache.arrow.memory.util.HistoricalLog;
import org.apache.arrow.util.Preconditions;
import io.netty.util.internal.PlatformDependent;
/**
* ArrowBuf serves as a facade over underlying memory by providing
* several access APIs to read/write data into a chunk of direct
* memory. All the accounting, ownership and reference management
* is done by {@link ReferenceManager} and ArrowBuf can work
* with a custom user provided implementation of ReferenceManager
* <p>
* Two important instance variables of an ArrowBuf:
* (1) address - starting virtual address in the underlying memory
* chunk that this ArrowBuf has access to
* (2) length - length (in bytes) in the underlying memory chunk
* that this ArrowBuf has access to
* </p>
* <p>
* The mangement (allocation, deallocation, reference counting etc) for
* the memory chunk is not done by ArrowBuf.
* Default implementation of ReferenceManager, allocation is in
* {@link BaseAllocator}, {@link BufferLedger} and {@link AllocationManager}
* </p>
*/
public final class ArrowBuf implements AutoCloseable {
private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ArrowBuf.class);
private static final int SHORT_SIZE = Short.BYTES;
private static final int INT_SIZE = Integer.BYTES;
private static final int FLOAT_SIZE = Float.BYTES;
private static final int DOUBLE_SIZE = Double.BYTES;
private static final int LONG_SIZE = Long.BYTES;
private static final AtomicLong idGenerator = new AtomicLong(0);
private static final int LOG_BYTES_PER_ROW = 10;
private final long id = idGenerator.incrementAndGet();
private final ReferenceManager referenceManager;
private final BufferManager bufferManager;
private final long addr;
private final boolean isEmpty;
private int readerIndex;
private int writerIndex;
private final HistoricalLog historicalLog = BaseAllocator.DEBUG ?
new HistoricalLog(BaseAllocator.DEBUG_LOG_LENGTH, "ArrowBuf[%d]", id) : null;
private volatile int length;
/**
* Constructs a new ArrowBuf
* @param referenceManager The memory manager to track memory usage and reference count of this buffer
* @param length The byte length of this buffer
* @param isEmpty Indicates if this buffer is empty which enables some optimizations.
*/
public ArrowBuf(
final ReferenceManager referenceManager,
final BufferManager bufferManager,
final int length,
final long memoryAddress,
boolean isEmpty) {
this.referenceManager = referenceManager;
this.bufferManager = bufferManager;
this.isEmpty = isEmpty;
this.addr = memoryAddress;
this.length = length;
this.readerIndex = 0;
this.writerIndex = 0;
if (BaseAllocator.DEBUG) {
historicalLog.recordEvent("create()");
}
}
public int refCnt() {
return isEmpty ? 1 : referenceManager.getRefCount();
}
/**
* Allows a function to determine whether not reading a particular string of bytes is valid.
*
* <p>Will throw an exception if the memory is not readable for some reason. Only doesn't
* something in the case that
* AssertionUtil.BOUNDS_CHECKING_ENABLED is true.
*
* @param start The starting position of the bytes to be read.
* @param end The exclusive endpoint of the bytes to be read.
*/
public void checkBytes(int start, int end) {
if (BoundsChecking.BOUNDS_CHECKING_ENABLED) {
checkIndexD(start, end - start);
}
}
/**
* For get/set operations, reference count should be >= 1.
*/
private void ensureAccessible() {
if (this.refCnt() == 0) {
throw new IllegalStateException("Ref count should be >= 1 for accessing the ArrowBuf");
}
}
/**
* Get a wrapper buffer to comply with Netty interfaces and
* can be used in RPC/RPC allocator code.
* @return netty compliant {@link NettyArrowBuf}
*/
public NettyArrowBuf asNettyBuffer() {
final NettyArrowBuf nettyArrowBuf = new NettyArrowBuf(
this,
isEmpty ? null : referenceManager.getAllocator().getAsByteBufAllocator(),
length);
nettyArrowBuf.readerIndex(readerIndex);
nettyArrowBuf.writerIndex(writerIndex);
return nettyArrowBuf;
}
/**
* Get reference manager for this ArrowBuf.
* @return user provided implementation of {@link ReferenceManager}
*/
public ReferenceManager getReferenceManager() {
return referenceManager;
}
public boolean isEmpty() {
return isEmpty;
}
public int capacity() {
return length;
}
/**
* Adjusts the capacity of this buffer. Size increases are NOT supported.
*
* @param newCapacity Must be in in the range [0, length).
*/
public synchronized ArrowBuf capacity(int newCapacity) {
if (newCapacity == length) {
return this;
}
Preconditions.checkArgument(newCapacity >= 0);
if (newCapacity < length) {
length = newCapacity;
return this;
}
throw new UnsupportedOperationException("Buffers don't support resizing that increases the size.");
}
/**
* Returns the byte order of elements in this buffer.
*/
public ByteOrder order() {
return ByteOrder.LITTLE_ENDIAN;
}
/**
* Returns the number of bytes still available to read in this buffer.
*/
public int readableBytes() {
Preconditions.checkState(writerIndex >= readerIndex,
"Writer index cannot be less than reader index");
return writerIndex - readerIndex;
}
/**
* Returns the number of bytes still available to write into this buffer before capacity is reached.
*/
public int writableBytes() {
return capacity() - writerIndex;
}
/**
* Returns a slice of only the readable bytes in the buffer.
*/
public ArrowBuf slice() {
return slice(readerIndex, readableBytes());
}
/**
* Returns a slice (view) starting at <code>index</code> with the given <code>length</code>.
*/
public ArrowBuf slice(int index, int length) {
if (isEmpty) {
return this;
}
Preconditions.checkPositionIndex(index, this.length);
Preconditions.checkPositionIndex(index + length, this.length);
/*
* Re the behavior of reference counting, see http://netty.io/wiki/reference-counted-objects
* .html#wiki-h3-5, which
* explains that derived buffers share their reference count with their parent
*/
final ArrowBuf newBuf = referenceManager.deriveBuffer(this, index, length);
newBuf.writerIndex(length);
return newBuf;
}
public ByteBuffer nioBuffer() {
return isEmpty ? ByteBuffer.allocateDirect(0) : asNettyBuffer().nioBuffer();
}
public ByteBuffer nioBuffer(int index, int length) {
return isEmpty ? ByteBuffer.allocateDirect(0) : asNettyBuffer().nioBuffer(index, length);
}
public long memoryAddress() {
return this.addr;
}
@Override
public String toString() {
return String.format("ArrowBuf[%d], address:%d, length:%d", id, memoryAddress(), length);
}
@Override
public int hashCode() {
return System.identityHashCode(this);
}
@Override
public boolean equals(Object obj) {
// identity equals only.
return this == obj;
}
/*
* IMPORTANT NOTE
* The data getters and setters work with a caller provided
* index. This index is 0 based and since ArrowBuf has access
* to a portion of underlying chunk of memory starting at
* some address, we convert the given relative index into
* absolute index as memory address + index.
*
* Example:
*
* Let's say we have an underlying chunk of memory of length 64 bytes
* Now let's say we have an ArrowBuf that has access to the chunk
* from offset 4 for length of 16 bytes.
*
* If the starting virtual address of chunk is MAR, then memory
* address of this ArrowBuf is MAR + offset -- this is what is stored
* in variable addr. See the BufferLedger and AllocationManager code
* for the implementation of ReferenceManager that manages a
* chunk of memory and creates ArrowBuf with access to a range of
* bytes within the chunk (or the entire chunk)
*
* So now to get/set data, we will do => addr + index
* This logic is put in method addr(index) and is frequently
* used in get/set data methods to compute the absolute
* byte address for get/set operation in the underlying chunk
*
* @param index the index at which we the user wants to read/write
* @return the absolute address within the memory
*/
private long addr(int index) {
return addr + index;
}
/*-------------------------------------------------*
| Following are a set of fast path data set and |
| get APIs to write/read data from ArrowBuf |
| at a given index (0 based relative to this |
| ArrowBuf and not relative to the underlying |
| memory chunk). |
| |
*-------------------------------------------------*/
/**
* Helper function to do bounds checking at a particular
* index for particular length of data.
* @param index index (0 based relative to this ArrowBuf)
* @param length provided length of data for get/set
*/
private void chk(int index, int length) {
if (BoundsChecking.BOUNDS_CHECKING_ENABLED) {
checkIndexD(index, length);
}
}
private void checkIndexD(int index, int fieldLength) {
// check reference count
ensureAccessible();
// check bounds
Preconditions.checkArgument(fieldLength >= 0, "expecting non-negative data length");
if (index < 0 || index > capacity() - fieldLength) {
if (BaseAllocator.DEBUG) {
historicalLog.logHistory(logger);
}
throw new IndexOutOfBoundsException(String.format(
"index: %d, length: %d (expected: range(0, %d))", index, fieldLength, capacity()));
}
}
/**
* Get long value stored at a particular index in the
* underlying memory chunk this ArrowBuf has access to.
* @param index index (0 based relative to this ArrowBuf)
* where the value will be read from
* @return 8 byte long value
*/
public long getLong(int index) {
chk(index, LONG_SIZE);
return PlatformDependent.getLong(addr(index));
}
/**
* Set long value at a particular index in the
* underlying memory chunk this ArrowBuf has access to.
* @param index index (0 based relative to this ArrowBuf)
* where the value will be written
* @param value value to write
*/
public void setLong(int index, long value) {
chk(index, LONG_SIZE);
PlatformDependent.putLong(addr(index), value);
}
/**
* Get float value stored at a particular index in the
* underlying memory chunk this ArrowBuf has access to.
* @param index index (0 based relative to this ArrowBuf)
* where the value will be read from
* @return 4 byte float value
*/
public float getFloat(int index) {
return Float.intBitsToFloat(getInt(index));
}
/**
* Set float value at a particular index in the
* underlying memory chunk this ArrowBuf has access to.
* @param index index (0 based relative to this ArrowBuf)
* where the value will be written
* @param value value to write
*/
public void setFloat(int index, float value) {
chk(index, FLOAT_SIZE);
PlatformDependent.putInt(addr(index), Float.floatToRawIntBits(value));
}
/**
* Get double value stored at a particular index in the
* underlying memory chunk this ArrowBuf has access to.
* @param index index (0 based relative to this ArrowBuf)
* where the value will be read from
* @return 8 byte double value
*/
public double getDouble(int index) {
return Double.longBitsToDouble(getLong(index));
}
/**
* Set double value at a particular index in the
* underlying memory chunk this ArrowBuf has access to.
* @param index index (0 based relative to this ArrowBuf)
* where the value will be written
* @param value value to write
*/
public void setDouble(int index, double value) {
chk(index, DOUBLE_SIZE);
PlatformDependent.putLong(addr(index), Double.doubleToRawLongBits(value));
}
/**
* Get char value stored at a particular index in the
* underlying memory chunk this ArrowBuf has access to.
* @param index index (0 based relative to this ArrowBuf)
* where the value will be read from
* @return 2 byte char value
*/
public char getChar(int index) {
return (char) getShort(index);
}
/**
* Set char value at a particular index in the
* underlying memory chunk this ArrowBuf has access to.
* @param index index (0 based relative to this ArrowBuf)
* where the value will be written
* @param value value to write
*/
public void setChar(int index, int value) {
chk(index, SHORT_SIZE);
PlatformDependent.putShort(addr(index), (short) value);
}
/**
* Get int value stored at a particular index in the
* underlying memory chunk this ArrowBuf has access to.
* @param index index (0 based relative to this ArrowBuf)
* where the value will be read from
* @return 4 byte int value
*/
public int getInt(int index) {
chk(index, INT_SIZE);
return PlatformDependent.getInt(addr(index));
}
/**
* Set int value at a particular index in the
* underlying memory chunk this ArrowBuf has access to.
* @param index index (0 based relative to this ArrowBuf)
* where the value will be written
* @param value value to write
*/
public void setInt(int index, int value) {
chk(index, INT_SIZE);
PlatformDependent.putInt(addr(index), value);
}
/**
* Get short value stored at a particular index in the
* underlying memory chunk this ArrowBuf has access to.
* @param index index (0 based relative to this ArrowBuf)
* where the value will be read from
* @return 2 byte short value
*/
public short getShort(int index) {
chk(index, SHORT_SIZE);
return PlatformDependent.getShort(addr(index));
}
/**
* Set short value at a particular index in the
* underlying memory chunk this ArrowBuf has access to.
* @param index index (0 based relative to this ArrowBuf)
* where the value will be written
* @param value value to write
*/
public void setShort(int index, int value) {
setShort(index, (short)value);
}
/**
* Set short value at a particular index in the
* underlying memory chunk this ArrowBuf has access to.
* @param index index (0 based relative to this ArrowBuf)
* where the value will be written
* @param value value to write
*/
public void setShort(int index, short value) {
chk(index, SHORT_SIZE);
PlatformDependent.putShort(addr(index), value);
}
/**
* Set byte value at a particular index in the
* underlying memory chunk this ArrowBuf has access to.
* @param index index (0 based relative to this ArrowBuf)
* where the value will be written
* @param value value to write
*/
public void setByte(int index, int value) {
chk(index, 1);
PlatformDependent.putByte(addr(index), (byte) value);
}
/**
* Set byte value at a particular index in the
* underlying memory chunk this ArrowBuf has access to.
* @param index index (0 based relative to this ArrowBuf)
* where the value will be written
* @param value value to write
*/
public void setByte(int index, byte value) {
chk(index, 1);
PlatformDependent.putByte(addr(index), value);
}
/**
* Get byte value stored at a particular index in the
* underlying memory chunk this ArrowBuf has access to.
* @param index index (0 based relative to this ArrowBuf)
* where the value will be read from
* @return byte value
*/
public byte getByte(int index) {
chk(index, 1);
return PlatformDependent.getByte(addr(index));
}
/*--------------------------------------------------*
| Following are another set of data set APIs |
| that directly work with writerIndex |
| |
*--------------------------------------------------*/
/**
* Helper function to do bound checking w.r.t writerIndex
* by checking if we can set "length" bytes of data at the
* writerIndex in this ArrowBuf.
* @param length provided length of data for set
*/
private void ensureWritable(final int length) {
if (BoundsChecking.BOUNDS_CHECKING_ENABLED) {
Preconditions.checkArgument(length >= 0, "expecting non-negative length");
// check reference count
this.ensureAccessible();
// check bounds
if (length > writableBytes()) {
throw new IndexOutOfBoundsException(
String.format("writerIndex(%d) + length(%d) exceeds capacity(%d)", writerIndex, length, capacity()));
}
}
}
/**
* Helper function to do bound checking w.r.t readerIndex
* by checking if we can read "length" bytes of data at the
* readerIndex in this ArrowBuf.
* @param length provided length of data for get
*/
private void ensureReadable(final int length) {
if (BoundsChecking.BOUNDS_CHECKING_ENABLED) {
Preconditions.checkArgument(length >= 0, "expecting non-negative length");
// check reference count
this.ensureAccessible();
// check bounds
if (length > readableBytes()) {
throw new IndexOutOfBoundsException(
String.format("readerIndex(%d) + length(%d) exceeds writerIndex(%d)", readerIndex, length, writerIndex));
}
}
}
/**
* Read the byte at readerIndex.
* @return byte value
*/
public byte readByte() {
ensureReadable(1);
final byte b = getByte(readerIndex);
++readerIndex;
return b;
}
/**
* Read dst.length bytes at readerIndex into dst byte array
* @param dst byte array where the data will be written
*/
public void readBytes(byte[] dst) {
Preconditions.checkArgument(dst != null, "expecting valid dst bytearray");
ensureReadable(dst.length);
getBytes(readerIndex, dst, 0, dst.length);
}
/**
* Set the provided byte value at the writerIndex.
* @param value value to set
*/
public void writeByte(byte value) {
ensureWritable(1);
PlatformDependent.putByte(addr(writerIndex), value);
++writerIndex;
}
/**
* Set the lower order byte for the provided value at
* the writerIndex.
* @param value value to be set
*/
public void writeByte(int value) {
ensureWritable(1);
PlatformDependent.putByte(addr(writerIndex), (byte)value);
++writerIndex;
}
/**
* Write the bytes from given byte array into this
* ArrowBuf starting at writerIndex.
* @param src src byte array
*/
public void writeBytes(byte[] src) {
Preconditions.checkArgument(src != null, "expecting valid src array");
writeBytes(src, 0, src.length);
}
/**
* Write the bytes from given byte array starting at srcIndex
* into this ArrowBuf starting at writerIndex.
* @param src src byte array
* @param srcIndex index in the byte array where the copy will being from
* @param length length of data to copy
*/
public void writeBytes(byte[] src, int srcIndex, int length) {
ensureWritable(length);
setBytes(writerIndex, src, srcIndex, length);
writerIndex += length;
}
/**
* Set the provided int value as short at the writerIndex.
* @param value value to set
*/
public void writeShort(int value) {
ensureWritable(SHORT_SIZE);
PlatformDependent.putShort(addr(writerIndex), (short) value);
writerIndex += SHORT_SIZE;
}
/**
* Set the provided int value at the writerIndex.
* @param value value to set
*/
public void writeInt(int value) {
ensureWritable(INT_SIZE);
PlatformDependent.putInt(addr(writerIndex), value);
writerIndex += INT_SIZE;
}
/**
* Set the provided long value at the writerIndex.
* @param value value to set
*/
public void writeLong(long value) {
ensureWritable(LONG_SIZE);
PlatformDependent.putLong(addr(writerIndex), value);
writerIndex += LONG_SIZE;
}
/**
* Set the provided float value at the writerIndex.
* @param value value to set
*/
public void writeFloat(float value) {
ensureWritable(FLOAT_SIZE);
PlatformDependent.putInt(addr(writerIndex), Float.floatToRawIntBits(value));
writerIndex += FLOAT_SIZE;
}
/**
* Set the provided double value at the writerIndex.
* @param value value to set
*/
public void writeDouble(double value) {
ensureWritable(DOUBLE_SIZE);
PlatformDependent.putLong(addr(writerIndex), Double.doubleToRawLongBits(value));
writerIndex += DOUBLE_SIZE;
}
/*--------------------------------------------------*
| Following are another set of data set/get APIs |
| that read and write stream of bytes from/to byte |
| arrays, ByteBuffer, ArrowBuf etc |
| |
*--------------------------------------------------*/
/**
* Determine if the requested {@code index} and {@code length} will fit within {@code capacity}.
* @param index The starting index.
* @param length The length which will be utilized (starting from {@code index}).
* @param capacity The capacity that {@code index + length} is allowed to be within.
* @return {@code true} if the requested {@code index} and {@code length} will fit within {@code capacity}.
* {@code false} if this would result in an index out of bounds exception.
*/
private static boolean isOutOfBounds(int index, int length, int capacity) {
return (index | length | (index + length) | (capacity - (index + length))) < 0;
}
private void checkIndex(int index, int fieldLength) {
// check reference count
this.ensureAccessible();
// check bounds
if (isOutOfBounds(index, fieldLength, this.capacity())) {
throw new IndexOutOfBoundsException(String.format("index: %d, length: %d (expected: range(0, %d))",
index, fieldLength, this.capacity()));
}
}
/**
* Copy data from this ArrowBuf at a given index in into destination
* byte array.
* @param index starting index (0 based relative to the portion of memory)
* this ArrowBuf has access to
* @param dst byte array to copy the data into
*/
public void getBytes(int index, byte[] dst) {
getBytes(index, dst, 0, dst.length);
}
/**
* Copy data from this ArrowBuf at a given index into destination byte array.
* @param index index (0 based relative to the portion of memory
* this ArrowBuf has access to)
* @param dst byte array to copy the data into
* @param dstIndex starting index in dst byte array to copy into
* @param length length of data to copy from this ArrowBuf
*/
public void getBytes(int index, byte[] dst, int dstIndex, int length) {
// bound check for this ArrowBuf where the data will be copied from
checkIndex(index, length);
// null check
Preconditions.checkArgument(dst != null, "expecting a valid dst byte array");
// bound check for dst byte array where the data will be copied to
if (isOutOfBounds(dstIndex, length, dst.length)) {
// not enough space to copy "length" bytes into dst array from dstIndex onwards
throw new IndexOutOfBoundsException("Not enough space to copy data into destination" + dstIndex);
}
if (length != 0) {
// copy "length" bytes from this ArrowBuf starting at addr(index) address
// into dst byte array at dstIndex onwards
PlatformDependent.copyMemory(addr(index), dst, dstIndex, (long)length);
}
}
/**
* Copy data from a given byte array into this ArrowBuf starting at
* a given index.
* @param index starting index (0 based relative to the portion of memory)
* this ArrowBuf has access to
* @param src byte array to copy the data from
*/
public void setBytes(int index, byte[] src) {
setBytes(index, src, 0, src.length);
}
/**
* Copy data from a given byte array starting at the given source index into
* this ArrowBuf at a given index.
* @param index index (0 based relative to the portion of memory this ArrowBuf
* has access to)
* @param src src byte array to copy the data from
* @param srcIndex index in the byte array where the copy will start from
* @param length length of data to copy from byte array
*/
public void setBytes(int index, byte[] src, int srcIndex, int length) {
// bound check for this ArrowBuf where the data will be copied into
checkIndex(index, length);
// null check
Preconditions.checkArgument(src != null, "expecting a valid src byte array");
// bound check for src byte array where the data will be copied from
if (isOutOfBounds(srcIndex, length, src.length)) {
// not enough space to copy "length" bytes into dst array from dstIndex onwards
throw new IndexOutOfBoundsException("Not enough space to copy data from byte array" + srcIndex);
}
if (length > 0) {
// copy "length" bytes from src byte array at the starting index (srcIndex)
// into this ArrowBuf starting at address "addr(index)"
PlatformDependent.copyMemory(src, srcIndex, addr(index), (long)length);
}
}
/**
* Copy data from this ArrowBuf at a given index into the destination
* ByteBuffer.
* @param index index (0 based relative to the portion of memory this ArrowBuf
* has access to)
* @param dst dst ByteBuffer where the data will be copied into
*/
public void getBytes(int index, ByteBuffer dst) {
// bound check for this ArrowBuf where the data will be copied from
checkIndex(index, dst.remaining());
// dst.remaining() bytes of data will be copied into dst ByteBuffer
if (dst.remaining() != 0) {
// address in this ArrowBuf where the copy will begin from
final long srcAddress = addr(index);
if (dst.isDirect()) {
if (dst.isReadOnly()) {
throw new ReadOnlyBufferException();
}
// copy dst.remaining() bytes of data from this ArrowBuf starting
// at address srcAddress into the dst ByteBuffer starting at
// address dstAddress
final long dstAddress = PlatformDependent.directBufferAddress(dst) + (long)dst.position();
PlatformDependent.copyMemory(srcAddress, dstAddress, (long)dst.remaining());
// after copy, bump the next write position for the dst ByteBuffer
dst.position(dst.position() + dst.remaining());
} else if (dst.hasArray()) {
// copy dst.remaining() bytes of data from this ArrowBuf starting
// at address srcAddress into the dst ByteBuffer starting at
// index dstIndex
final int dstIndex = dst.arrayOffset() + dst.position();
PlatformDependent.copyMemory(srcAddress, dst.array(), dstIndex, (long)dst.remaining());
// after copy, bump the next write position for the dst ByteBuffer
dst.position(dst.position() + dst.remaining());
} else {
throw new UnsupportedOperationException("Copy from this ArrowBuf to ByteBuffer is not supported");
}
}
}
/**
* Copy data into this ArrowBuf at a given index onwards from
* a source ByteBuffer.
* @param index index index (0 based relative to the portion of memory
* this ArrowBuf has access to)
* @param src src ByteBuffer where the data will be copied from
*/
public void setBytes(int index, ByteBuffer src) {
// bound check for this ArrowBuf where the data will be copied into
checkIndex(index, src.remaining());
// length of data to copy
int length = src.remaining();
// address in this ArrowBuf where the data will be copied to
long dstAddress = addr(index);
if (length != 0) {
if (src.isDirect()) {
// copy src.remaining() bytes of data from src ByteBuffer starting at
// address srcAddress into this ArrowBuf starting at address dstAddress
final long srcAddress = PlatformDependent.directBufferAddress(src) + (long)src.position();
PlatformDependent.copyMemory(srcAddress, dstAddress, (long)length);
// after copy, bump the next read position for the src ByteBuffer
src.position(src.position() + length);
} else if (src.hasArray()) {
// copy src.remaining() bytes of data from src ByteBuffer starting at
// index srcIndex into this ArrowBuf starting at address dstAddress
final int srcIndex = src.arrayOffset() + src.position();
PlatformDependent.copyMemory(src.array(), srcIndex, dstAddress, (long)length);
// after copy, bump the next read position for the src ByteBuffer
src.position(src.position() + length);
} else {
// copy word at a time
while (length >= LONG_SIZE) {
PlatformDependent.putLong(dstAddress, src.getLong());
length -= LONG_SIZE;
dstAddress += LONG_SIZE;
}
// copy last byte
while (length > 0) {
PlatformDependent.putByte(dstAddress, src.get());
--length;
++dstAddress;
}
}
}
}
/**
* Copy data into this ArrowBuf at a given index onwards from
* a source ByteBuffer starting at a given srcIndex for a certain
* length.
* @param index index (0 based relative to the portion of memory
* this ArrowBuf has access to)
* @param src src ByteBuffer where the data will be copied from
* @param srcIndex starting index in the src ByteBuffer where the data copy
* will start from
* @param length length of data to copy from src ByteBuffer
*/
public void setBytes(int index, ByteBuffer src, int srcIndex, int length) {
// bound check for this ArrowBuf where the data will be copied into
checkIndex(index, length);
if (src.isDirect()) {
// copy length bytes of data from src ByteBuffer starting at address
// srcAddress into this ArrowBuf at address dstAddress
final long srcAddress = PlatformDependent.directBufferAddress(src) + (long)srcIndex;
final long dstAddress = addr(index);
PlatformDependent.copyMemory(srcAddress, dstAddress, (long)length);
} else {
if (srcIndex == 0 && src.capacity() == length) {
// copy the entire ByteBuffer from start to end of length
setBytes(index, src);
} else {
ByteBuffer newBuf = src.duplicate();
newBuf.position(srcIndex);
newBuf.limit(srcIndex + length);
setBytes(index, newBuf);
}
}
}
/**
* Copy a given length of data from this ArrowBuf starting at a given index
* into a dst ArrowBuf at dstIndex.
* @param index index (0 based relative to the portion of memory
* this ArrowBuf has access to)
* @param dst dst ArrowBuf where the data will be copied into
* @param dstIndex index (0 based relative to the portion of memory
* dst ArrowBuf has access to)
* @param length length of data to copy
*/
public void getBytes(int index, ArrowBuf dst, int dstIndex, int length) {
// bound check for this ArrowBuf where the data will be copied from
checkIndex(index, length);
// bound check for this ArrowBuf where the data will be copied into
Preconditions.checkArgument(dst != null, "expecting a valid ArrowBuf");
// bound check for dst ArrowBuf
if (isOutOfBounds(dstIndex, length, dst.capacity())) {
throw new IndexOutOfBoundsException(String.format("index: %d, length: %d (expected: range(0, %d))",
dstIndex, length, dst.capacity()));
}
if (length != 0) {
// copy length bytes of data from this ArrowBuf starting at
// address srcAddress into dst ArrowBuf starting at address
// dstAddress
final long srcAddress = addr(index);
final long dstAddress = dst.memoryAddress() + (long)dstIndex;
PlatformDependent.copyMemory(srcAddress, dstAddress, (long)length);
}
}
/**
* Copy data from src ArrowBuf starting at index srcIndex into this
* ArrowBuf at given index.
* @param index index index (0 based relative to the portion of memory
* this ArrowBuf has access to)
* @param src src ArrowBuf where the data will be copied from
* @param srcIndex starting index in the src ArrowBuf where the copy
* will begin from
* @param length length of data to copy from src ArrowBuf
*/
public void setBytes(int index, ArrowBuf src, int srcIndex, int length) {
// bound check for this ArrowBuf where the data will be copied into
checkIndex(index, length);
// null check
Preconditions.checkArgument(src != null, "expecting a valid ArrowBuf");
// bound check for src ArrowBuf
if (isOutOfBounds(srcIndex, length, src.capacity())) {
throw new IndexOutOfBoundsException(String.format("index: %d, length: %d (expected: range(0, %d))",
index, length, src.capacity()));
}
if (length != 0) {
// copy length bytes of data from src ArrowBuf starting at
// address srcAddress into this ArrowBuf starting at address
// dstAddress
final long srcAddress = src.memoryAddress() + (long)srcIndex;
final long dstAddress = addr(index);
PlatformDependent.copyMemory(srcAddress, dstAddress, (long)length);
}
}
/**
* Copy readableBytes() number of bytes from src ArrowBuf
* starting from its readerIndex into this ArrowBuf starting
* at the given index.
* @param index index index (0 based relative to the portion of memory
* this ArrowBuf has access to)
* @param src src ArrowBuf where the data will be copied from
*/
public void setBytes(int index, ArrowBuf src) {
// null check
Preconditions.checkArgument(src != null, "expecting valid ArrowBuf");
final int length = src.readableBytes();
// bound check for this ArrowBuf where the data will be copied into
checkIndex(index, length);
final long srcAddress = src.memoryAddress() + (long)src.readerIndex;
final long dstAddress = addr(index);
PlatformDependent.copyMemory(srcAddress, dstAddress, (long)length);
src.readerIndex(src.readerIndex + length);
}
/**
* Copy a certain length of bytes from given InputStream
* into this ArrowBuf at the provided index.
* @param index index index (0 based relative to the portion of memory
* this ArrowBuf has access to)
* @param in src stream to copy from
* @param length length of data to copy
* @return number of bytes copied from stream into ArrowBuf
* @throws IOException on failing to read from stream