History

Added some missing classes from java/nio, fixed some bad things

Dmitry Kabak (userdima committed 13 years ago

Commit 7d47bc81 parent b1be22084 files · +40111 −23Browse files

Showing the changes to java/pkg/java/nio/DirectByteBuffer.java (1 of 84 files). Show all

Unified · Split

java/pkg/java/nio/DirectByteBuffer.java+1009−0
@@ -0,0 +1,1009 @@
1/*
2 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26// -- This file was mechanically generated: Do not edit! -- //
27
28package java.nio;
29
30import java.io.FileDescriptor;
31import sun.misc.Cleaner;
32import sun.misc.Unsafe;
33import sun.misc.VM;
34import sun.nio.ch.DirectBuffer;
35
36
37class DirectByteBuffer
38
39 extends MappedByteBuffer
40
41
42
43 implements DirectBuffer
44{
45
46
47
48 // Cached unsafe-access object
49 protected static final Unsafe unsafe = Bits.unsafe();
50
51 // Cached array base offset
52 private static final long arrayBaseOffset = (long)unsafe.arrayBaseOffset(byte[].class);
53
54 // Cached unaligned-access capability
55 protected static final boolean unaligned = Bits.unaligned();
56
57 // Base address, used in all indexing calculations
58 // NOTE: moved up to Buffer.java for speed in JNI GetDirectBufferAddress
59 // protected long address;
60
61 // If this buffer is a view of another buffer then we keep a reference to
62 // that buffer so that its memory isn't freed before we're done with it
63 protected Object viewedBuffer = null;
64
65 public Object viewedBuffer() {
66 return viewedBuffer;
67 }
68
69
70
71 private static class Deallocator
72 implements Runnable
73 {
74
75 private static Unsafe unsafe = Unsafe.getUnsafe();
76
77 private long address;
78 private long size;
79 private int capacity;
80
81 private Deallocator(long address, long size, int capacity) {
82 assert (address != 0);
83 this.address = address;
84 this.size = size;
85 this.capacity = capacity;
86 }
87
88 public void run() {
89 if (address == 0) {
90 // Paranoia
91 return;
92 }
93 unsafe.freeMemory(address);
94 address = 0;
95 Bits.unreserveMemory(size, capacity);
96 }
97
98 }
99
100 private final Cleaner cleaner;
101
102 public Cleaner cleaner() { return cleaner; }
103
104
105
106
107
108
109
110
111
112
113
114 // Primary constructor
115 //
116 DirectByteBuffer(int cap) { // package-private
117
118 super(-1, 0, cap, cap);
119 boolean pa = VM.isDirectMemoryPageAligned();
120 int ps = Bits.pageSize();
121 long size = Math.max(1L, (long)cap + (pa ? ps : 0));
122 Bits.reserveMemory(size, cap);
123
124 long base = 0;
125 try {
126 base = unsafe.allocateMemory(size);
127 } catch (OutOfMemoryError x) {
128 Bits.unreserveMemory(size, cap);
129 throw x;
130 }
131 unsafe.setMemory(base, size, (byte) 0);
132 if (pa && (base % ps != 0)) {
133 // Round up to page boundary
134 address = base + ps - (base & (ps - 1));
135 } else {
136 address = base;
137 }
138 cleaner = Cleaner.create(this, new Deallocator(base, size, cap));
139
140
141
142 }
143
144
145
146 // Invoked only by JNI: NewDirectByteBuffer(void*, long)
147 //
148 private DirectByteBuffer(long addr, int cap) {
149 super(-1, 0, cap, cap);
150 address = addr;
151 cleaner = null;
152 }
153
154
155
156 // For memory-mapped buffers -- invoked by FileChannelImpl via reflection
157 //
158 protected DirectByteBuffer(int cap, long addr,
159 FileDescriptor fd,
160 Runnable unmapper)
161 {
162
163 super(-1, 0, cap, cap, fd);
164 address = addr;
165 viewedBuffer = null;
166 cleaner = Cleaner.create(this, unmapper);
167
168
169
170 }
171
172
173
174 // For duplicates and slices
175 //
176 DirectByteBuffer(DirectBuffer db, // package-private
177 int mark, int pos, int lim, int cap,
178 int off)
179 {
180
181 super(mark, pos, lim, cap);
182 address = db.address() + off;
183 viewedBuffer = db;
184
185 cleaner = null;
186
187
188
189
190 }
191
192 public ByteBuffer slice() {
193 int pos = this.position();
194 int lim = this.limit();
195 assert (pos <= lim);
196 int rem = (pos <= lim ? lim - pos : 0);
197 int off = (pos << 0);
198 assert (off >= 0);
199 return new DirectByteBuffer(this, -1, 0, rem, rem, off);
200 }
201
202 public ByteBuffer duplicate() {
203 return new DirectByteBuffer(this,
204 this.markValue(),
205 this.position(),
206 this.limit(),
207 this.capacity(),
208 0);
209 }
210
211 public ByteBuffer asReadOnlyBuffer() {
212
213 return new DirectByteBufferR(this,
214 this.markValue(),
215 this.position(),
216 this.limit(),
217 this.capacity(),
218 0);
219
220
221
222 }
223
224
225
226 public long address() {
227 return address;
228 }
229
230 private long ix(int i) {
231 return address + (i << 0);
232 }
233
234 public byte get() {
235 return ((unsafe.getByte(ix(nextGetIndex()))));
236 }
237
238 public byte get(int i) {
239 return ((unsafe.getByte(ix(checkIndex(i)))));
240 }
241
242 public ByteBuffer get(byte[] dst, int offset, int length) {
243
244 if ((length << 0) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
245 checkBounds(offset, length, dst.length);
246 int pos = position();
247 int lim = limit();
248 assert (pos <= lim);
249 int rem = (pos <= lim ? lim - pos : 0);
250 if (length > rem)
251 throw new BufferUnderflowException();
252
253
254
255
256
257
258
259
260 Bits.copyToArray(ix(pos), dst, arrayBaseOffset,
261 offset << 0,
262 length << 0);
263 position(pos + length);
264 } else {
265 super.get(dst, offset, length);
266 }
267 return this;
268
269
270
271 }
272
273
274
275 public ByteBuffer put(byte x) {
276
277 unsafe.putByte(ix(nextPutIndex()), ((x)));
278 return this;
279
280
281
282 }
283
284 public ByteBuffer put(int i, byte x) {
285
286 unsafe.putByte(ix(checkIndex(i)), ((x)));
287 return this;
288
289
290
291 }
292
293 public ByteBuffer put(ByteBuffer src) {
294
295 if (src instanceof DirectByteBuffer) {
296 if (src == this)
297 throw new IllegalArgumentException();
298 DirectByteBuffer sb = (DirectByteBuffer)src;
299
300 int spos = sb.position();
301 int slim = sb.limit();
302 assert (spos <= slim);
303 int srem = (spos <= slim ? slim - spos : 0);
304
305 int pos = position();
306 int lim = limit();
307 assert (pos <= lim);
308 int rem = (pos <= lim ? lim - pos : 0);
309
310 if (srem > rem)
311 throw new BufferOverflowException();
312 unsafe.copyMemory(sb.ix(spos), ix(pos), srem << 0);
313 sb.position(spos + srem);
314 position(pos + srem);
315 } else if (src.hb != null) {
316
317 int spos = src.position();
318 int slim = src.limit();
319 assert (spos <= slim);
320 int srem = (spos <= slim ? slim - spos : 0);
321
322 put(src.hb, src.offset + spos, srem);
323 src.position(spos + srem);
324
325 } else {
326 super.put(src);
327 }
328 return this;
329
330
331
332 }
333
334 public ByteBuffer put(byte[] src, int offset, int length) {
335
336 if ((length << 0) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
337 checkBounds(offset, length, src.length);
338 int pos = position();
339 int lim = limit();
340 assert (pos <= lim);
341 int rem = (pos <= lim ? lim - pos : 0);
342 if (length > rem)
343 throw new BufferOverflowException();
344
345
346
347
348
349
350
351 Bits.copyFromArray(src, arrayBaseOffset, offset << 0,
352 ix(pos), length << 0);
353 position(pos + length);
354 } else {
355 super.put(src, offset, length);
356 }
357 return this;
358
359
360
361 }
362
363 public ByteBuffer compact() {
364
365 int pos = position();
366 int lim = limit();
367 assert (pos <= lim);
368 int rem = (pos <= lim ? lim - pos : 0);
369
370 unsafe.copyMemory(ix(pos), ix(0), rem << 0);
371 position(rem);
372 limit(capacity());
373 discardMark();
374 return this;
375
376
377
378 }
379
380 public boolean isDirect() {
381 return true;
382 }
383
384 public boolean isReadOnly() {
385 return false;
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 byte _get(int i) { // package-private
452 return unsafe.getByte(address + i);
453 }
454
455 void _put(int i, byte b) { // package-private
456
457 unsafe.putByte(address + i, b);
458
459
460
461 }
462
463
464
465
466 private char getChar(long a) {
467 if (unaligned) {
468 char x = unsafe.getChar(a);
469 return (nativeByteOrder ? x : Bits.swap(x));
470 }
471 return Bits.getChar(a, bigEndian);
472 }
473
474 public char getChar() {
475 return getChar(ix(nextGetIndex((1 << 1))));
476 }
477
478 public char getChar(int i) {
479 return getChar(ix(checkIndex(i, (1 << 1))));
480 }
481
482
483
484 private ByteBuffer putChar(long a, char x) {
485
486 if (unaligned) {
487 char y = (x);
488 unsafe.putChar(a, (nativeByteOrder ? y : Bits.swap(y)));
489 } else {
490 Bits.putChar(a, x, bigEndian);
491 }
492 return this;
493
494
495
496 }
497
498 public ByteBuffer putChar(char x) {
499
500 putChar(ix(nextPutIndex((1 << 1))), x);
501 return this;
502
503
504
505 }
506
507 public ByteBuffer putChar(int i, char x) {
508
509 putChar(ix(checkIndex(i, (1 << 1))), x);
510 return this;
511
512
513
514 }
515
516 public CharBuffer asCharBuffer() {
517 int off = this.position();
518 int lim = this.limit();
519 assert (off <= lim);
520 int rem = (off <= lim ? lim - off : 0);
521
522 int size = rem >> 1;
523 if (!unaligned && ((address + off) % (1 << 1) != 0)) {
524 return (bigEndian
525 ? (CharBuffer)(new ByteBufferAsCharBufferB(this,
526 -1,
527 0,
528 size,
529 size,
530 off))
531 : (CharBuffer)(new ByteBufferAsCharBufferL(this,
532 -1,
533 0,
534 size,
535 size,
536 off)));
537 } else {
538 return (nativeByteOrder
539 ? (CharBuffer)(new DirectCharBufferU(this,
540 -1,
541 0,
542 size,
543 size,
544 off))
545 : (CharBuffer)(new DirectCharBufferS(this,
546 -1,
547 0,
548 size,
549 size,
550 off)));
551 }
552 }
553
554
555
556
557 private short getShort(long a) {
558 if (unaligned) {
559 short x = unsafe.getShort(a);
560 return (nativeByteOrder ? x : Bits.swap(x));
561 }
562 return Bits.getShort(a, bigEndian);
563 }
564
565 public short getShort() {
566 return getShort(ix(nextGetIndex((1 << 1))));
567 }
568
569 public short getShort(int i) {
570 return getShort(ix(checkIndex(i, (1 << 1))));
571 }
572
573
574
575 private ByteBuffer putShort(long a, short x) {
576
577 if (unaligned) {
578 short y = (x);
579 unsafe.putShort(a, (nativeByteOrder ? y : Bits.swap(y)));
580 } else {
581 Bits.putShort(a, x, bigEndian);
582 }
583 return this;
584
585
586
587 }
588
589 public ByteBuffer putShort(short x) {
590
591 putShort(ix(nextPutIndex((1 << 1))), x);
592 return this;
593
594
595
596 }
597
598 public ByteBuffer putShort(int i, short x) {
599
600 putShort(ix(checkIndex(i, (1 << 1))), x);
601 return this;
602
603
604
605 }
606
607 public ShortBuffer asShortBuffer() {
608 int off = this.position();
609 int lim = this.limit();
610 assert (off <= lim);
611 int rem = (off <= lim ? lim - off : 0);
612
613 int size = rem >> 1;
614 if (!unaligned && ((address + off) % (1 << 1) != 0)) {
615 return (bigEndian
616 ? (ShortBuffer)(new ByteBufferAsShortBufferB(this,
617 -1,
618 0,
619 size,
620 size,
621 off))
622 : (ShortBuffer)(new ByteBufferAsShortBufferL(this,
623 -1,
624 0,
625 size,
626 size,
627 off)));
628 } else {
629 return (nativeByteOrder
630 ? (ShortBuffer)(new DirectShortBufferU(this,
631 -1,
632 0,
633 size,
634 size,
635 off))
636 : (ShortBuffer)(new DirectShortBufferS(this,
637 -1,
638 0,
639 size,
640 size,
641 off)));
642 }
643 }
644
645
646
647
648 private int getInt(long a) {
649 if (unaligned) {
650 int x = unsafe.getInt(a);
651 return (nativeByteOrder ? x : Bits.swap(x));
652 }
653 return Bits.getInt(a, bigEndian);
654 }
655
656 public int getInt() {
657 return getInt(ix(nextGetIndex((1 << 2))));
658 }
659
660 public int getInt(int i) {
661 return getInt(ix(checkIndex(i, (1 << 2))));
662 }
663
664
665
666 private ByteBuffer putInt(long a, int x) {
667
668 if (unaligned) {
669 int y = (x);
670 unsafe.putInt(a, (nativeByteOrder ? y : Bits.swap(y)));
671 } else {
672 Bits.putInt(a, x, bigEndian);
673 }
674 return this;
675
676
677
678 }
679
680 public ByteBuffer putInt(int x) {
681
682 putInt(ix(nextPutIndex((1 << 2))), x);
683 return this;
684
685
686
687 }
688
689 public ByteBuffer putInt(int i, int x) {
690
691 putInt(ix(checkIndex(i, (1 << 2))), x);
692 return this;
693
694
695
696 }
697
698 public IntBuffer asIntBuffer() {
699 int off = this.position();
700 int lim = this.limit();
701 assert (off <= lim);
702 int rem = (off <= lim ? lim - off : 0);
703
704 int size = rem >> 2;
705 if (!unaligned && ((address + off) % (1 << 2) != 0)) {
706 return (bigEndian
707 ? (IntBuffer)(new ByteBufferAsIntBufferB(this,
708 -1,
709 0,
710 size,
711 size,
712 off))
713 : (IntBuffer)(new ByteBufferAsIntBufferL(this,
714 -1,
715 0,
716 size,
717 size,
718 off)));
719 } else {
720 return (nativeByteOrder
721 ? (IntBuffer)(new DirectIntBufferU(this,
722 -1,
723 0,
724 size,
725 size,
726 off))
727 : (IntBuffer)(new DirectIntBufferS(this,
728 -1,
729 0,
730 size,
731 size,
732 off)));
733 }
734 }
735
736
737
738
739 private long getLong(long a) {
740 if (unaligned) {
741 long x = unsafe.getLong(a);
742 return (nativeByteOrder ? x : Bits.swap(x));
743 }
744 return Bits.getLong(a, bigEndian);
745 }
746
747 public long getLong() {
748 return getLong(ix(nextGetIndex((1 << 3))));
749 }
750
751 public long getLong(int i) {
752 return getLong(ix(checkIndex(i, (1 << 3))));
753 }
754
755
756
757 private ByteBuffer putLong(long a, long x) {
758
759 if (unaligned) {
760 long y = (x);
761 unsafe.putLong(a, (nativeByteOrder ? y : Bits.swap(y)));
762 } else {
763 Bits.putLong(a, x, bigEndian);
764 }
765 return this;
766
767
768
769 }
770
771 public ByteBuffer putLong(long x) {
772
773 putLong(ix(nextPutIndex((1 << 3))), x);
774 return this;
775
776
777
778 }
779
780 public ByteBuffer putLong(int i, long x) {
781
782 putLong(ix(checkIndex(i, (1 << 3))), x);
783 return this;
784
785
786
787 }
788
789 public LongBuffer asLongBuffer() {
790 int off = this.position();
791 int lim = this.limit();
792 assert (off <= lim);
793 int rem = (off <= lim ? lim - off : 0);
794
795 int size = rem >> 3;
796 if (!unaligned && ((address + off) % (1 << 3) != 0)) {
797 return (bigEndian
798 ? (LongBuffer)(new ByteBufferAsLongBufferB(this,
799 -1,
800 0,
801 size,
802 size,
803 off))
804 : (LongBuffer)(new ByteBufferAsLongBufferL(this,
805 -1,
806 0,
807 size,
808 size,
809 off)));
810 } else {
811 return (nativeByteOrder
812 ? (LongBuffer)(new DirectLongBufferU(this,
813 -1,
814 0,
815 size,
816 size,
817 off))
818 : (LongBuffer)(new DirectLongBufferS(this,
819 -1,
820 0,
821 size,
822 size,
823 off)));
824 }
825 }
826
827
828
829
830 private float getFloat(long a) {
831 if (unaligned) {
832 int x = unsafe.getInt(a);
833 return Float.intBitsToFloat(nativeByteOrder ? x : Bits.swap(x));
834 }
835 return Bits.getFloat(a, bigEndian);
836 }
837
838 public float getFloat() {
839 return getFloat(ix(nextGetIndex((1 << 2))));
840 }
841
842 public float getFloat(int i) {
843 return getFloat(ix(checkIndex(i, (1 << 2))));
844 }
845
846
847
848 private ByteBuffer putFloat(long a, float x) {
849
850 if (unaligned) {
851 int y = Float.floatToRawIntBits(x);
852 unsafe.putInt(a, (nativeByteOrder ? y : Bits.swap(y)));
853 } else {
854 Bits.putFloat(a, x, bigEndian);
855 }
856 return this;
857
858
859
860 }
861
862 public ByteBuffer putFloat(float x) {
863
864 putFloat(ix(nextPutIndex((1 << 2))), x);
865 return this;
866
867
868
869 }
870
871 public ByteBuffer putFloat(int i, float x) {
872
873 putFloat(ix(checkIndex(i, (1 << 2))), x);
874 return this;
875
876
877
878 }
879
880 public FloatBuffer asFloatBuffer() {
881 int off = this.position();
882 int lim = this.limit();
883 assert (off <= lim);
884 int rem = (off <= lim ? lim - off : 0);
885
886 int size = rem >> 2;
887 if (!unaligned && ((address + off) % (1 << 2) != 0)) {
888 return (bigEndian
889 ? (FloatBuffer)(new ByteBufferAsFloatBufferB(this,
890 -1,
891 0,
892 size,
893 size,
894 off))
895 : (FloatBuffer)(new ByteBufferAsFloatBufferL(this,
896 -1,
897 0,
898 size,
899 size,
900 off)));
901 } else {
902 return (nativeByteOrder
903 ? (FloatBuffer)(new DirectFloatBufferU(this,
904 -1,
905 0,
906 size,
907 size,
908 off))
909 : (FloatBuffer)(new DirectFloatBufferS(this,
910 -1,
911 0,
912 size,
913 size,
914 off)));
915 }
916 }
917
918
919
920
921 private double getDouble(long a) {
922 if (unaligned) {
923 long x = unsafe.getLong(a);
924 return Double.longBitsToDouble(nativeByteOrder ? x : Bits.swap(x));
925 }
926 return Bits.getDouble(a, bigEndian);
927 }
928
929 public double getDouble() {
930 return getDouble(ix(nextGetIndex((1 << 3))));
931 }
932
933 public double getDouble(int i) {
934 return getDouble(ix(checkIndex(i, (1 << 3))));
935 }
936
937
938
939 private ByteBuffer putDouble(long a, double x) {
940
941 if (unaligned) {
942 long y = Double.doubleToRawLongBits(x);
943 unsafe.putLong(a, (nativeByteOrder ? y : Bits.swap(y)));
944 } else {
945 Bits.putDouble(a, x, bigEndian);
946 }
947 return this;
948
949
950
951 }
952
953 public ByteBuffer putDouble(double x) {
954
955 putDouble(ix(nextPutIndex((1 << 3))), x);
956 return this;
957
958
959
960 }
961
962 public ByteBuffer putDouble(int i, double x) {
963
964 putDouble(ix(checkIndex(i, (1 << 3))), x);
965 return this;
966
967
968
969 }
970
971 public DoubleBuffer asDoubleBuffer() {
972 int off = this.position();
973 int lim = this.limit();
974 assert (off <= lim);
975 int rem = (off <= lim ? lim - off : 0);
976
977 int size = rem >> 3;
978 if (!unaligned && ((address + off) % (1 << 3) != 0)) {
979 return (bigEndian
980 ? (DoubleBuffer)(new ByteBufferAsDoubleBufferB(this,
981 -1,
982 0,
983 size,
984 size,
985 off))
986 : (DoubleBuffer)(new ByteBufferAsDoubleBufferL(this,
987 -1,
988 0,
989 size,
990 size,
991 off)));
992 } else {
993 return (nativeByteOrder
994 ? (DoubleBuffer)(new DirectDoubleBufferU(this,
995 -1,
996 0,
997 size,
998 size,
999 off))
1000 : (DoubleBuffer)(new DirectDoubleBufferS(this,
1001 -1,
1002 0,
1003 size,
1004 size,
1005 off)));
1006 }
1007 }
1008
1009}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t