--- old/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template 2015-03-02 18:37:41.303858525 +0000 +++ new/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template 2015-03-02 18:37:41.177862418 +0000 @@ -27,6 +27,7 @@ package java.nio; +import sun.misc.Unsafe; /** #if[rw] @@ -52,6 +53,19 @@ #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); @@ -131,6 +145,12 @@ return i + offset; } +#if[byte] + protected long byteOffset(long i) { + return arrayBaseOffset + i + offset; + } +#end[byte] + public $type$ get() { return hb[ix(nextGetIndex())]; } @@ -235,8 +255,6 @@ #end[rw] } - - #if[byte] byte _get(int i) { // package-private @@ -256,18 +274,18 @@ #if[rw] public char getChar() { - return Bits.getChar(this, ix(nextGetIndex(2)), bigEndian); + return getChar(nextGetIndex(2)); } public char getChar(int i) { - return Bits.getChar(this, ix(checkIndex(i, 2)), bigEndian); + return unsafe.getCharUnaligned(hb, byteOffset(checkIndex(i, 2)), bigEndian); } #end[rw] public $Type$Buffer putChar(char x) { #if[rw] - Bits.putChar(this, ix(nextPutIndex(2)), x, bigEndian); + putChar(nextPutIndex(2), x); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -276,7 +294,9 @@ public $Type$Buffer putChar(int i, char x) { #if[rw] - Bits.putChar(this, ix(checkIndex(i, 2)), x, bigEndian); + char y = (x); + unsafe.putCharUnaligned(hb, byteOffset(checkIndex(i, 2)), + nativeByteOrder ? y : Bits.swap(y)); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -307,18 +327,18 @@ #if[rw] public short getShort() { - return Bits.getShort(this, ix(nextGetIndex(2)), bigEndian); + return getShort(nextGetIndex(2)); } public short getShort(int i) { - return Bits.getShort(this, ix(checkIndex(i, 2)), bigEndian); + return unsafe.getShortUnaligned(hb, byteOffset(checkIndex(i, 2)), bigEndian); } #end[rw] public $Type$Buffer putShort(short x) { #if[rw] - Bits.putShort(this, ix(nextPutIndex(2)), x, bigEndian); + putShort(nextPutIndex(2), x); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -327,7 +347,9 @@ public $Type$Buffer putShort(int i, short x) { #if[rw] - Bits.putShort(this, ix(checkIndex(i, 2)), x, bigEndian); + short y = (x); + unsafe.putShortUnaligned(hb, byteOffset(checkIndex(i, 2)), + nativeByteOrder ? y : Bits.swap(y)); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -358,18 +380,18 @@ #if[rw] public int getInt() { - return Bits.getInt(this, ix(nextGetIndex(4)), bigEndian); + return getInt(nextGetIndex(4)); } public int getInt(int i) { - return Bits.getInt(this, ix(checkIndex(i, 4)), bigEndian); + return unsafe.getIntUnaligned(hb, byteOffset(checkIndex(i, 4)), bigEndian); } #end[rw] public $Type$Buffer putInt(int x) { #if[rw] - Bits.putInt(this, ix(nextPutIndex(4)), x, bigEndian); + putInt(nextPutIndex(4), x); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -378,7 +400,9 @@ public $Type$Buffer putInt(int i, int x) { #if[rw] - Bits.putInt(this, ix(checkIndex(i, 4)), x, bigEndian); + int y = (x); + unsafe.putIntUnaligned(hb, byteOffset(checkIndex(i, 4)), + nativeByteOrder ? y : Bits.swap(y)); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -409,18 +433,18 @@ #if[rw] public long getLong() { - return Bits.getLong(this, ix(nextGetIndex(8)), bigEndian); + return getLong(nextGetIndex(8)); } public long getLong(int i) { - return Bits.getLong(this, ix(checkIndex(i, 8)), bigEndian); + return unsafe.getLongUnaligned(hb, byteOffset(checkIndex(i, 8)), bigEndian); } #end[rw] public $Type$Buffer putLong(long x) { #if[rw] - Bits.putLong(this, ix(nextPutIndex(8)), x, bigEndian); + putLong(nextPutIndex(8), x); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -429,7 +453,9 @@ public $Type$Buffer putLong(int i, long x) { #if[rw] - Bits.putLong(this, ix(checkIndex(i, 8)), x, bigEndian); + long y = (x); + unsafe.putLongUnaligned(hb, byteOffset(checkIndex(i, 8)), + nativeByteOrder ? y : Bits.swap(y)); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -460,18 +486,19 @@ #if[rw] public float getFloat() { - return Bits.getFloat(this, ix(nextGetIndex(4)), bigEndian); + return getFloat(nextGetIndex(4)); } public float getFloat(int i) { - return Bits.getFloat(this, ix(checkIndex(i, 4)), bigEndian); + int x = unsafe.getIntUnaligned(hb, byteOffset(checkIndex(i, 4)), bigEndian); + return Float.intBitsToFloat(x); } #end[rw] public $Type$Buffer putFloat(float x) { #if[rw] - Bits.putFloat(this, ix(nextPutIndex(4)), x, bigEndian); + putFloat(nextPutIndex(4), x); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -480,7 +507,9 @@ public $Type$Buffer putFloat(int i, float x) { #if[rw] - Bits.putFloat(this, ix(checkIndex(i, 4)), x, bigEndian); + int y = Float.floatToRawIntBits(x); + unsafe.putIntUnaligned(hb, byteOffset(checkIndex(i, 4)), + nativeByteOrder ? y : Bits.swap(y)); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -511,18 +540,19 @@ #if[rw] public double getDouble() { - return Bits.getDouble(this, ix(nextGetIndex(8)), bigEndian); + return getDouble(nextGetIndex(8)); } public double getDouble(int i) { - return Bits.getDouble(this, ix(checkIndex(i, 8)), bigEndian); + long x = unsafe.getLongUnaligned(hb, byteOffset(checkIndex(i, 8)), bigEndian); + return Double.longBitsToDouble(x); } #end[rw] public $Type$Buffer putDouble(double x) { #if[rw] - Bits.putDouble(this, ix(nextPutIndex(8)), x, bigEndian); + putDouble(nextPutIndex(8), x); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -531,7 +561,9 @@ public $Type$Buffer putDouble(int i, double x) { #if[rw] - Bits.putDouble(this, ix(checkIndex(i, 8)), x, bigEndian); + long y = Double.doubleToRawLongBits(x); + unsafe.putLongUnaligned(hb, byteOffset(checkIndex(i, 8)), + nativeByteOrder ? y : Bits.swap(y)); return this; #else[rw] throw new ReadOnlyBufferException(); @@ -560,6 +592,7 @@ #end[byte] + #if[char] String toString(int start, int end) { // package-private