< prev index next >

src/java.base/share/classes/java/nio/Direct-X-Buffer.java.template

Print this page




  33 import sun.misc.VM;
  34 import sun.nio.ch.DirectBuffer;
  35 
  36 
  37 class Direct$Type$Buffer$RW$$BO$
  38 #if[rw]
  39     extends {#if[byte]?Mapped$Type$Buffer:$Type$Buffer}
  40 #else[rw]
  41     extends Direct$Type$Buffer$BO$
  42 #end[rw]
  43     implements DirectBuffer
  44 {
  45 
  46 #if[rw]
  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($type$[].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     // An object attached to this buffer. If this buffer is a view of another
  62     // buffer then we use this field to keep a reference to that buffer to
  63     // ensure that its memory isn't freed before we are done with it.
  64     private final Object att;
  65 
  66     public Object attachment() {
  67         return att;
  68     }
  69 
  70 #if[byte]
  71 
  72     private static class Deallocator


 261 
 262     public $Type$Buffer get($type$[] dst, int offset, int length) {
 263 #if[rw]
 264         if ((length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
 265             checkBounds(offset, length, dst.length);
 266             int pos = position();
 267             int lim = limit();
 268             assert (pos <= lim);
 269             int rem = (pos <= lim ? lim - pos : 0);
 270             if (length > rem)
 271                 throw new BufferUnderflowException();
 272 
 273 #if[!byte]
 274             if (order() != ByteOrder.nativeOrder())
 275                 Bits.copyTo$Memtype$Array(ix(pos), dst,
 276                                           offset << $LG_BYTES_PER_VALUE$,
 277                                           length << $LG_BYTES_PER_VALUE$);
 278             else
 279 #end[!byte]
 280                 Bits.copyToArray(ix(pos), dst, arrayBaseOffset,
 281                                  offset << $LG_BYTES_PER_VALUE$,
 282                                  length << $LG_BYTES_PER_VALUE$);
 283             position(pos + length);
 284         } else {
 285             super.get(dst, offset, length);
 286         }
 287         return this;
 288 #else[rw]
 289         throw new ReadOnlyBufferException();
 290 #end[rw]
 291     }
 292 
 293 #end[rw]
 294 
 295     public $Type$Buffer put($type$ x) {
 296 #if[rw]
 297         unsafe.put$Swaptype$(ix(nextPutIndex()), $swap$($toBits$(x)));
 298         return this;
 299 #else[rw]
 300         throw new ReadOnlyBufferException();
 301 #end[rw]


 351 #end[rw]
 352     }
 353 
 354     public $Type$Buffer put($type$[] src, int offset, int length) {
 355 #if[rw]
 356         if ((length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
 357             checkBounds(offset, length, src.length);
 358             int pos = position();
 359             int lim = limit();
 360             assert (pos <= lim);
 361             int rem = (pos <= lim ? lim - pos : 0);
 362             if (length > rem)
 363                 throw new BufferOverflowException();
 364 
 365 #if[!byte]
 366             if (order() != ByteOrder.nativeOrder())
 367                 Bits.copyFrom$Memtype$Array(src, offset << $LG_BYTES_PER_VALUE$,
 368                                             ix(pos), length << $LG_BYTES_PER_VALUE$);
 369             else
 370 #end[!byte]
 371                 Bits.copyFromArray(src, arrayBaseOffset, offset << $LG_BYTES_PER_VALUE$,
 372                                    ix(pos), length << $LG_BYTES_PER_VALUE$);
 373             position(pos + length);
 374         } else {
 375             super.put(src, offset, length);
 376         }
 377         return this;
 378 #else[rw]
 379         throw new ReadOnlyBufferException();
 380 #end[rw]
 381     }
 382 
 383     public $Type$Buffer compact() {
 384 #if[rw]
 385         int pos = position();
 386         int lim = limit();
 387         assert (pos <= lim);
 388         int rem = (pos <= lim ? lim - pos : 0);
 389 
 390         unsafe.copyMemory(ix(pos), ix(0), rem << $LG_BYTES_PER_VALUE$);
 391         position(rem);




  33 import sun.misc.VM;
  34 import sun.nio.ch.DirectBuffer;
  35 
  36 
  37 class Direct$Type$Buffer$RW$$BO$
  38 #if[rw]
  39     extends {#if[byte]?Mapped$Type$Buffer:$Type$Buffer}
  40 #else[rw]
  41     extends Direct$Type$Buffer$BO$
  42 #end[rw]
  43     implements DirectBuffer
  44 {
  45 
  46 #if[rw]
  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($type$[].class);
  53     private static final long arrayIndexScale = (long)unsafe.arrayIndexScale($type$[].class);
  54 
  55     // Cached unaligned-access capability
  56     protected static final boolean unaligned = Bits.unaligned();
  57 
  58     // Base address, used in all indexing calculations
  59     // NOTE: moved up to Buffer.java for speed in JNI GetDirectBufferAddress
  60     //    protected long address;
  61 
  62     // An object attached to this buffer. If this buffer is a view of another
  63     // buffer then we use this field to keep a reference to that buffer to
  64     // ensure that its memory isn't freed before we are done with it.
  65     private final Object att;
  66 
  67     public Object attachment() {
  68         return att;
  69     }
  70 
  71 #if[byte]
  72 
  73     private static class Deallocator


 262 
 263     public $Type$Buffer get($type$[] dst, int offset, int length) {
 264 #if[rw]
 265         if ((length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
 266             checkBounds(offset, length, dst.length);
 267             int pos = position();
 268             int lim = limit();
 269             assert (pos <= lim);
 270             int rem = (pos <= lim ? lim - pos : 0);
 271             if (length > rem)
 272                 throw new BufferUnderflowException();
 273 
 274 #if[!byte]
 275             if (order() != ByteOrder.nativeOrder())
 276                 Bits.copyTo$Memtype$Array(ix(pos), dst,
 277                                           offset << $LG_BYTES_PER_VALUE$,
 278                                           length << $LG_BYTES_PER_VALUE$);
 279             else
 280 #end[!byte]
 281                 Bits.copyToArray(ix(pos), dst, arrayBaseOffset,
 282                                  offset * arrayIndexScale,
 283                                  length << $LG_BYTES_PER_VALUE$);
 284             position(pos + length);
 285         } else {
 286             super.get(dst, offset, length);
 287         }
 288         return this;
 289 #else[rw]
 290         throw new ReadOnlyBufferException();
 291 #end[rw]
 292     }
 293 
 294 #end[rw]
 295 
 296     public $Type$Buffer put($type$ x) {
 297 #if[rw]
 298         unsafe.put$Swaptype$(ix(nextPutIndex()), $swap$($toBits$(x)));
 299         return this;
 300 #else[rw]
 301         throw new ReadOnlyBufferException();
 302 #end[rw]


 352 #end[rw]
 353     }
 354 
 355     public $Type$Buffer put($type$[] src, int offset, int length) {
 356 #if[rw]
 357         if ((length << $LG_BYTES_PER_VALUE$) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
 358             checkBounds(offset, length, src.length);
 359             int pos = position();
 360             int lim = limit();
 361             assert (pos <= lim);
 362             int rem = (pos <= lim ? lim - pos : 0);
 363             if (length > rem)
 364                 throw new BufferOverflowException();
 365 
 366 #if[!byte]
 367             if (order() != ByteOrder.nativeOrder())
 368                 Bits.copyFrom$Memtype$Array(src, offset << $LG_BYTES_PER_VALUE$,
 369                                             ix(pos), length << $LG_BYTES_PER_VALUE$);
 370             else
 371 #end[!byte]
 372                 Bits.copyFromArray(src, arrayBaseOffset, offset * arrayIndexScale,
 373                                    ix(pos), length << $LG_BYTES_PER_VALUE$);
 374             position(pos + length);
 375         } else {
 376             super.put(src, offset, length);
 377         }
 378         return this;
 379 #else[rw]
 380         throw new ReadOnlyBufferException();
 381 #end[rw]
 382     }
 383 
 384     public $Type$Buffer compact() {
 385 #if[rw]
 386         int pos = position();
 387         int lim = limit();
 388         assert (pos <= lim);
 389         int rem = (pos <= lim ? lim - pos : 0);
 390 
 391         unsafe.copyMemory(ix(pos), ix(0), rem << $LG_BYTES_PER_VALUE$);
 392         position(rem);


< prev index next >