1
25
26
27
28 package java.nio;
29
30
40
41 class HeapByteBuffer
42 extends ByteBuffer
43 {
44
45 private static final long ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(byte[].class);
46
47
48 private static final long ARRAY_INDEX_SCALE = UNSAFE.arrayIndexScale(byte[].class);
49
50
51
52
58
59 HeapByteBuffer(int cap, int lim) {
60
61 super(-1, 0, lim, cap, new byte[cap], 0);
62
66 this.address = ARRAY_BASE_OFFSET;
67
68
69
70
71 }
72
73 HeapByteBuffer(byte[] buf, int off, int len) {
74
75 super(-1, off, off + len, buf.length, buf, 0);
76
80 this.address = ARRAY_BASE_OFFSET;
81
82
83
84
85 }
86
87 protected HeapByteBuffer(byte[] buf,
88 int mark, int pos, int lim, int cap,
89 int off)
90 {
91
92 super(mark, pos, lim, cap, buf, off);
93
97 this.address = ARRAY_BASE_OFFSET + off * ARRAY_INDEX_SCALE;
98
99
100
101
102 }
103
104 public ByteBuffer slice() {
105 return new HeapByteBuffer(hb,
106 -1,
107 0,
108 this.remaining(),
109 this.remaining(),
110 this.position() + offset);
111 }
112
113
114 ByteBuffer slice(int pos, int lim) {
115 assert (pos >= 0);
116 assert (pos <= lim);
117 int rem = lim - pos;
118 return new HeapByteBuffer(hb,
119 -1,
120 0,
121 rem,
122 rem,
123 pos + offset);
124 }
125
126
127 public ByteBuffer duplicate() {
128 return new HeapByteBuffer(hb,
129 this.markValue(),
130 this.position(),
131 this.limit(),
132 this.capacity(),
133 offset);
134 }
135
136 public ByteBuffer asReadOnlyBuffer() {
137
138 return new HeapByteBufferR(hb,
139 this.markValue(),
140 this.position(),
141 this.limit(),
142 this.capacity(),
143 offset);
144
145
146
147 }
148
149
150
151 protected int ix(int i) {
152 return i + offset;
153 }
154
155
156 private long byteOffset(long i) {
157 return address + i;
158 }
159
160
161 public byte get() {
162 return hb[ix(nextGetIndex())];
163 }
164
165 public byte get(int i) {
166 return hb[ix(checkIndex(i))];
167 }
168
169
170
171
172
173
174
175 public ByteBuffer get(byte[] dst, int offset, int length) {
176 checkBounds(offset, length, dst.length);
177 if (length > remaining())
178 throw new BufferUnderflowException();
179 System.arraycopy(hb, ix(position()), dst, offset, length);
180 position(position() + length);
181 return this;
182 }
183
184 public boolean isDirect() {
185 return false;
186 }
187
188
189
190 public boolean isReadOnly() {
191 return false;
192 }
193
194 public ByteBuffer put(byte x) {
195
196 hb[ix(nextPutIndex())] = x;
197 return this;
198
199
200
201 }
202
203 public ByteBuffer put(int i, byte x) {
204
205 hb[ix(checkIndex(i))] = x;
206 return this;
207
208
209
210 }
211
212 public ByteBuffer put(byte[] src, int offset, int length) {
213
214 checkBounds(offset, length, src.length);
215 if (length > remaining())
216 throw new BufferOverflowException();
217 System.arraycopy(src, offset, hb, ix(position()), length);
218 position(position() + length);
219 return this;
220
221
222
223 }
224
225 public ByteBuffer put(ByteBuffer src) {
226
227 if (src instanceof HeapByteBuffer) {
228 if (src == this)
229 throw createSameBufferException();
230 HeapByteBuffer sb = (HeapByteBuffer)src;
231 int n = sb.remaining();
232 if (n > remaining())
233 throw new BufferOverflowException();
234 System.arraycopy(sb.hb, sb.ix(sb.position()),
235 hb, ix(position()), n);
236 sb.position(sb.position() + n);
237 position(position() + n);
238 } else if (src.isDirect()) {
239 int n = src.remaining();
240 if (n > remaining())
241 throw new BufferOverflowException();
242 src.get(hb, ix(position()), n);
243 position(position() + n);
244 } else {
245 super.put(src);
246 }
247 return this;
248
249
250
251 }
252
253 public ByteBuffer compact() {
254
255 System.arraycopy(hb, ix(position()), hb, ix(0), remaining());
256 position(remaining());
257 limit(capacity());
258 discardMark();
259 return this;
260
261
262
263 }
264
265
266
267
268
269 byte _get(int i) {
270 return hb[i];
271 }
272
273 void _put(int i, byte b) {
274
275 hb[i] = b;
276
277
278
279 }
280
281
282
283
284
285 public char getChar() {
286 return UNSAFE.getCharUnaligned(hb, byteOffset(nextGetIndex(2)), bigEndian);
287 }
288
289 public char getChar(int i) {
290 return UNSAFE.getCharUnaligned(hb, byteOffset(checkIndex(i, 2)), bigEndian);
291 }
292
293
294
295 public ByteBuffer putChar(char x) {
296
297 UNSAFE.putCharUnaligned(hb, byteOffset(nextPutIndex(2)), x, bigEndian);
298 return this;
299
300
301
302 }
303
304 public ByteBuffer putChar(int i, char x) {
305
306 UNSAFE.putCharUnaligned(hb, byteOffset(checkIndex(i, 2)), x, bigEndian);
307 return this;
308
309
310
311 }
312
313 public CharBuffer asCharBuffer() {
314 int size = this.remaining() >> 1;
315 long addr = address + position();
316 return (bigEndian
317 ? (CharBuffer)(new ByteBufferAsCharBufferB(this,
318 -1,
319 0,
320 size,
321 size,
322 addr))
323 : (CharBuffer)(new ByteBufferAsCharBufferL(this,
324 -1,
325 0,
326 size,
327 size,
328 addr)));
329 }
330
331
332
333
334
335
336 public short getShort() {
337 return UNSAFE.getShortUnaligned(hb, byteOffset(nextGetIndex(2)), bigEndian);
338 }
339
340 public short getShort(int i) {
341 return UNSAFE.getShortUnaligned(hb, byteOffset(checkIndex(i, 2)), bigEndian);
342 }
343
344
345
346 public ByteBuffer putShort(short x) {
347
348 UNSAFE.putShortUnaligned(hb, byteOffset(nextPutIndex(2)), x, bigEndian);
349 return this;
350
351
352
353 }
354
355 public ByteBuffer putShort(int i, short x) {
356
357 UNSAFE.putShortUnaligned(hb, byteOffset(checkIndex(i, 2)), x, bigEndian);
358 return this;
359
360
361
362 }
363
364 public ShortBuffer asShortBuffer() {
365 int size = this.remaining() >> 1;
366 long addr = address + position();
367 return (bigEndian
368 ? (ShortBuffer)(new ByteBufferAsShortBufferB(this,
369 -1,
370 0,
371 size,
372 size,
373 addr))
374 : (ShortBuffer)(new ByteBufferAsShortBufferL(this,
375 -1,
376 0,
377 size,
378 size,
379 addr)));
380 }
381
382
383
384
385
386
387 public int getInt() {
388 return UNSAFE.getIntUnaligned(hb, byteOffset(nextGetIndex(4)), bigEndian);
389 }
390
391 public int getInt(int i) {
392 return UNSAFE.getIntUnaligned(hb, byteOffset(checkIndex(i, 4)), bigEndian);
393 }
394
395
396
397 public ByteBuffer putInt(int x) {
398
399 UNSAFE.putIntUnaligned(hb, byteOffset(nextPutIndex(4)), x, bigEndian);
400 return this;
401
402
403
404 }
405
406 public ByteBuffer putInt(int i, int x) {
407
408 UNSAFE.putIntUnaligned(hb, byteOffset(checkIndex(i, 4)), x, bigEndian);
409 return this;
410
411
412
413 }
414
415 public IntBuffer asIntBuffer() {
416 int size = this.remaining() >> 2;
417 long addr = address + position();
418 return (bigEndian
419 ? (IntBuffer)(new ByteBufferAsIntBufferB(this,
420 -1,
421 0,
422 size,
423 size,
424 addr))
425 : (IntBuffer)(new ByteBufferAsIntBufferL(this,
426 -1,
427 0,
428 size,
429 size,
430 addr)));
431 }
432
433
434
435
436
437
438 public long getLong() {
439 return UNSAFE.getLongUnaligned(hb, byteOffset(nextGetIndex(8)), bigEndian);
440 }
441
442 public long getLong(int i) {
443 return UNSAFE.getLongUnaligned(hb, byteOffset(checkIndex(i, 8)), bigEndian);
444 }
445
446
447
448 public ByteBuffer putLong(long x) {
449
450 UNSAFE.putLongUnaligned(hb, byteOffset(nextPutIndex(8)), x, bigEndian);
451 return this;
452
453
454
455 }
456
457 public ByteBuffer putLong(int i, long x) {
458
459 UNSAFE.putLongUnaligned(hb, byteOffset(checkIndex(i, 8)), x, bigEndian);
460 return this;
461
462
463
464 }
465
466 public LongBuffer asLongBuffer() {
467 int size = this.remaining() >> 3;
468 long addr = address + position();
469 return (bigEndian
470 ? (LongBuffer)(new ByteBufferAsLongBufferB(this,
471 -1,
472 0,
473 size,
474 size,
475 addr))
476 : (LongBuffer)(new ByteBufferAsLongBufferL(this,
477 -1,
478 0,
479 size,
480 size,
481 addr)));
482 }
483
484
485
486
487
488
489 public float getFloat() {
490 int x = UNSAFE.getIntUnaligned(hb, byteOffset(nextGetIndex(4)), bigEndian);
491 return Float.intBitsToFloat(x);
492 }
493
494 public float getFloat(int i) {
495 int x = UNSAFE.getIntUnaligned(hb, byteOffset(checkIndex(i, 4)), bigEndian);
496 return Float.intBitsToFloat(x);
497 }
498
499
500
501 public ByteBuffer putFloat(float x) {
502
503 int y = Float.floatToRawIntBits(x);
504 UNSAFE.putIntUnaligned(hb, byteOffset(nextPutIndex(4)), y, bigEndian);
505 return this;
506
507
508
509 }
510
511 public ByteBuffer putFloat(int i, float x) {
512
513 int y = Float.floatToRawIntBits(x);
514 UNSAFE.putIntUnaligned(hb, byteOffset(checkIndex(i, 4)), y, bigEndian);
515 return this;
516
517
518
519 }
520
521 public FloatBuffer asFloatBuffer() {
522 int size = this.remaining() >> 2;
523 long addr = address + position();
524 return (bigEndian
525 ? (FloatBuffer)(new ByteBufferAsFloatBufferB(this,
526 -1,
527 0,
528 size,
529 size,
530 addr))
531 : (FloatBuffer)(new ByteBufferAsFloatBufferL(this,
532 -1,
533 0,
534 size,
535 size,
536 addr)));
537 }
538
539
540
541
542
543
544 public double getDouble() {
545 long x = UNSAFE.getLongUnaligned(hb, byteOffset(nextGetIndex(8)), bigEndian);
546 return Double.longBitsToDouble(x);
547 }
548
549 public double getDouble(int i) {
550 long x = UNSAFE.getLongUnaligned(hb, byteOffset(checkIndex(i, 8)), bigEndian);
551 return Double.longBitsToDouble(x);
552 }
553
554
555
556 public ByteBuffer putDouble(double x) {
557
558 long y = Double.doubleToRawLongBits(x);
559 UNSAFE.putLongUnaligned(hb, byteOffset(nextPutIndex(8)), y, bigEndian);
560 return this;
561
562
563
564 }
565
566 public ByteBuffer putDouble(int i, double x) {
567
568 long y = Double.doubleToRawLongBits(x);
569 UNSAFE.putLongUnaligned(hb, byteOffset(checkIndex(i, 8)), y, bigEndian);
570 return this;
571
572
573
574 }
575
576 public DoubleBuffer asDoubleBuffer() {
577 int size = this.remaining() >> 3;
578 long addr = address + position();
579 return (bigEndian
580 ? (DoubleBuffer)(new ByteBufferAsDoubleBufferB(this,
581 -1,
582 0,
583 size,
584 size,
585 addr))
586 : (DoubleBuffer)(new ByteBufferAsDoubleBufferL(this,
587 -1,
588 0,
589 size,
590 size,
591 addr)));
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