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

Print this page

        

*** 25,34 **** --- 25,35 ---- #warn This file is preprocessed before being compiled package java.nio; + import sun.misc.Unsafe; /** #if[rw] * A read/write Heap$Type$Buffer. #else[rw]
*** 50,59 **** --- 51,73 ---- protected final $type$[] hb; protected final int offset; #end[rw] */ + #if[byte] + + // Cached unsafe-access object + protected static final Unsafe unsafe = Bits.unsafe(); + + // Cached array base offset + private static final long arrayBaseOffset = unsafe.arrayBaseOffset($type$[].class); + + // Cached unaligned-access capability + protected static final boolean unaligned = Bits.unaligned(); + + #end[byte] + Heap$Type$Buffer$RW$(int cap, int lim) { // package-private #if[rw] super(-1, 0, lim, cap, new $type$[cap], 0); /* hb = new $type$[cap];
*** 129,138 **** --- 143,158 ---- protected int ix(int i) { return i + offset; } + #if[byte] + protected long byteOffset(long i) { + return arrayBaseOffset + i + offset; + } + #end[byte] + public $type$ get() { return hb[ix(nextGetIndex())]; } public $type$ get(int i) {
*** 233,244 **** #else[rw] throw new ReadOnlyBufferException(); #end[rw] } - - #if[byte] byte _get(int i) { // package-private return hb[i]; } --- 253,262 ----
*** 254,284 **** // char #if[rw] public char getChar() { ! return Bits.getChar(this, ix(nextGetIndex(2)), bigEndian); } public char getChar(int i) { ! return Bits.getChar(this, ix(checkIndex(i, 2)), bigEndian); } #end[rw] public $Type$Buffer putChar(char x) { #if[rw] ! Bits.putChar(this, ix(nextPutIndex(2)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] } public $Type$Buffer putChar(int i, char x) { #if[rw] ! Bits.putChar(this, ix(checkIndex(i, 2)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] } --- 272,304 ---- // char #if[rw] public char getChar() { ! return getChar(nextGetIndex(2)); } public char getChar(int i) { ! return unsafe.getCharUnaligned(hb, byteOffset(checkIndex(i, 2)), bigEndian); } #end[rw] public $Type$Buffer putChar(char x) { #if[rw] ! putChar(nextPutIndex(2), x); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] } public $Type$Buffer putChar(int i, char x) { #if[rw] ! char y = (x); ! unsafe.putCharUnaligned(hb, byteOffset(checkIndex(i, 2)), ! nativeByteOrder ? y : Bits.swap(y)); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] }
*** 305,335 **** // short #if[rw] public short getShort() { ! return Bits.getShort(this, ix(nextGetIndex(2)), bigEndian); } public short getShort(int i) { ! return Bits.getShort(this, ix(checkIndex(i, 2)), bigEndian); } #end[rw] public $Type$Buffer putShort(short x) { #if[rw] ! Bits.putShort(this, ix(nextPutIndex(2)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] } public $Type$Buffer putShort(int i, short x) { #if[rw] ! Bits.putShort(this, ix(checkIndex(i, 2)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] } --- 325,357 ---- // short #if[rw] public short getShort() { ! return getShort(nextGetIndex(2)); } public short getShort(int i) { ! return unsafe.getShortUnaligned(hb, byteOffset(checkIndex(i, 2)), bigEndian); } #end[rw] public $Type$Buffer putShort(short x) { #if[rw] ! putShort(nextPutIndex(2), x); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] } public $Type$Buffer putShort(int i, short x) { #if[rw] ! short y = (x); ! unsafe.putShortUnaligned(hb, byteOffset(checkIndex(i, 2)), ! nativeByteOrder ? y : Bits.swap(y)); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] }
*** 356,386 **** // int #if[rw] public int getInt() { ! return Bits.getInt(this, ix(nextGetIndex(4)), bigEndian); } public int getInt(int i) { ! return Bits.getInt(this, ix(checkIndex(i, 4)), bigEndian); } #end[rw] public $Type$Buffer putInt(int x) { #if[rw] ! Bits.putInt(this, ix(nextPutIndex(4)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] } public $Type$Buffer putInt(int i, int x) { #if[rw] ! Bits.putInt(this, ix(checkIndex(i, 4)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] } --- 378,410 ---- // int #if[rw] public int getInt() { ! return getInt(nextGetIndex(4)); } public int getInt(int i) { ! return unsafe.getIntUnaligned(hb, byteOffset(checkIndex(i, 4)), bigEndian); } #end[rw] public $Type$Buffer putInt(int x) { #if[rw] ! putInt(nextPutIndex(4), x); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] } public $Type$Buffer putInt(int i, int x) { #if[rw] ! int y = (x); ! unsafe.putIntUnaligned(hb, byteOffset(checkIndex(i, 4)), ! nativeByteOrder ? y : Bits.swap(y)); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] }
*** 407,437 **** // long #if[rw] public long getLong() { ! return Bits.getLong(this, ix(nextGetIndex(8)), bigEndian); } public long getLong(int i) { ! return Bits.getLong(this, ix(checkIndex(i, 8)), bigEndian); } #end[rw] public $Type$Buffer putLong(long x) { #if[rw] ! Bits.putLong(this, ix(nextPutIndex(8)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] } public $Type$Buffer putLong(int i, long x) { #if[rw] ! Bits.putLong(this, ix(checkIndex(i, 8)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] } --- 431,463 ---- // long #if[rw] public long getLong() { ! return getLong(nextGetIndex(8)); } public long getLong(int i) { ! return unsafe.getLongUnaligned(hb, byteOffset(checkIndex(i, 8)), bigEndian); } #end[rw] public $Type$Buffer putLong(long x) { #if[rw] ! putLong(nextPutIndex(8), x); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] } public $Type$Buffer putLong(int i, long x) { #if[rw] ! long y = (x); ! unsafe.putLongUnaligned(hb, byteOffset(checkIndex(i, 8)), ! nativeByteOrder ? y : Bits.swap(y)); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] }
*** 458,488 **** // float #if[rw] public float getFloat() { ! return Bits.getFloat(this, ix(nextGetIndex(4)), bigEndian); } public float getFloat(int i) { ! return Bits.getFloat(this, ix(checkIndex(i, 4)), bigEndian); } #end[rw] public $Type$Buffer putFloat(float x) { #if[rw] ! Bits.putFloat(this, ix(nextPutIndex(4)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] } public $Type$Buffer putFloat(int i, float x) { #if[rw] ! Bits.putFloat(this, ix(checkIndex(i, 4)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] } --- 484,517 ---- // float #if[rw] public float getFloat() { ! return getFloat(nextGetIndex(4)); } public float getFloat(int i) { ! int x = unsafe.getIntUnaligned(hb, byteOffset(checkIndex(i, 4)), bigEndian); ! return Float.intBitsToFloat(x); } #end[rw] public $Type$Buffer putFloat(float x) { #if[rw] ! putFloat(nextPutIndex(4), x); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] } public $Type$Buffer putFloat(int i, float x) { #if[rw] ! int y = Float.floatToRawIntBits(x); ! unsafe.putIntUnaligned(hb, byteOffset(checkIndex(i, 4)), ! nativeByteOrder ? y : Bits.swap(y)); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] }
*** 509,539 **** // double #if[rw] public double getDouble() { ! return Bits.getDouble(this, ix(nextGetIndex(8)), bigEndian); } public double getDouble(int i) { ! return Bits.getDouble(this, ix(checkIndex(i, 8)), bigEndian); } #end[rw] public $Type$Buffer putDouble(double x) { #if[rw] ! Bits.putDouble(this, ix(nextPutIndex(8)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] } public $Type$Buffer putDouble(int i, double x) { #if[rw] ! Bits.putDouble(this, ix(checkIndex(i, 8)), x, bigEndian); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] } --- 538,571 ---- // double #if[rw] public double getDouble() { ! return getDouble(nextGetIndex(8)); } public double getDouble(int i) { ! long x = unsafe.getLongUnaligned(hb, byteOffset(checkIndex(i, 8)), bigEndian); ! return Double.longBitsToDouble(x); } #end[rw] public $Type$Buffer putDouble(double x) { #if[rw] ! putDouble(nextPutIndex(8), x); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] } public $Type$Buffer putDouble(int i, double x) { #if[rw] ! long y = Double.doubleToRawLongBits(x); ! unsafe.putLongUnaligned(hb, byteOffset(checkIndex(i, 8)), ! nativeByteOrder ? y : Bits.swap(y)); return this; #else[rw] throw new ReadOnlyBufferException(); #end[rw] }
*** 558,567 **** --- 590,600 ---- #end[byte] + #if[char] String toString(int start, int end) { // package-private try { return new String(hb, start + offset, end - start);