Skip to content

Commit

Permalink
Merge pull request #4797 from Fenrur/buffer-double-float-le
Browse files Browse the repository at this point in the history
Buffer adding support for float le and double le
  • Loading branch information
vietj authored Aug 10, 2023
2 parents dc32b78 + e8488ad commit a4cf658
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 7 deletions.
42 changes: 42 additions & 0 deletions src/main/java/io/vertx/core/buffer/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,27 @@ default Object toJson() {
*/
double getDouble(int pos);

/**
* Gets a double at the specified absolute {@code index} in this buffer in Little Endian Byte Order.
*
* @throws IndexOutOfBoundsException if the specified {@code pos} is less than {@code 0} or {@code pos + 8} is greater than the length of the Buffer.
*/
double getDoubleLE(int pos);

/**
* Returns the {@code float} at position {@code pos} in the Buffer.
*
* @throws IndexOutOfBoundsException if the specified {@code pos} is less than {@code 0} or {@code pos + 4} is greater than the length of the Buffer.
*/
float getFloat(int pos);

/**
* Gets a float at the specified absolute {@code index} in this buffer in Little Endian Byte Order.
*
* @throws IndexOutOfBoundsException if the specified {@code pos} is less than {@code 0} or {@code pos + 4} is greater than the length of the Buffer.
*/
float getFloatLE(int pos);

/**
* Returns the {@code short} at position {@code pos} in the Buffer.
*
Expand Down Expand Up @@ -464,13 +478,27 @@ default Object toJson() {
@Fluent
Buffer appendFloat(float f);

/**
* Appends the specified unsigned {@code float} to the end of the Buffer in the Little Endian Byte Order.The buffer will expand as necessary to accommodate any bytes written.<p>
* Returns a reference to {@code this} so multiple operations can be appended together.
*/
@Fluent
Buffer appendFloatLE(float f);

/**
* Appends the specified {@code double} to the end of the Buffer. The buffer will expand as necessary to accommodate any bytes written.<p>
* Returns a reference to {@code this} so multiple operations can be appended together.
*/
@Fluent
Buffer appendDouble(double d);

/**
* Appends the specified unsigned {@code double} to the end of the Buffer in the Little Endian Byte Order.The buffer will expand as necessary to accommodate any bytes written.<p>
* Returns a reference to {@code this} so multiple operations can be appended together.
*/
@Fluent
Buffer appendDoubleLE(double d);

/**
* Appends the specified {@code String} to the end of the Buffer with the encoding as specified by {@code enc}.<p>
* The buffer will expand as necessary to accommodate any bytes written.<p>
Expand Down Expand Up @@ -564,13 +592,27 @@ default Object toJson() {
@Fluent
Buffer setDouble(int pos, double d);

/**
* Sets the {@code double} at position {@code pos} in the Buffer to the value {@code d} in the Little Endian Byte Order.<p>
* The buffer will expand as necessary to accommodate any value written.
*/
@Fluent
Buffer setDoubleLE(int pos, double d);

/**
* Sets the {@code float} at position {@code pos} in the Buffer to the value {@code f}.<p>
* The buffer will expand as necessary to accommodate any value written.
*/
@Fluent
Buffer setFloat(int pos, float f);

/**
* Sets the {@code float} at position {@code pos} in the Buffer to the value {@code f} in the Little Endian Byte Order.<p>
* The buffer will expand as necessary to accommodate any value written.
*/
@Fluent
Buffer setFloatLE(int pos, float f);

/**
* Sets the {@code short} at position {@code pos} in the Buffer to the value {@code s}.<p>
* The buffer will expand as necessary to accommodate any value written.
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/io/vertx/core/buffer/impl/BufferImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,21 @@ public double getDouble(int pos) {
return buffer.getDouble(pos);
}

public double getDoubleLE(int pos) {
checkUpperBound(pos, 8);
return buffer.getDoubleLE(pos);
}

public float getFloat(int pos) {
checkUpperBound(pos, 4);
return buffer.getFloat(pos);
}

public float getFloatLE(int pos) {
checkUpperBound(pos, 4);
return buffer.getFloatLE(pos);
}

public short getShort(int pos) {
checkUpperBound(pos, 2);
return buffer.getShort(pos);
Expand Down Expand Up @@ -338,11 +348,23 @@ public BufferImpl appendFloat(float f) {
return this;
}

@Override
public BufferImpl appendFloatLE(float f) {
buffer.writeFloatLE(f);
return this;
}

public BufferImpl appendDouble(double d) {
buffer.writeDouble(d);
return this;
}

@Override
public BufferImpl appendDoubleLE(double d) {
buffer.writeDoubleLE(d);
return this;
}

public BufferImpl appendString(String str, String enc) {
return append(str, Charset.forName(Objects.requireNonNull(enc)));
}
Expand Down Expand Up @@ -417,12 +439,26 @@ public BufferImpl setDouble(int pos, double d) {
return this;
}

@Override
public BufferImpl setDoubleLE(int pos, double d) {
ensureLength(pos + 8);
buffer.setDoubleLE(pos, d);
return this;
}

public BufferImpl setFloat(int pos, float f) {
ensureLength(pos + 4);
buffer.setFloat(pos, f);
return this;
}

@Override
public BufferImpl setFloatLE(int pos, float f) {
ensureLength(pos + 4);
buffer.setFloatLE(pos, f);
return this;
}

public BufferImpl setShort(int pos, short s) {
ensureLength(pos + 2);
buffer.setShort(pos, s);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/io/vertx/core/buffer/impl/BufferInternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,15 @@ static BufferInternal buffer(byte[] bytes) {
@Override
BufferInternal appendFloat(float f);

@Override
BufferInternal appendFloatLE(float f);

@Override
BufferInternal appendDouble(double d);

@Override
BufferInternal appendDoubleLE(double d);

@Override
BufferInternal appendString(String str, String enc);

Expand Down Expand Up @@ -149,9 +155,15 @@ static BufferInternal buffer(byte[] bytes) {
@Override
BufferInternal setDouble(int pos, double d);

@Override
BufferInternal setDoubleLE(int pos, double d);

@Override
BufferInternal setFloat(int pos, float f);

@Override
BufferInternal setFloatLE(int pos, float f);

@Override
BufferInternal setShort(int pos, short s);

Expand Down
51 changes: 44 additions & 7 deletions src/test/java/io/vertx/core/buffer/BufferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ public void testLE() {
checkBEAndLE(4, Buffer.buffer().appendInt(Integer.MAX_VALUE), Buffer.buffer().appendIntLE(Integer.MAX_VALUE));
checkBEAndLE(4, Buffer.buffer().appendUnsignedInt(Integer.MAX_VALUE), Buffer.buffer().appendUnsignedIntLE(Integer.MAX_VALUE));
checkBEAndLE(8, Buffer.buffer().appendLong(Long.MAX_VALUE), Buffer.buffer().appendLongLE(Long.MAX_VALUE));
checkBEAndLE(4, Buffer.buffer().appendFloat(Float.MAX_VALUE), Buffer.buffer().appendFloatLE(Float.MAX_VALUE));
checkBEAndLE(8, Buffer.buffer().appendDouble(Double.MAX_VALUE), Buffer.buffer().appendDoubleLE(Double.MAX_VALUE));
}

private void checkBEAndLE(int size, Buffer big, Buffer little) {
Expand Down Expand Up @@ -529,32 +531,67 @@ public void testGetLongLE() throws Exception {
testGetSetLong(true);
}

@Test
public void testGetFloat() throws Exception {
public void testGetFloat(boolean isLE) throws Exception {
int numFloats = 100;
Buffer b = Buffer.buffer(numFloats * 4);
for (int i = 0; i < numFloats; i++) {
b.setFloat(i * 4, i);
if (isLE) {
b.setFloatLE(i * 4, i);
} else {
b.setFloat(i * 4, i);
}

}

for (int i = 0; i < numFloats; i++) {
assertEquals((float) i, b.getFloat(i * 4), 0);
if (isLE) {
assertEquals((float) i, b.getFloatLE(i * 4), 0);
} else {
assertEquals((float) i, b.getFloat(i * 4), 0);
}
}
}

@Test
public void testGetDouble() throws Exception {
public void testGetFloat() throws Exception {
testGetFloat(false);
}

@Test
public void testGetFloatLE() throws Exception {
testGetFloat(true);
}

public void testGetDouble(boolean isLE) throws Exception {
int numDoubles = 100;
Buffer b = Buffer.buffer(numDoubles * 8);
for (int i = 0; i < numDoubles; i++) {
b.setDouble(i * 8, i);
if (isLE) {
b.setDoubleLE(i * 8, i);
} else {
b.setDouble(i * 8, i);
}
}

for (int i = 0; i < numDoubles; i++) {
assertEquals((double) i, b.getDouble(i * 8), 0);
if (isLE) {
assertEquals((double) i, b.getDoubleLE(i * 8), 0);
} else {
assertEquals((double) i, b.getDouble(i * 8), 0);
}
}
}

@Test
public void testGetDouble() throws Exception {
testGetDouble(false);
}

@Test
public void testGetDoubleLE() throws Exception {
testGetDouble(true);
}

private void testGetSetShort(boolean isLE) throws Exception {
int numShorts = 100;
Buffer b = Buffer.buffer(numShorts * 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ public void testDuplicate() {
assertEquals(3, duplicate.readerIndex());
assertEquals(0, byteBuf.readerIndex());
}

}

0 comments on commit a4cf658

Please sign in to comment.