-
Notifications
You must be signed in to change notification settings - Fork 668
/
ByteBuffer.java
1818 lines (1681 loc) · 74.8 KB
/
ByteBuffer.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
/*
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.nio;
/**
* A byte buffer.
*
* <p> This class defines six categories of operations upon
* byte buffers:
*
* <ul>
*
* <li><p> Absolute and relative {@link #get() <i>get</i>} and
* {@link #put(byte) <i>put</i>} methods that read and write
* single bytes; </p></li>
*
* <li><p> Relative {@link #get(byte[]) <i>bulk get</i>}
* methods that transfer contiguous sequences of bytes from this buffer
* into an array; </p></li>
*
* <li><p> Relative {@link #put(byte[]) <i>bulk put</i>}
* methods that transfer contiguous sequences of bytes from a
* byte array or some other byte
* buffer into this buffer; </p></li>
*
*
*
* <li><p> Absolute and relative {@link #getChar() <i>get</i>}
* and {@link #putChar(char) <i>put</i>} methods that read and
* write values of other primitive types, translating them to and from
* sequences of bytes in a particular byte order; </p></li>
*
* <li><p> Methods for creating <i><a href="#views">view buffers</a></i>,
* which allow a byte buffer to be viewed as a buffer containing values of
* some other primitive type; and </p></li>
*
*
*
* <li><p> A method for {@link #compact compacting}
* a byte buffer. </p></li>
*
* </ul>
*
* <p> Byte buffers can be created either by {@link #allocate
* <i>allocation</i>}, which allocates space for the buffer's
*
*
*
* content, or by {@link #wrap(byte[]) <i>wrapping</i>} an
* existing byte array into a buffer.
*
*
*
*
*
*
*
*
*
*
*
* <a id="direct"></a>
* <h2> Direct <i>vs.</i> non-direct buffers </h2>
*
* <p> A byte buffer is either <i>direct</i> or <i>non-direct</i>. Given a
* direct byte buffer, the Java virtual machine will make a best effort to
* perform native I/O operations directly upon it. That is, it will attempt to
* avoid copying the buffer's content to (or from) an intermediate buffer
* before (or after) each invocation of one of the underlying operating
* system's native I/O operations.
*
* <p> A direct byte buffer may be created by invoking the {@link
* #allocateDirect(int) allocateDirect} factory method of this class. The
* buffers returned by this method typically have somewhat higher allocation
* and deallocation costs than non-direct buffers. The contents of direct
* buffers may reside outside of the normal garbage-collected heap, and so
* their impact upon the memory footprint of an application might not be
* obvious. It is therefore recommended that direct buffers be allocated
* primarily for large, long-lived buffers that are subject to the underlying
* system's native I/O operations. In general it is best to allocate direct
* buffers only when they yield a measureable gain in program performance.
*
* <p> A direct byte buffer may also be created by {@link
* java.nio.channels.FileChannel#map mapping} a region of a file
* directly into memory. An implementation of the Java platform may optionally
* support the creation of direct byte buffers from native code via JNI. If an
* instance of one of these kinds of buffers refers to an inaccessible region
* of memory then an attempt to access that region will not change the buffer's
* content and will cause an unspecified exception to be thrown either at the
* time of the access or at some later time.
*
* <p> Whether a byte buffer is direct or non-direct may be determined by
* invoking its {@link #isDirect isDirect} method. This method is provided so
* that explicit buffer management can be done in performance-critical code.
*
*
* <a id="bin"></a>
* <h2> Access to binary data </h2>
*
* <p> This class defines methods for reading and writing values of all other
* primitive types, except {@code boolean}. Primitive values are translated
* to (or from) sequences of bytes according to the buffer's current byte
* order, which may be retrieved and modified via the {@link #order order}
* methods. Specific byte orders are represented by instances of the {@link
* ByteOrder} class. The initial order of a byte buffer is always {@link
* ByteOrder#BIG_ENDIAN BIG_ENDIAN}.
*
* <p> For access to heterogeneous binary data, that is, sequences of values of
* different types, this class defines a family of absolute and relative
* <i>get</i> and <i>put</i> methods for each type. For 32-bit floating-point
* values, for example, this class defines:
*
* <blockquote><pre>
* float {@link #getFloat()}
* float {@link #getFloat(int) getFloat(int index)}
* void {@link #putFloat(float) putFloat(float f)}
* void {@link #putFloat(int, float) putFloat(int index, float f)}</pre></blockquote>
*
* <p> Corresponding methods are defined for the types {@code char,
* short, int, long}, and {@code double}. The index
* parameters of the absolute <i>get</i> and <i>put</i> methods are in terms of
* bytes rather than of the type being read or written.
*
* <a id="views"></a>
*
* <p> For access to homogeneous binary data, that is, sequences of values of
* the same type, this class defines methods that can create <i>views</i> of a
* given byte buffer. A <i>view buffer</i> is simply another buffer whose
* content is backed by the byte buffer. Changes to the byte buffer's content
* will be visible in the view buffer, and vice versa; the two buffers'
* position, limit, and mark values are independent. The {@link
* #asFloatBuffer() asFloatBuffer} method, for example, creates an instance of
* the {@link FloatBuffer} class that is backed by the byte buffer upon which
* the method is invoked. Corresponding view-creation methods are defined for
* the types {@code char, short, int, long}, and {@code double}.
*
* <p> View buffers have three important advantages over the families of
* type-specific <i>get</i> and <i>put</i> methods described above:
*
* <ul>
*
* <li><p> A view buffer is indexed not in terms of bytes but rather in terms
* of the type-specific size of its values; </p></li>
*
* <li><p> A view buffer provides relative bulk <i>get</i> and <i>put</i>
* methods that can transfer contiguous sequences of values between a buffer
* and an array or some other buffer of the same type; and </p></li>
*
* <li><p> A view buffer is potentially much more efficient because it will
* be direct if, and only if, its backing byte buffer is direct. </p></li>
*
* </ul>
*
* <p> The byte order of a view buffer is fixed to be that of its byte buffer
* at the time that the view is created. </p>
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* <h2> Invocation chaining </h2>
*
*
* <p> Methods in this class that do not otherwise have a value to return are
* specified to return the buffer upon which they are invoked. This allows
* method invocations to be chained.
*
*
*
* The sequence of statements
*
* <blockquote><pre>
* bb.putInt(0xCAFEBABE);
* bb.putShort(3);
* bb.putShort(45);</pre></blockquote>
*
* can, for example, be replaced by the single statement
*
* <blockquote><pre>
* bb.putInt(0xCAFEBABE).putShort(3).putShort(45);</pre></blockquote>
*
* @author Mark Reinhold
* @author JSR-51 Expert Group
* @since 1.4
*/
/*
* 字节缓冲区
*
* ByteBuffer
* ┌─────────┴────────┐
* HeapByteBuffer MappedByteBuffer DirectBuffer
* | ├──────────────────┘
* HeapByteBufferR DirectByteBuffer
* |
* DirectByteBufferR
*/
public abstract class ByteBuffer extends Buffer implements Comparable<ByteBuffer> {
/**
* These fields are declared here rather than in Heap-X-Buffer in order to
* reduce the number of virtual method invocations needed to access these
* values, which is especially costly when coding small buffers.
*/
final byte[] hb; // Non-null only for heap buffers
final int offset; // 寻址偏移量,用于ByteBuffer/HeapByteBuffer/DirectByteBuffer这三组实现
boolean isReadOnly; // 该缓冲区是否只读
// 缓冲区字节是否为大端法存储
boolean bigEndian = true;
// 缓冲区字节序与本地字节序是否一致
boolean nativeByteOrder = (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN);
/*▼ 构造器 ████████████████████████████████████████████████████████████████████████████████┓ */
/* Creates a new buffer with the given mark, position, limit, capacity, backing array, and array offset */
ByteBuffer(int mark, int pos, int lim, int cap, byte[] hb, int offset) {
super(mark, pos, lim, cap);
this.hb = hb;
this.offset = offset;
}
/* Creates a new buffer with the given mark, position, limit, and capacity */
ByteBuffer(int mark, int pos, int lim, int cap) { // package-private
this(mark, pos, lim, cap, null, 0);
}
/*▲ 构造器 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 工厂方法 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Allocates a new direct byte buffer.
*
* <p> The new buffer's position will be zero, its limit will be its
* capacity, its mark will be undefined, each of its elements will be
* initialized to zero, and its byte order will be
* {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}. Whether or not it has a
* {@link #hasArray backing array} is unspecified.
*
* @param capacity The new buffer's capacity, in bytes
*
* @return The new byte buffer
*
* @throws IllegalArgumentException If the {@code capacity} is a negative integer
*/
// 创建直接内存缓冲区DirectByteBuffer
public static ByteBuffer allocateDirect(int capacity) {
return new DirectByteBuffer(capacity);
}
/**
* Allocates a new byte buffer.
*
* <p> The new buffer's position will be zero, its limit will be its
* capacity, its mark will be undefined, each of its elements will be
* initialized to zero, and its byte order will be
*
* {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}.
*
*
*
*
* It will have a {@link #array backing array}, and its
* {@link #arrayOffset array offset} will be zero.
*
* @param capacity The new buffer's capacity, in bytes
*
* @return The new byte buffer
*
* @throws IllegalArgumentException If the {@code capacity} is a negative integer
*/
// 创建堆内存缓冲区HeapByteBuffer
public static ByteBuffer allocate(int capacity) {
if(capacity<0)
throw createCapacityException(capacity);
return new HeapByteBuffer(capacity, capacity);
}
/**
* Wraps a byte array into a buffer.
*
* <p> The new buffer will be backed by the given byte array;
* that is, modifications to the buffer will cause the array to be modified
* and vice versa. The new buffer's capacity will be
* {@code array.length}, its position will be {@code offset}, its limit
* will be {@code offset + length}, its mark will be undefined, and its
* byte order will be
*
* {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}.
*
*
*
*
* Its {@link #array backing array} will be the given array, and
* its {@link #arrayOffset array offset} will be zero. </p>
*
* @param array The array that will back the new buffer
* @param offset The offset of the subarray to be used; must be non-negative and
* no larger than {@code array.length}. The new buffer's position
* will be set to this value.
* @param length The length of the subarray to be used;
* must be non-negative and no larger than
* {@code array.length - offset}.
* The new buffer's limit will be set to {@code offset + length}.
*
* @return The new byte buffer
*
* @throws IndexOutOfBoundsException If the preconditions on the {@code offset} and {@code length}
* parameters do not hold
*/
// 包装一个字节数组到buffer(包装一部分)
public static ByteBuffer wrap(byte[] array, int offset, int length) {
try {
return new HeapByteBuffer(array, offset, length);
} catch(IllegalArgumentException x) {
throw new IndexOutOfBoundsException();
}
}
/**
* Wraps a byte array into a buffer.
*
* <p> The new buffer will be backed by the given byte array;
* that is, modifications to the buffer will cause the array to be modified
* and vice versa. The new buffer's capacity and limit will be
* {@code array.length}, its position will be zero, its mark will be
* undefined, and its byte order will be
*
* {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}.
*
*
* Its {@link #array backing array} will be the given array, and its
* {@link #arrayOffset array offset} will be zero. </p>
*
* @param array The array that will back this buffer
*
* @return The new byte buffer
*/
// 包装一个字节数组到buffer
public static ByteBuffer wrap(byte[] array) {
return wrap(array, 0, array.length);
}
/*▲ 工厂方法 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 缓冲区属性 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Tells whether or not this byte buffer is direct.
*
* @return {@code true} if, and only if, this buffer is direct
*/
// 直接缓冲区/非直接缓冲区
public abstract boolean isDirect();
/*▲ 缓冲区属性 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 标记操作 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* {@inheritDoc}
*/
// 在当前游标position处设置新的mark(备忘)
@Override
public ByteBuffer mark() {
super.mark();
return this;
}
/**
* {@inheritDoc}
*/
// 设置新的游标position
@Override
public ByteBuffer position(int newPosition) {
super.position(newPosition);
return this;
}
/**
* {@inheritDoc}
*/
// 设置新的上界limit
@Override
public ByteBuffer limit(int newLimit) {
super.limit(newLimit);
return this;
}
/**
* {@inheritDoc}
*/
// 将当前游标position回退到mark(备忘)位置
@Override
public ByteBuffer reset() {
super.reset();
return this;
}
/**
* {@inheritDoc}
*/
// 清理缓冲区,重置标记
@Override
public ByteBuffer clear() {
super.clear();
return this;
}
/**
* {@inheritDoc}
*/
// 修改标记,可以切换缓冲区读/写模式
@Override
public ByteBuffer flip() {
super.flip();
return this;
}
/**
* {@inheritDoc}
*/
// 丢弃备忘,游标归零
@Override
public ByteBuffer rewind() {
super.rewind();
return this;
}
/*▲ 标记操作 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 创建新缓冲区,新旧缓冲区共享内部的存储容器 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Creates a new byte buffer whose content is a shared subsequence of
* this buffer's content.
*
* <p> The content of the new buffer will start at this buffer's current
* position. Changes to this buffer's content will be visible in the new
* buffer, and vice versa; the two buffers' position, limit, and mark
* values will be independent.
*
* <p> The new buffer's position will be zero, its capacity and its limit
* will be the number of bytes remaining in this buffer, its mark will be
* undefined, and its byte order will be
*
* {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}.
*
* The new buffer will be direct if, and only if, this buffer is direct, and
* it will be read-only if, and only if, this buffer is read-only. </p>
*
* @return The new byte buffer
*
* @see #alignedSlice(int)
*/
// 切片,截取旧缓冲区的【活跃区域】,作为新缓冲区的【原始区域】。两个缓冲区标记独立
@Override
public abstract ByteBuffer slice();
// 切片,截取旧缓冲区【活跃区域】中pos~lim中的一段,作为新缓冲区的【原始区域】。两个缓冲区标记独立
abstract ByteBuffer slice(int pos, int lim);
/**
* Creates a new byte buffer whose content is a shared and aligned
* subsequence of this buffer's content.
*
* <p> The content of the new buffer will start at this buffer's current
* position rounded up to the index of the nearest aligned byte for the
* given unit size, and end at this buffer's limit rounded down to the index
* of the nearest aligned byte for the given unit size.
* If rounding results in out-of-bound values then the new buffer's capacity
* and limit will be zero. If rounding is within bounds the following
* expressions will be true for a new buffer {@code nb} and unit size
* {@code unitSize}:
* <pre>{@code
* nb.alignmentOffset(0, unitSize) == 0
* nb.alignmentOffset(nb.limit(), unitSize) == 0
* }</pre>
*
* <p> Changes to this buffer's content will be visible in the new
* buffer, and vice versa; the two buffers' position, limit, and mark
* values will be independent.
*
* <p> The new buffer's position will be zero, its capacity and its limit
* will be the number of bytes remaining in this buffer or fewer subject to
* alignment, its mark will be undefined, and its byte order will be
* {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}.
*
* The new buffer will be direct if, and only if, this buffer is direct, and
* it will be read-only if, and only if, this buffer is read-only. </p>
*
* @param unitSize The unit size in bytes, must be a power of {@code 2}
*
* @return The new byte buffer
*
* @throws IllegalArgumentException If the unit size not a power of {@code 2}
* @throws UnsupportedOperationException If the native platform does not guarantee stable aligned slices
* for the given unit size when managing the memory regions
* of buffers of the same kind as this buffer (direct or
* non-direct). For example, if garbage collection would result
* in the moving of a memory region covered by a non-direct buffer
* from one location to another and both locations have different
* alignment characteristics.
* @apiNote This method may be utilized to create a new buffer where unit size bytes
* from index, that is a multiple of the unit size, may be accessed
* atomically, if supported by the native platform.
* @implNote This implementation throws {@code UnsupportedOperationException} for
* non-direct buffers when the given unit size is greater then {@code 8}.
* @see #alignmentOffset(int, int)
* @see #slice()
* @since 9
*/
/*
* 切片
*
* 创建一个新的共享缓存区,该缓存区是当前缓冲区的一个子序列
* 切片之前,需要为新缓冲区先计算一个新的游标和新的上界(进行字节对齐校准)
* 一般为保证切片后,新缓冲区仍然满足容量要求,所以往往会先给原缓冲区多分配unitSize-1个字节的空间
*/
public final ByteBuffer alignedSlice(int unitSize) {
int pos = position(); // 游标
int lim = limit(); // 上界
// 计算出未对齐的部分
int pos_mod = alignmentOffset(pos, unitSize);
int lim_mod = alignmentOffset(lim, unitSize);
// 将pos向上对齐
int aligned_pos = (pos_mod > 0) ? pos + (unitSize - pos_mod) : pos;
// 将lim向下对齐
int aligned_lim = lim - lim_mod;
if(aligned_pos > lim || aligned_lim < pos) {
aligned_pos = aligned_lim = pos;
}
// 校正pos和limit之后再切片
return slice(aligned_pos, aligned_lim);
}
/**
* Creates a new byte buffer that shares this buffer's content.
*
* <p> The content of the new buffer will be that of this buffer. Changes
* to this buffer's content will be visible in the new buffer, and vice
* versa; the two buffers' position, limit, and mark values will be
* independent.
*
* <p> The new buffer's capacity, limit, position,
*
* and mark values will be identical to those of this buffer, and its byte
* order will be {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}.
*
* The new buffer will be direct if, and only if, this buffer is direct, and
* it will be read-only if, and only if, this buffer is read-only. </p>
*
* @return The new byte buffer
*/
// 副本,新缓冲区共享旧缓冲区的【原始区域】,且新旧缓冲区【活跃区域】一致。两个缓冲区标记独立。
@Override
public abstract ByteBuffer duplicate();
/**
* Creates a new, read-only byte buffer that shares this buffer's
* content.
*
* <p> The content of the new buffer will be that of this buffer. Changes
* to this buffer's content will be visible in the new buffer; the new
* buffer itself, however, will be read-only and will not allow the shared
* content to be modified. The two buffers' position, limit, and mark
* values will be independent.
*
* <p> The new buffer's capacity, limit, position,
*
* and mark values will be identical to those of this buffer, and its byte
* order will be {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}.
*
* <p> If this buffer is itself read-only then this method behaves in
* exactly the same way as the {@link #duplicate duplicate} method. </p>
*
* @return The new, read-only byte buffer
*/
// 只读副本,新缓冲区共享旧缓冲区的【原始区域】,且新旧缓冲区【活跃区域】一致。两个缓冲区标记独立。
public abstract ByteBuffer asReadOnlyBuffer();
/*▲ 创建新缓冲区,新旧缓冲区共享内部的存储容器 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ get ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Relative <i>get</i> method. Reads the byte at this buffer's
* current position, and then increments the position.
*
* @return The byte at the buffer's current position
*
* @throws BufferUnderflowException If the buffer's current position is not smaller than its limit
*/
// 读取position处(可能需要加offset)的byte,然后递增position。
public abstract byte get();
/**
* Absolute <i>get</i> method. Reads the byte at the given
* index.
*
* @param index The index from which the byte will be read
*
* @return The byte at the given index
*
* @throws IndexOutOfBoundsException If {@code index} is negative
* or not smaller than the buffer's limit
*/
// 读取i处(可能需要加offset)的byte(有越界检查)
public abstract byte get(int i);
/**
* Relative bulk <i>get</i> method.
*
* <p> This method transfers bytes from this buffer into the given
* destination array. If there are fewer bytes remaining in the
* buffer than are required to satisfy the request, that is, if
* {@code length} {@code >} {@code remaining()}, then no
* bytes are transferred and a {@link BufferUnderflowException} is
* thrown.
*
* <p> Otherwise, this method copies {@code length} bytes from this
* buffer into the given array, starting at the current position of this
* buffer and at the given offset in the array. The position of this
* buffer is then incremented by {@code length}.
*
* <p> In other words, an invocation of this method of the form
* <code>src.get(dst, off, len)</code> has exactly the same effect as
* the loop
*
* <pre>{@code
* for (int i = off; i < off + len; i++)
* dst[i] = src.get();
* }</pre>
*
* except that it first checks that there are sufficient bytes in
* this buffer and it is potentially much more efficient.
*
* @param dst The array into which bytes are to be written
* @param offset The offset within the array of the first byte to be
* written; must be non-negative and no larger than
* {@code dst.length}
* @param length The maximum number of bytes to be written to the given
* array; must be non-negative and no larger than
* {@code dst.length - offset}
*
* @return This buffer
*
* @throws BufferUnderflowException If there are fewer than {@code length} bytes
* remaining in this buffer
* @throws IndexOutOfBoundsException If the preconditions on the {@code offset} and {@code length}
* parameters do not hold
*/
// 复制源缓存区的length个元素到dst数组offset索引处
public ByteBuffer get(byte[] dst, int offset, int length) {
checkBounds(offset, length, dst.length);
if(length > remaining())
throw new BufferUnderflowException();
int end = offset + length;
for(int i = offset; i < end; i++)
dst[i] = get();
return this;
}
/**
* Relative bulk <i>get</i> method.
*
* <p> This method transfers bytes from this buffer into the given
* destination array. An invocation of this method of the form
* {@code src.get(a)} behaves in exactly the same way as the invocation
*
* <pre>
* src.get(a, 0, a.length) </pre>
*
* @param dst The destination array
*
* @return This buffer
*
* @throws BufferUnderflowException If there are fewer than {@code length} bytes
* remaining in this buffer
*/
// 复制源缓存区的内容到dst数组,尽量填满dst
public ByteBuffer get(byte[] dst) {
return get(dst, 0, dst.length);
}
/**
* Relative <i>get</i> method for reading a char value.
*
* <p> Reads the next two bytes at this buffer's current position,
* composing them into a char value according to the current byte order,
* and then increments the position by two. </p>
*
* @return The char value at the buffer's current position
*
* @throws BufferUnderflowException If there are fewer than two bytes
* remaining in this buffer
*/
// 一次读2个字节,按char解析,将position增加2个单位
public abstract char getChar();
/**
* Absolute <i>get</i> method for reading a char value.
*
* <p> Reads two bytes at the given index, composing them into a
* char value according to the current byte order. </p>
*
* @param index The index from which the bytes will be read
*
* @return The char value at the given index
*
* @throws IndexOutOfBoundsException If {@code index} is negative
* or not smaller than the buffer's limit,
* minus one
*/
// 读取i处2个字节解析为char(有越界检查)
public abstract char getChar(int i);
/**
* Relative <i>get</i> method for reading a short value.
*
* <p> Reads the next two bytes at this buffer's current position,
* composing them into a short value according to the current byte order,
* and then increments the position by two. </p>
*
* @return The short value at the buffer's current position
*
* @throws BufferUnderflowException If there are fewer than two bytes
* remaining in this buffer
*/
// 一次读2个字节,按short解析,将position增加2个单位
public abstract short getShort();
/**
* Absolute <i>get</i> method for reading a short value.
*
* <p> Reads two bytes at the given index, composing them into a
* short value according to the current byte order. </p>
*
* @param index The index from which the bytes will be read
*
* @return The short value at the given index
*
* @throws IndexOutOfBoundsException If {@code index} is negative
* or not smaller than the buffer's limit,
* minus one
*/
// 读取i处2个字节解析为short(有越界检查)
public abstract short getShort(int i);
/**
* Relative <i>get</i> method for reading an int value.
*
* <p> Reads the next four bytes at this buffer's current position,
* composing them into an int value according to the current byte order,
* and then increments the position by four. </p>
*
* @return The int value at the buffer's current position
*
* @throws BufferUnderflowException If there are fewer than four bytes
* remaining in this buffer
*/
// 一次读4个字节,按int解析,将position增加4个单位
public abstract int getInt();
/**
* Absolute <i>get</i> method for reading an int value.
*
* <p> Reads four bytes at the given index, composing them into a
* int value according to the current byte order. </p>
*
* @param index The index from which the bytes will be read
*
* @return The int value at the given index
*
* @throws IndexOutOfBoundsException If {@code index} is negative
* or not smaller than the buffer's limit,
* minus three
*/
// 读取i处4个字节解析为int(有越界检查)
public abstract int getInt(int i);
/**
* Relative <i>get</i> method for reading a long value.
*
* <p> Reads the next eight bytes at this buffer's current position,
* composing them into a long value according to the current byte order,
* and then increments the position by eight. </p>
*
* @return The long value at the buffer's current position
*
* @throws BufferUnderflowException If there are fewer than eight bytes
* remaining in this buffer
*/
// 一次读8个字节,按long解析,将position增加8个单位
public abstract long getLong();
/**
* Absolute <i>get</i> method for reading a long value.
*
* <p> Reads eight bytes at the given index, composing them into a
* long value according to the current byte order. </p>
*
* @param index The index from which the bytes will be read
*
* @return The long value at the given index
*
* @throws IndexOutOfBoundsException If {@code index} is negative
* or not smaller than the buffer's limit,
* minus seven
*/
// 读取i处8个字节解析为long(有越界检查)
public abstract long getLong(int i);
/**
* Relative <i>get</i> method for reading a float value.
*
* <p> Reads the next four bytes at this buffer's current position,
* composing them into a float value according to the current byte order,
* and then increments the position by four. </p>
*
* @return The float value at the buffer's current position
*
* @throws BufferUnderflowException If there are fewer than four bytes
* remaining in this buffer
*/
// 一次读4个字节,按float解析,将position增加4个单位
public abstract float getFloat();
/**
* Absolute <i>get</i> method for reading a float value.
*
* <p> Reads four bytes at the given index, composing them into a
* float value according to the current byte order. </p>
*
* @param index The index from which the bytes will be read
*
* @return The float value at the given index
*
* @throws IndexOutOfBoundsException If {@code index} is negative
* or not smaller than the buffer's limit,
* minus three
*/
// 读取i处4个字节解析为float(有越界检查)
public abstract float getFloat(int i);
/**
* Relative <i>get</i> method for reading a double value.
*
* <p> Reads the next eight bytes at this buffer's current position,
* composing them into a double value according to the current byte order,
* and then increments the position by eight. </p>
*
* @return The double value at the buffer's current position
*
* @throws BufferUnderflowException If there are fewer than eight bytes
* remaining in this buffer
*/
// 一次读8个字节,按double解析,将position增加8个单位
public abstract double getDouble();
/**
* Absolute <i>get</i> method for reading a double value.
*
* <p> Reads eight bytes at the given index, composing them into a
* double value according to the current byte order. </p>
*
* @param index The index from which the bytes will be read
*
* @return The double value at the given index
*
* @throws IndexOutOfBoundsException If {@code index} is negative
* or not smaller than the buffer's limit,
* minus seven
*/
// 读取i处8个字节解析为double(有越界检查)
public abstract double getDouble(int i);
/*▲ get ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ put ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Relative <i>put</i> method <i>(optional operation)</i>.
*
* <p> Writes the given byte into this buffer at the current
* position, and then increments the position. </p>
*
* @param b The byte to be written
*
* @return This buffer
*
* @throws BufferOverflowException If this buffer's current position is not smaller than its limit
* @throws ReadOnlyBufferException If this buffer is read-only
*/
// 向position处(可能需要加offset)写入byte,并将position递增
public abstract ByteBuffer put(byte b);
/**
* Absolute <i>put</i> method <i>(optional operation)</i>.
*
* <p> Writes the given byte into this buffer at the given
* index. </p>
*
* @param index The index at which the byte will be written
* @param b The byte value to be written
*
* @return This buffer
*
* @throws IndexOutOfBoundsException If {@code index} is negative
* or not smaller than the buffer's limit
* @throws ReadOnlyBufferException If this buffer is read-only
*/
// 向i处(可能需要加offset)写入byte
public abstract ByteBuffer put(int i, byte b);
/**
* Relative bulk <i>put</i> method <i>(optional operation)</i>.
*
* <p> This method transfers bytes into this buffer from the given
* source array. If there are more bytes to be copied from the array
* than remain in this buffer, that is, if
* {@code length} {@code >} {@code remaining()}, then no
* bytes are transferred and a {@link BufferOverflowException} is
* thrown.
*
* <p> Otherwise, this method copies {@code length} bytes from the
* given array into this buffer, starting at the given offset in the array
* and at the current position of this buffer. The position of this buffer
* is then incremented by {@code length}.
*
* <p> In other words, an invocation of this method of the form
* <code>dst.put(src, off, len)</code> has exactly the same effect as
* the loop
*
* <pre>{@code
* for (int i = off; i < off + len; i++)
* dst.put(a[i]);