From e966ee0371d884e1d9d2beba20768a2901f2e7dc Mon Sep 17 00:00:00 2001 From: Fenrur Date: Wed, 2 Aug 2023 17:49:47 +0200 Subject: [PATCH 1/4] Adding support to Buffer -> float LE and double LE --- .../java/io/vertx/core/buffer/Buffer.java | 42 +++++++++++++++ .../io/vertx/core/buffer/impl/BufferImpl.java | 36 +++++++++++++ .../core/buffer/impl/BufferInternal.java | 12 +++++ .../java/io/vertx/core/buffer/BufferTest.java | 51 ++++++++++++++++--- 4 files changed, 134 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/vertx/core/buffer/Buffer.java b/src/main/java/io/vertx/core/buffer/Buffer.java index 727382bfff8..f51cf7929bc 100644 --- a/src/main/java/io/vertx/core/buffer/Buffer.java +++ b/src/main/java/io/vertx/core/buffer/Buffer.java @@ -190,6 +190,13 @@ 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. * @@ -197,6 +204,13 @@ default Object toJson() { */ 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. * @@ -464,6 +478,13 @@ 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.

+ * 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.

* Returns a reference to {@code this} so multiple operations can be appended together. @@ -471,6 +492,13 @@ default Object toJson() { @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.

+ * 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}.

* The buffer will expand as necessary to accommodate any bytes written.

@@ -564,6 +592,13 @@ 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.

+ * 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}.

* The buffer will expand as necessary to accommodate any value written. @@ -571,6 +606,13 @@ default Object toJson() { @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.

+ * 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}.

