added get/put float/double to ByteBuffers, as well as duplicate

This commit is contained in:
Luke Wahlmeier
2016-01-10 03:03:03 -07:00
parent c1df8b949c
commit 9d87e3b2a7
7 changed files with 212 additions and 27 deletions

View File

@ -14,10 +14,9 @@ public abstract class ByteBuffer
extends Buffer
implements Comparable<ByteBuffer>
{
private final boolean readOnly;
protected ByteBuffer(boolean readOnly) {
this.readOnly = readOnly;
this.readonly = readOnly;
}
public static ByteBuffer allocate(int capacity) {
@ -39,6 +38,8 @@ public abstract class ByteBuffer
public abstract ByteBuffer asReadOnlyBuffer();
public abstract ByteBuffer slice();
public abstract ByteBuffer duplicate();
protected abstract void doPut(int offset, byte val);
@ -122,7 +123,7 @@ public abstract class ByteBuffer
public ByteBuffer put(byte[] arr) {
return put(arr, 0, arr.length);
}
private void rawPutLong(int position, long val) {
doPut(position , (byte) ((val >> 56) & 0xff));
doPut(position + 1, (byte) ((val >> 48) & 0xff));
@ -145,6 +146,14 @@ public abstract class ByteBuffer
doPut(position , (byte) ((val >> 8) & 0xff));
doPut(position + 1, (byte) ((val ) & 0xff));
}
public ByteBuffer putDouble(int position, double val) {
return putLong(position, Double.doubleToRawLongBits(val));
}
public ByteBuffer putFloat(int position, float val) {
return putInt(position, Float.floatToRawIntBits(val));
}
public ByteBuffer putLong(int position, long val) {
checkPut(position, 8, true);
@ -169,6 +178,14 @@ public abstract class ByteBuffer
return this;
}
public ByteBuffer putDouble(double val) {
return putLong(Double.doubleToRawLongBits(val));
}
public ByteBuffer putFloat(float val) {
return putInt(Float.floatToRawIntBits(val));
}
public ByteBuffer putLong(long val) {
checkPut(position, 8, false);
@ -207,6 +224,14 @@ public abstract class ByteBuffer
public ByteBuffer get(byte[] dst) {
return get(dst, 0, dst.length);
}
public double getDouble(int position) {
return Double.longBitsToDouble(getLong(position));
}
public float getFloat(int position) {
return Float.intBitsToFloat(getInt(position));
}
public long getLong(int position) {
checkGet(position, 8, true);
@ -248,7 +273,15 @@ public abstract class ByteBuffer
return (short) (( ((int) (doGet(position ) & 0xFF)) << 8)
| (((int) (doGet(position + 1) & 0xFF)) ));
}
public double getDouble() {
return Double.longBitsToDouble(getLong());
}
public float getFloat() {
return Float.intBitsToFloat(getInt());
}
public long getLong() {
checkGet(position, 8, false);
@ -274,7 +307,7 @@ public abstract class ByteBuffer
}
protected void checkPut(int position, int amount, boolean absolute) {
if (readOnly) {
if (isReadOnly()) {
throw new ReadOnlyBufferException();
}