KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > nio > DirectByteBuffer


1 /*
2  * @(#)Direct-X-Buffer.java 1.48 04/05/03
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 // -- This file was mechanically generated: Do not edit! -- //
9

10 package java.nio;
11
12 import sun.misc.Cleaner;
13 import sun.misc.Unsafe;
14 import sun.nio.ch.DirectBuffer;
15 import sun.nio.ch.FileChannelImpl;
16
17
18 class DirectByteBuffer
19
20     extends MappedByteBuffer JavaDoc
21
22
23
24     implements DirectBuffer
25 {
26
27
28
29     // Cached unsafe-access object
30
protected static final Unsafe unsafe = Bits.unsafe();
31
32     // Cached unaligned-access capability
33
protected static final boolean unaligned = Bits.unaligned();
34
35     // Base address, used in all indexing calculations
36
// NOTE: moved up to Buffer.java for speed in JNI GetDirectBufferAddress
37
// protected long address;
38

39     // If this buffer is a view of another buffer then we keep a reference to
40
// that buffer so that its memory isn't freed before we're done with it
41
protected Object JavaDoc viewedBuffer = null;
42
43     public Object JavaDoc viewedBuffer() {
44         return viewedBuffer;
45     }
46
47
48
49     private static class Deallocator
50     implements Runnable JavaDoc
51     {
52
53     private static Unsafe unsafe = Unsafe.getUnsafe();
54
55     private long address;
56     private int capacity;
57
58     private Deallocator(long address, int capacity) {
59         assert (address != 0);
60         this.address = address;
61         this.capacity = capacity;
62     }
63
64     public void run() {
65         if (address == 0) {
66         // Paranoia
67
return;
68         }
69         unsafe.freeMemory(address);
70         address = 0;
71         Bits.unreserveMemory(capacity);
72     }
73
74     }
75
76     private final Cleaner cleaner;
77
78     public Cleaner cleaner() { return cleaner; }
79
80
81
82
83
84
85
86
87
88
89
90     // Primary constructor
91
//
92
DirectByteBuffer(int cap) { // package-private
93

94     super(-1, 0, cap, cap, false);
95     Bits.reserveMemory(cap);
96     int ps = Bits.pageSize();
97     long base = 0;
98     try {
99         base = unsafe.allocateMemory(cap + ps);
100     } catch (OutOfMemoryError JavaDoc x) {
101         Bits.unreserveMemory(cap);
102         throw x;
103     }
104     unsafe.setMemory(base, cap + ps, (byte) 0);
105     if (base % ps != 0) {
106         // Round up to page boundary
107
address = base + ps - (base & (ps - 1));
108     } else {
109         address = base;
110     }
111     cleaner = Cleaner.create(this, new Deallocator(base, cap));
112
113
114
115     }
116
117
118
119     // Invoked only by JNI: NewDirectByteBuffer(void*, long)
120
//
121
private DirectByteBuffer(long addr, int cap) {
122         super(-1, 0, cap, cap, false);
123     address = addr;
124     cleaner = null;
125     }
126
127
128
129     // For memory-mapped buffers -- invoked by FileChannelImpl via reflection
130
//
131
protected DirectByteBuffer(int cap, long addr, Runnable JavaDoc unmapper) {
132
133         super(-1, 0, cap, cap, true);
134     address = addr;
135         viewedBuffer = null;
136     cleaner = Cleaner.create(this, unmapper);
137
138
139
140     }
141
142
143
144     // For duplicates and slices
145
//
146
DirectByteBuffer(DirectBuffer db, // package-private
147
int mark, int pos, int lim, int cap,
148                    int off)
149     {
150
151     super(mark, pos, lim, cap);
152     address = db.address() + off;
153     viewedBuffer = db;
154
155     cleaner = null;
156
157
158
159
160     }
161
162     public ByteBuffer JavaDoc slice() {
163     int pos = this.position();
164     int lim = this.limit();
165     assert (pos <= lim);
166     int rem = (pos <= lim ? lim - pos : 0);
167     int off = (pos << 0);
168         assert (off >= 0);
169     return new DirectByteBuffer JavaDoc(this, -1, 0, rem, rem, off);
170     }
171
172     public ByteBuffer JavaDoc duplicate() {
173     return new DirectByteBuffer JavaDoc(this,
174                           this.markValue(),
175                           this.position(),
176                           this.limit(),
177                           this.capacity(),
178                           0);
179     }
180
181     public ByteBuffer JavaDoc asReadOnlyBuffer() {
182
183     return new DirectByteBufferR JavaDoc(this,
184                        this.markValue(),
185                        this.position(),
186                        this.limit(),
187                        this.capacity(),
188                        0);
189
190
191
192     }
193
194
195
196     public long address() {
197     return address;
198     }
199
200     private long ix(int i) {
201         return address + (i << 0);
202     }
203
204     public byte get() {
205     return ((unsafe.getByte(ix(nextGetIndex()))));
206     }
207
208     public byte get(int i) {
209     return ((unsafe.getByte(ix(checkIndex(i)))));
210     }
211
212     public ByteBuffer JavaDoc get(byte[] dst, int offset, int length) {
213
214     if ((length << 0) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
215         checkBounds(offset, length, dst.length);
216         int pos = position();
217         int lim = limit();
218         assert (pos <= lim);
219         int rem = (pos <= lim ? lim - pos : 0);
220         if (length > rem)
221         throw new BufferUnderflowException JavaDoc();
222
223         if (order() != ByteOrder.nativeOrder())
224         Bits.copyToByteArray(ix(pos), dst,
225                       offset << 0,
226                       length << 0);
227         else
228         Bits.copyToByteArray(ix(pos), dst,
229                      offset << 0,
230                      length << 0);
231         position(pos + length);
232     } else {
233         super.get(dst, offset, length);
234     }
235     return this;
236
237
238
239     }
240
241
242
243     public ByteBuffer JavaDoc put(byte x) {
244
245     unsafe.putByte(ix(nextPutIndex()), ((x)));
246     return this;
247
248
249
250     }
251
252     public ByteBuffer JavaDoc put(int i, byte x) {
253
254     unsafe.putByte(ix(checkIndex(i)), ((x)));
255     return this;
256
257
258
259     }
260
261     public ByteBuffer JavaDoc put(ByteBuffer JavaDoc src) {
262
263     if (src instanceof DirectByteBuffer JavaDoc) {
264         if (src == this)
265         throw new IllegalArgumentException JavaDoc();
266         DirectByteBuffer JavaDoc sb = (DirectByteBuffer JavaDoc)src;
267
268         int spos = sb.position();
269         int slim = sb.limit();
270         assert (spos <= slim);
271         int srem = (spos <= slim ? slim - spos : 0);
272
273         int pos = position();
274         int lim = limit();
275         assert (pos <= lim);
276         int rem = (pos <= lim ? lim - pos : 0);
277
278         if (srem > rem)
279         throw new BufferOverflowException JavaDoc();
280         unsafe.copyMemory(sb.ix(spos), ix(pos), srem << 0);
281         sb.position(spos + srem);
282         position(pos + srem);
283     } else if (!src.isDirect()) {
284
285         int spos = src.position();
286         int slim = src.limit();
287         assert (spos <= slim);
288         int srem = (spos <= slim ? slim - spos : 0);
289
290         put(src.hb, src.offset + spos, srem);
291         src.position(spos + srem);
292
293     } else {
294         super.put(src);
295     }
296     return this;
297
298
299
300     }
301
302     public ByteBuffer JavaDoc put(byte[] src, int offset, int length) {
303
304     if ((length << 0) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
305         checkBounds(offset, length, src.length);
306         int pos = position();
307         int lim = limit();
308         assert (pos <= lim);
309         int rem = (pos <= lim ? lim - pos : 0);
310         if (length > rem)
311         throw new BufferOverflowException JavaDoc();
312
313         if (order() != ByteOrder.nativeOrder())
314         Bits.copyFromByteArray(src, offset << 0,
315                         ix(pos), length << 0);
316         else
317         Bits.copyFromByteArray(src, offset << 0,
318                        ix(pos), length << 0);
319         position(pos + length);
320     } else {
321         super.put(src, offset, length);
322     }
323     return this;
324
325
326
327     }
328     
329     public ByteBuffer JavaDoc compact() {
330
331     int pos = position();
332     int lim = limit();
333     assert (pos <= lim);
334     int rem = (pos <= lim ? lim - pos : 0);
335
336     unsafe.copyMemory(ix(pos), ix(0), rem << 0);
337     position(rem);
338     limit(capacity());
339     return this;
340
341
342
343     }
344
345     public boolean isDirect() {
346     return true;
347     }
348
349     public boolean isReadOnly() {
350     return false;
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     byte _get(int i) { // package-private
415
return unsafe.getByte(address + i);
416     }
417
418     void _put(int i, byte b) { // package-private
419

420     unsafe.putByte(address + i, b);
421
422
423
424     }
425
426
427
428
429     private char getChar(long a) {
430     if (unaligned) {
431         char x = unsafe.getChar(a);
432         return (nativeByteOrder ? x : Bits.swap(x));
433     }
434     return Bits.getChar(a, bigEndian);
435     }
436
437     public char getChar() {
438     return getChar(ix(nextGetIndex((1 << 1))));
439     }
440
441     public char getChar(int i) {
442     return getChar(ix(checkIndex(i, (1 << 1))));
443     }
444
445
446
447     private ByteBuffer JavaDoc putChar(long a, char x) {
448
449     if (unaligned) {
450         char y = (x);
451         unsafe.putChar(a, (nativeByteOrder ? y : Bits.swap(y)));
452     } else {
453         Bits.putChar(a, x, bigEndian);
454     }
455     return this;
456
457
458
459     }
460
461     public ByteBuffer JavaDoc putChar(char x) {
462
463     putChar(ix(nextPutIndex((1 << 1))), x);
464     return this;
465
466
467
468     }
469
470     public ByteBuffer JavaDoc putChar(int i, char x) {
471
472     putChar(ix(checkIndex(i, (1 << 1))), x);
473     return this;
474
475
476
477     }
478
479     public CharBuffer JavaDoc asCharBuffer() {
480     int off = this.position();
481     int lim = this.limit();
482     assert (off <= lim);
483     int rem = (off <= lim ? lim - off : 0);
484
485     int size = rem >> 1;
486     if (!unaligned && ((address + off) % (1 << 1) != 0)) {
487         return (bigEndian
488             ? (CharBuffer JavaDoc)(new ByteBufferAsCharBufferB JavaDoc(this,
489                                        -1,
490                                        0,
491                                        size,
492                                        size,
493                                        off))
494             : (CharBuffer JavaDoc)(new ByteBufferAsCharBufferL JavaDoc(this,
495                                        -1,
496                                        0,
497                                        size,
498                                        size,
499                                        off)));
500     } else {
501         return (nativeByteOrder
502             ? (CharBuffer JavaDoc)(new DirectCharBufferU JavaDoc(this,
503                                  -1,
504                                  0,
505                                  size,
506                                  size,
507                                  off))
508             : (CharBuffer JavaDoc)(new DirectCharBufferS JavaDoc(this,
509                                  -1,
510                                  0,
511                                  size,
512                                  size,
513                                  off)));
514     }
515     }
516
517
518
519
520     private short getShort(long a) {
521     if (unaligned) {
522         short x = unsafe.getShort(a);
523         return (nativeByteOrder ? x : Bits.swap(x));
524     }
525     return Bits.getShort(a, bigEndian);
526     }
527
528     public short getShort() {
529     return getShort(ix(nextGetIndex((1 << 1))));
530     }
531
532     public short getShort(int i) {
533     return getShort(ix(checkIndex(i, (1 << 1))));
534     }
535
536
537
538     private ByteBuffer JavaDoc putShort(long a, short x) {
539
540     if (unaligned) {
541         short y = (x);
542         unsafe.putShort(a, (nativeByteOrder ? y : Bits.swap(y)));
543     } else {
544         Bits.putShort(a, x, bigEndian);
545     }
546     return this;
547
548
549
550     }
551
552     public ByteBuffer JavaDoc putShort(short x) {
553
554     putShort(ix(nextPutIndex((1 << 1))), x);
555     return this;
556
557
558
559     }
560
561     public ByteBuffer JavaDoc putShort(int i, short x) {
562
563     putShort(ix(checkIndex(i, (1 << 1))), x);
564     return this;
565
566
567
568     }
569
570     public ShortBuffer JavaDoc asShortBuffer() {
571     int off = this.position();
572     int lim = this.limit();
573     assert (off <= lim);
574     int rem = (off <= lim ? lim - off : 0);
575
576     int size = rem >> 1;
577     if (!unaligned && ((address + off) % (1 << 1) != 0)) {
578         return (bigEndian
579             ? (ShortBuffer JavaDoc)(new ByteBufferAsShortBufferB JavaDoc(this,
580                                        -1,
581                                        0,
582                                        size,
583                                        size,
584                                        off))
585             : (ShortBuffer JavaDoc)(new ByteBufferAsShortBufferL JavaDoc(this,
586                                        -1,
587                                        0,
588                                        size,
589                                        size,
590                                        off)));
591     } else {
592         return (nativeByteOrder
593             ? (ShortBuffer JavaDoc)(new DirectShortBufferU JavaDoc(this,
594                                  -1,
595                                  0,
596                                  size,
597                                  size,
598                                  off))
599             : (ShortBuffer JavaDoc)(new DirectShortBufferS JavaDoc(this,
600                                  -1,
601                                  0,
602                                  size,
603                                  size,
604                                  off)));
605     }
606     }
607
608
609
610
611     private int getInt(long a) {
612     if (unaligned) {
613         int x = unsafe.getInt(a);
614         return (nativeByteOrder ? x : Bits.swap(x));
615     }
616     return Bits.getInt(a, bigEndian);
617     }
618
619     public int getInt() {
620     return getInt(ix(nextGetIndex((1 << 2))));
621     }
622
623     public int getInt(int i) {
624     return getInt(ix(checkIndex(i, (1 << 2))));
625     }
626
627
628
629     private ByteBuffer JavaDoc putInt(long a, int x) {
630
631     if (unaligned) {
632         int y = (x);
633         unsafe.putInt(a, (nativeByteOrder ? y : Bits.swap(y)));
634     } else {
635         Bits.putInt(a, x, bigEndian);
636     }
637     return this;
638
639
640
641     }
642
643     public ByteBuffer JavaDoc putInt(int x) {
644
645     putInt(ix(nextPutIndex((1 << 2))), x);
646     return this;
647
648
649
650     }
651
652     public ByteBuffer JavaDoc putInt(int i, int x) {
653
654     putInt(ix(checkIndex(i, (1 << 2))), x);
655     return this;
656
657
658
659     }
660
661     public IntBuffer JavaDoc asIntBuffer() {
662     int off = this.position();
663     int lim = this.limit();
664     assert (off <= lim);
665     int rem = (off <= lim ? lim - off : 0);
666
667     int size = rem >> 2;
668     if (!unaligned && ((address + off) % (1 << 2) != 0)) {
669         return (bigEndian
670             ? (IntBuffer JavaDoc)(new ByteBufferAsIntBufferB JavaDoc(this,
671                                        -1,
672                                        0,
673                                        size,
674                                        size,
675                                        off))
676             : (IntBuffer JavaDoc)(new ByteBufferAsIntBufferL JavaDoc(this,
677                                        -1,
678                                        0,
679                                        size,
680                                        size,
681                                        off)));
682     } else {
683         return (nativeByteOrder
684             ? (IntBuffer JavaDoc)(new DirectIntBufferU JavaDoc(this,
685                                  -1,
686                                  0,
687                                  size,
688                                  size,
689                                  off))
690             : (IntBuffer JavaDoc)(new DirectIntBufferS JavaDoc(this,
691                                  -1,
692                                  0,
693                                  size,
694                                  size,
695                                  off)));
696     }
697     }
698
699
700
701
702     private long getLong(long a) {
703     if (unaligned) {
704         long x = unsafe.getLong(a);
705         return (nativeByteOrder ? x : Bits.swap(x));
706     }
707     return Bits.getLong(a, bigEndian);
708     }
709
710     public long getLong() {
711     return getLong(ix(nextGetIndex((1 << 3))));
712     }
713
714     public long getLong(int i) {
715     return getLong(ix(checkIndex(i, (1 << 3))));
716     }
717
718
719
720     private ByteBuffer JavaDoc putLong(long a, long x) {
721
722     if (unaligned) {
723         long y = (x);
724         unsafe.putLong(a, (nativeByteOrder ? y : Bits.swap(y)));
725     } else {
726         Bits.putLong(a, x, bigEndian);
727     }
728     return this;
729
730
731
732     }
733
734     public ByteBuffer JavaDoc putLong(long x) {
735
736     putLong(ix(nextPutIndex((1 << 3))), x);
737     return this;
738
739
740
741     }
742
743     public ByteBuffer JavaDoc putLong(int i, long x) {
744
745     putLong(ix(checkIndex(i, (1 << 3))), x);
746     return this;
747
748
749
750     }
751
752     public LongBuffer JavaDoc asLongBuffer() {
753     int off = this.position();
754     int lim = this.limit();
755     assert (off <= lim);
756     int rem = (off <= lim ? lim - off : 0);
757
758     int size = rem >> 3;
759     if (!unaligned && ((address + off) % (1 << 3) != 0)) {
760         return (bigEndian
761             ? (LongBuffer JavaDoc)(new ByteBufferAsLongBufferB JavaDoc(this,
762                                        -1,
763                                        0,
764                                        size,
765                                        size,
766                                        off))
767             : (LongBuffer JavaDoc)(new ByteBufferAsLongBufferL JavaDoc(this,
768                                        -1,
769                                        0,
770                                        size,
771                                        size,
772                                        off)));
773     } else {
774         return (nativeByteOrder
775             ? (LongBuffer JavaDoc)(new DirectLongBufferU JavaDoc(this,
776                                  -1,
777                                  0,
778                                  size,
779                                  size,
780                                  off))
781             : (LongBuffer JavaDoc)(new DirectLongBufferS JavaDoc(this,
782                                  -1,
783                                  0,
784                                  size,
785                                  size,
786                                  off)));
787     }
788     }
789
790
791
792
793     private float getFloat(long a) {
794     if (unaligned) {
795         int x = unsafe.getInt(a);
796         return Float.intBitsToFloat(nativeByteOrder ? x : Bits.swap(x));
797     }
798     return Bits.getFloat(a, bigEndian);
799     }
800
801     public float getFloat() {
802     return getFloat(ix(nextGetIndex((1 << 2))));
803     }
804
805     public float getFloat(int i) {
806     return getFloat(ix(checkIndex(i, (1 << 2))));
807     }
808
809
810
811     private ByteBuffer JavaDoc putFloat(long a, float x) {
812
813     if (unaligned) {
814         int y = Float.floatToRawIntBits(x);
815         unsafe.putInt(a, (nativeByteOrder ? y : Bits.swap(y)));
816     } else {
817         Bits.putFloat(a, x, bigEndian);
818     }
819     return this;
820
821
822
823     }
824
825     public ByteBuffer JavaDoc putFloat(float x) {
826
827     putFloat(ix(nextPutIndex((1 << 2))), x);
828     return this;
829
830
831
832     }
833
834     public ByteBuffer JavaDoc putFloat(int i, float x) {
835
836     putFloat(ix(checkIndex(i, (1 << 2))), x);
837     return this;
838
839
840
841     }
842
843     public FloatBuffer JavaDoc asFloatBuffer() {
844     int off = this.position();
845     int lim = this.limit();
846     assert (off <= lim);
847     int rem = (off <= lim ? lim - off : 0);
848
849     int size = rem >> 2;
850     if (!unaligned && ((address + off) % (1 << 2) != 0)) {
851         return (bigEndian
852             ? (FloatBuffer JavaDoc)(new ByteBufferAsFloatBufferB JavaDoc(this,
853                                        -1,
854                                        0,
855                                        size,
856                                        size,
857                                        off))
858             : (FloatBuffer JavaDoc)(new ByteBufferAsFloatBufferL JavaDoc(this,
859                                        -1,
860                                        0,
861                                        size,
862                                        size,
863                                        off)));
864     } else {
865         return (nativeByteOrder
866             ? (FloatBuffer JavaDoc)(new DirectFloatBufferU JavaDoc(this,
867                                  -1,
868                                  0,
869                                  size,
870                                  size,
871                                  off))
872             : (FloatBuffer JavaDoc)(new DirectFloatBufferS JavaDoc(this,
873                                  -1,
874                                  0,
875                                  size,
876                                  size,
877                                  off)));
878     }
879     }
880
881
882
883
884     private double getDouble(long a) {
885     if (unaligned) {
886         long x = unsafe.getLong(a);
887         return Double.longBitsToDouble(nativeByteOrder ? x : Bits.swap(x));
888     }
889     return Bits.getDouble(a, bigEndian);
890     }
891
892     public double getDouble() {
893     return getDouble(ix(nextGetIndex((1 << 3))));
894     }
895
896     public double getDouble(int i) {
897     return getDouble(ix(checkIndex(i, (1 << 3))));
898     }
899
900
901
902     private ByteBuffer JavaDoc putDouble(long a, double x) {
903
904     if (unaligned) {
905         long y = Double.doubleToRawLongBits(x);
906         unsafe.putLong(a, (nativeByteOrder ? y : Bits.swap(y)));
907     } else {
908         Bits.putDouble(a, x, bigEndian);
909     }
910     return this;
911
912
913
914     }
915
916     public ByteBuffer JavaDoc putDouble(double x) {
917
918     putDouble(ix(nextPutIndex((1 << 3))), x);
919     return this;
920
921
922
923     }
924
925     public ByteBuffer JavaDoc putDouble(int i, double x) {
926
927     putDouble(ix(checkIndex(i, (1 << 3))), x);
928     return this;
929
930
931
932     }
933
934     public DoubleBuffer JavaDoc asDoubleBuffer() {
935     int off = this.position();
936     int lim = this.limit();
937     assert (off <= lim);
938     int rem = (off <= lim ? lim - off : 0);
939
940     int size = rem >> 3;
941     if (!unaligned && ((address + off) % (1 << 3) != 0)) {
942         return (bigEndian
943             ? (DoubleBuffer JavaDoc)(new ByteBufferAsDoubleBufferB JavaDoc(this,
944                                        -1,
945                                        0,
946                                        size,
947                                        size,
948                                        off))
949             : (DoubleBuffer JavaDoc)(new ByteBufferAsDoubleBufferL JavaDoc(this,
950                                        -1,
951                                        0,
952                                        size,
953                                        size,
954                                        off)));
955     } else {
956         return (nativeByteOrder
957             ? (DoubleBuffer JavaDoc)(new DirectDoubleBufferU JavaDoc(this,
958                                  -1,
959                                  0,
960                                  size,
961                                  size,
962                                  off))
963             : (DoubleBuffer JavaDoc)(new DirectDoubleBufferS JavaDoc(this,
964                                  -1,
965                                  0,
966                                  size,
967                                  size,
968                                  off)));
969     }
970     }
971
972 }
973
Popular Tags