* The buffer will expand as necessary to accommodate any value written. diff --git a/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java b/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java index f6766441b10..ec008857737 100644 --- a/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java +++ b/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java @@ -136,11 +136,21 @@ public double getDouble(int pos) { return buffer.getDouble(pos); } + public double getDoubleLE(int pos) { + checkUpperBound(pos, 4); + 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); @@ -338,11 +348,23 @@ public BufferImpl appendFloat(float f) { return this; } + @Override + public Buffer appendFloatLE(float f) { + buffer.writeFloatLE(f); + return this; + } + public BufferImpl appendDouble(double d) { buffer.writeDouble(d); return this; } + @Override + public Buffer appendDoubleLE(double d) { + buffer.writeDoubleLE(d); + return this; + } + public BufferImpl appendString(String str, String enc) { return append(str, Charset.forName(Objects.requireNonNull(enc))); } @@ -417,12 +439,26 @@ public BufferImpl setDouble(int pos, double d) { return this; } + @Override + public Buffer 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 Buffer 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); diff --git a/src/main/java/io/vertx/core/buffer/impl/BufferInternal.java b/src/main/java/io/vertx/core/buffer/impl/BufferInternal.java index e698a5b6d69..0efefd8eec2 100644 --- a/src/main/java/io/vertx/core/buffer/impl/BufferInternal.java +++ b/src/main/java/io/vertx/core/buffer/impl/BufferInternal.java @@ -107,9 +107,15 @@ static BufferInternal buffer(byte[] bytes) { @Override BufferInternal appendFloat(float f); + @Override + Buffer appendFloatLE(float f); + @Override BufferInternal appendDouble(double d); + @Override + Buffer appendDoubleLE(double d); + @Override BufferInternal appendString(String str, String enc); @@ -149,9 +155,15 @@ static BufferInternal buffer(byte[] bytes) { @Override BufferInternal setDouble(int pos, double d); + @Override + Buffer setDoubleLE(int pos, double d); + @Override BufferInternal setFloat(int pos, float f); + @Override + Buffer setFloatLE(int pos, float f); + @Override BufferInternal setShort(int pos, short s); diff --git a/src/test/java/io/vertx/core/buffer/BufferTest.java b/src/test/java/io/vertx/core/buffer/BufferTest.java index 2cba92938f7..65972e31c76 100644 --- a/src/test/java/io/vertx/core/buffer/BufferTest.java +++ b/src/test/java/io/vertx/core/buffer/BufferTest.java @@ -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) { @@ -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); From 2520277d06e834423158c9a1214be2c05b0cbade Mon Sep 17 00:00:00 2001 From: Fenrur Date: Wed, 2 Aug 2023 17:52:56 +0200 Subject: [PATCH 2/4] Adding support to Buffer -> float LE and double LE --- src/test/java/io/vertx/core/buffer/impl/VertxBufferTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/io/vertx/core/buffer/impl/VertxBufferTest.java b/src/test/java/io/vertx/core/buffer/impl/VertxBufferTest.java index 2cee9b955b9..5ece7583cb6 100644 --- a/src/test/java/io/vertx/core/buffer/impl/VertxBufferTest.java +++ b/src/test/java/io/vertx/core/buffer/impl/VertxBufferTest.java @@ -73,4 +73,5 @@ public void testDuplicate() { assertEquals(3, duplicate.readerIndex()); assertEquals(0, byteBuf.readerIndex()); } + } From ced5ae3c034717ba5d610284c28b3e748737c32a Mon Sep 17 00:00:00 2001 From: Fenrur Date: Thu, 3 Aug 2023 11:43:03 +0200 Subject: [PATCH 3/4] Fix into BufferImpl return signature of new methods Buffer -> BufferImpl Fix into BufferInternal return signature of new methods Buffer -> BufferInternal --- src/main/java/io/vertx/core/buffer/impl/BufferImpl.java | 8 ++++---- .../java/io/vertx/core/buffer/impl/BufferInternal.java | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java b/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java index ec008857737..3f18a179515 100644 --- a/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java +++ b/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java @@ -349,7 +349,7 @@ public BufferImpl appendFloat(float f) { } @Override - public Buffer appendFloatLE(float f) { + public BufferImpl appendFloatLE(float f) { buffer.writeFloatLE(f); return this; } @@ -360,7 +360,7 @@ public BufferImpl appendDouble(double d) { } @Override - public Buffer appendDoubleLE(double d) { + public BufferImpl appendDoubleLE(double d) { buffer.writeDoubleLE(d); return this; } @@ -440,7 +440,7 @@ public BufferImpl setDouble(int pos, double d) { } @Override - public Buffer setDoubleLE(int pos, double d) { + public BufferImpl setDoubleLE(int pos, double d) { ensureLength(pos + 8); buffer.setDoubleLE(pos, d); return this; @@ -453,7 +453,7 @@ public BufferImpl setFloat(int pos, float f) { } @Override - public Buffer setFloatLE(int pos, float f) { + public BufferImpl setFloatLE(int pos, float f) { ensureLength(pos + 4); buffer.setFloatLE(pos, f); return this; diff --git a/src/main/java/io/vertx/core/buffer/impl/BufferInternal.java b/src/main/java/io/vertx/core/buffer/impl/BufferInternal.java index 0efefd8eec2..fc16acb7ef6 100644 --- a/src/main/java/io/vertx/core/buffer/impl/BufferInternal.java +++ b/src/main/java/io/vertx/core/buffer/impl/BufferInternal.java @@ -108,13 +108,13 @@ static BufferInternal buffer(byte[] bytes) { BufferInternal appendFloat(float f); @Override - Buffer appendFloatLE(float f); + BufferInternal appendFloatLE(float f); @Override BufferInternal appendDouble(double d); @Override - Buffer appendDoubleLE(double d); + BufferInternal appendDoubleLE(double d); @Override BufferInternal appendString(String str, String enc); @@ -156,13 +156,13 @@ static BufferInternal buffer(byte[] bytes) { BufferInternal setDouble(int pos, double d); @Override - Buffer setDoubleLE(int pos, double d); + BufferInternal setDoubleLE(int pos, double d); @Override BufferInternal setFloat(int pos, float f); @Override - Buffer setFloatLE(int pos, float f); + BufferInternal setFloatLE(int pos, float f); @Override BufferInternal setShort(int pos, short s); From e8488ade2c7d15f0285f66bb9b34c8fcc332f382 Mon Sep 17 00:00:00 2001 From: Fenrur Date: Thu, 3 Aug 2023 11:51:14 +0200 Subject: [PATCH 4/4] Fix checkUpperBound for double getDoubleLE(int pos) in BufferImpl 4 (byte size) -> 8 (byte size) --- src/main/java/io/vertx/core/buffer/impl/BufferImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java b/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java index 3f18a179515..0f49291b5bf 100644 --- a/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java +++ b/src/main/java/io/vertx/core/buffer/impl/BufferImpl.java @@ -137,7 +137,7 @@ public double getDouble(int pos) { } public double getDoubleLE(int pos) { - checkUpperBound(pos, 4); + checkUpperBound(pos, 8); return buffer.getDoubleLE(pos); }