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

@ -89,4 +89,12 @@ class ArrayByteBuffer extends ByteBuffer {
+ " limit: " + limit
+ " capacity: " + capacity + ")";
}
@Override
public ByteBuffer duplicate() {
ByteBuffer b = new ArrayByteBuffer(array, arrayOffset, capacity, isReadOnly());
b.limit(this.limit());
b.position(this.position());
return b;
}
}

View File

@ -14,6 +14,7 @@ public abstract class Buffer {
protected int capacity;
protected int position;
protected int limit;
protected boolean readonly;
public final int limit() {
return limit;
@ -61,4 +62,8 @@ public abstract class Buffer {
position = 0;
return this;
}
public boolean isReadOnly() {
return readonly;
}
}

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();
}

View File

@ -102,4 +102,12 @@ class DirectByteBuffer extends ByteBuffer {
+ " limit: " + limit
+ " capacity: " + capacity + ")";
}
@Override
public ByteBuffer duplicate() {
ByteBuffer b = new DirectByteBuffer(address, capacity, isReadOnly());
b.limit(this.limit());
b.position(this.position());
return b;
}
}

View File

@ -46,6 +46,14 @@ class FixedArrayByteBuffer extends DirectByteBuffer {
return new FixedArrayByteBuffer
(address + position, array, arrayOffset + position, remaining(), true);
}
@Override
public ByteBuffer duplicate() {
ByteBuffer b = new FixedArrayByteBuffer(address, array, arrayOffset, capacity, isReadOnly());
b.limit(this.limit());
b.position(this.position());
return b;
}
public String toString() {
return "(FixedArrayByteBuffer with address: " + address