Skip to content

Commit

Permalink
Change ByteBuffer implementation of reading and writing doubles to av…
Browse files Browse the repository at this point in the history
…oid longs
  • Loading branch information
niloc132 committed Dec 11, 2019
1 parent b84986d commit a48b7c9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
23 changes: 19 additions & 4 deletions src/main/java/java/nio/ByteBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import elemental2.core.ArrayBuffer;
import elemental2.core.ArrayBufferView;
import elemental2.core.Float64Array;
import elemental2.core.Int8Array;
import org.gwtproject.nio.HasArrayBufferView;
import org.gwtproject.nio.TypedArrayHelper;
Expand Down Expand Up @@ -376,7 +377,14 @@ public final char getChar (int index) {
* @exception BufferUnderflowException if the position is greater than {@code limit - 8}.
*/
public final double getDouble () {
return Numbers.longBitsToDouble(getLong());
int newPosition = position() + 8;
//if (newPosition > limit) {
//throw new BufferUnderflowException();
//}

double value = getDouble(position());
position = newPosition;
return value;
}

/** Returns the double at the specified index.
Expand All @@ -388,7 +396,7 @@ public final double getDouble () {
* @exception IndexOutOfBoundsException if {@code index} is invalid.
*/
public final double getDouble (int index) {
return Numbers.longBitsToDouble(getLong(index));
return Numbers.readDoubleBytes(byteArray, index, order);
}

/** Returns the float at the current position and increases the position by 4.
Expand Down Expand Up @@ -723,7 +731,13 @@ public final ByteBuffer putChar (int index, char value) {
* @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
*/
public ByteBuffer putDouble (double value) {
return putLong(Numbers.doubleToRawLongBits(value));
int newPosition = position() + 8;
//if (newPosition > limit) {
//throw new BufferOverflowException();
//}
putDouble(position(), value);
position = newPosition;
return this;
}

/** Writes the given double to the specified index of this buffer.
Expand All @@ -738,7 +752,8 @@ public ByteBuffer putDouble (double value) {
* @exception ReadOnlyBufferException if no changes may be made to the contents of this buffer.
*/
public ByteBuffer putDouble (int index, double value) {
return putLong(index, Numbers.doubleToRawLongBits(value));
Numbers.writeDoubleBytes(byteArray, index, value, order);
return this;
}

/** Writes the given float to the current position and increases the position by 4.
Expand Down
28 changes: 26 additions & 2 deletions src/main/java/java/nio/Numbers.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public static final float intBitsToFloat(int i) {
}

public static final double longBitsToDouble(long i) {
throw new RuntimeException("longBitsToDouble NYI");
return Double.longBitsToDouble(i);
}

public static final long doubleToRawLongBits(double i) {
throw new RuntimeException("doubleToRawLongBits NYI");
return Double.doubleToLongBits(i);
}

// TODO(jgw): Ugly hack to avoid longs.
Expand All @@ -68,4 +68,28 @@ public static final void setLoInt(int i) {
public static final void setHiInt(int i) {
wia.setAt(1, (double)i);
}

/**
* Helper which writes a double value to the common buffer, then copies the specific
* bytes back to the specified array at the given offset.
*/
public static final void writeDoubleBytes(Int8Array byteArray, int offset, double value, ByteOrder order) {
//TODO compare order with nativeOrder, if they don't match then copy data backward
wda.setAt(0, value);
for (int i = 0; i < 8; i++) {
byteArray.setAt(i + offset, wba.getAt(i));
}
}

/**
* Helper which copies the specified bytes to the common buffer, then reads out
* the double value that those bytes represent.
*/
public static final double readDoubleBytes(Int8Array byteArray, int offset, ByteOrder order) {
//TODO compare order with nativeOrder, if they don't match then copy data backward
for (int i = 0; i < 8; i++) {
wba.setAt(i, byteArray.getAt(offset + i));
}
return wda.getAt(0);
}
}

0 comments on commit a48b7c9

Please sign in to comment.