src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template
Print this page
@@ -25,10 +25,11 @@
#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,10 +51,20 @@
protected final $type$[] hb;
protected final int offset;
#end[rw]
*/
+#if[byte]
+
+ // Cached unsafe-access object
+ private static final Unsafe unsafe = Bits.unsafe();
+
+ // Cached array base offset
+ private static final long arrayBaseOffset = unsafe.arrayBaseOffset($type$[].class);
+
+#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,10 +140,16 @@
protected int ix(int i) {
return i + offset;
}
+#if[byte]
+ private long byteOffset(long i) {
+ return arrayBaseOffset + i + offset;
+ }
+#end[byte]
+
public $type$ get() {
return hb[ix(nextGetIndex())];
}
public $type$ get(int i) {
@@ -254,31 +271,31 @@
// char
#if[rw]
public char getChar() {
- return Bits.getChar(this, ix(nextGetIndex(2)), bigEndian);
+ return unsafe.getCharUnaligned(hb, byteOffset(nextGetIndex(2)), bigEndian);
}
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);
+ unsafe.putCharUnaligned(hb, byteOffset(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);
+ unsafe.putCharUnaligned(hb, byteOffset(checkIndex(i, 2)), x, bigEndian);
return this;
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}
@@ -305,31 +322,31 @@
// short
#if[rw]
public short getShort() {
- return Bits.getShort(this, ix(nextGetIndex(2)), bigEndian);
+ return unsafe.getShortUnaligned(hb, byteOffset(nextGetIndex(2)), bigEndian);
}
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);
+ unsafe.putShortUnaligned(hb, byteOffset(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);
+ unsafe.putShortUnaligned(hb, byteOffset(checkIndex(i, 2)), x, bigEndian);
return this;
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}
@@ -356,31 +373,31 @@
// int
#if[rw]
public int getInt() {
- return Bits.getInt(this, ix(nextGetIndex(4)), bigEndian);
+ return unsafe.getIntUnaligned(hb, byteOffset(nextGetIndex(4)), bigEndian);
}
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);
+ unsafe.putIntUnaligned(hb, byteOffset(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);
+ unsafe.putIntUnaligned(hb, byteOffset(checkIndex(i, 4)), x, bigEndian);
return this;
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}
@@ -407,31 +424,31 @@
// long
#if[rw]
public long getLong() {
- return Bits.getLong(this, ix(nextGetIndex(8)), bigEndian);
+ return unsafe.getLongUnaligned(hb, byteOffset(nextGetIndex(8)), bigEndian);
}
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);
+ unsafe.putLongUnaligned(hb, byteOffset(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);
+ unsafe.putLongUnaligned(hb, byteOffset(checkIndex(i, 8)), x, bigEndian);
return this;
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}
@@ -458,31 +475,33 @@
// float
#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();
#end[rw]
}
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)), y, bigEndian);
return this;
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}
@@ -509,31 +528,33 @@
// double
#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();
#end[rw]
}
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, 4)), y, bigEndian);
return this;
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}