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,70 ----
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,138 ****
--- 140,155 ----
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,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]
}
--- 271,301 ----
// char
#if[rw]
public char getChar() {
! return unsafe.getCharUnaligned(hb, byteOffset(nextGetIndex(2)), bigEndian);
}
public char getChar(int i) {
! return unsafe.getCharUnaligned(hb, byteOffset(checkIndex(i, 2)), bigEndian);
}
#end[rw]
public $Type$Buffer putChar(char x) {
#if[rw]
! 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]
! unsafe.putCharUnaligned(hb, byteOffset(checkIndex(i, 2)), x, bigEndian);
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]
}
--- 322,352 ----
// short
#if[rw]
public short getShort() {
! return unsafe.getShortUnaligned(hb, byteOffset(nextGetIndex(2)), bigEndian);
}
public short getShort(int i) {
! return unsafe.getShortUnaligned(hb, byteOffset(checkIndex(i, 2)), bigEndian);
}
#end[rw]
public $Type$Buffer putShort(short x) {
#if[rw]
! 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]
! unsafe.putShortUnaligned(hb, byteOffset(checkIndex(i, 2)), x, bigEndian);
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]
}
--- 373,403 ----
// int
#if[rw]
public int getInt() {
! return unsafe.getIntUnaligned(hb, byteOffset(nextGetIndex(4)), bigEndian);
}
public int getInt(int i) {
! return unsafe.getIntUnaligned(hb, byteOffset(checkIndex(i, 4)), bigEndian);
}
#end[rw]
public $Type$Buffer putInt(int x) {
#if[rw]
! 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]
! unsafe.putIntUnaligned(hb, byteOffset(checkIndex(i, 4)), x, bigEndian);
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]
}
--- 424,454 ----
// long
#if[rw]
public long getLong() {
! return unsafe.getLongUnaligned(hb, byteOffset(nextGetIndex(8)), bigEndian);
}
public long getLong(int i) {
! return unsafe.getLongUnaligned(hb, byteOffset(checkIndex(i, 8)), bigEndian);
}
#end[rw]
public $Type$Buffer putLong(long x) {
#if[rw]
! 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]
! unsafe.putLongUnaligned(hb, byteOffset(checkIndex(i, 8)), x, bigEndian);
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]
}
--- 475,507 ----
// 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)), y, bigEndian);
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]
}
--- 528,560 ----
// 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, 4)), y, bigEndian);
return this;
#else[rw]
throw new ReadOnlyBufferException();
#end[rw]
}