From a982aa4ee80a1017c53fb92282370e14afada09d Mon Sep 17 00:00:00 2001 From: Kevin Herron Date: Tue, 30 Dec 2025 06:58:14 -0800 Subject: [PATCH] Rename boxed array methods and add boxed array getters Rename set methods for boxed arrays (Boolean[], Byte[], Short[], Integer[], Long[], Float[], Double[]) from overloaded names to explicit "Boxed" names (e.g., setByteArray -> setBoxedByteArray) for API clarity. Add corresponding getter methods for boxed arrays (getBoxedBooleanArray, getBoxedByteArray, etc.) that return object arrays instead of primitives. --- .../digitalpetri/util/UnsignedByteOps.java | 87 +++- .../digitalpetri/util/AbstractByteOps.java | 131 ++++- .../java/com/digitalpetri/util/ByteOps.java | 148 ++++-- .../util/AbstractByteOpsTest.java | 490 ++++++++++++++++++ 4 files changed, 768 insertions(+), 88 deletions(-) diff --git a/byteops-unsigned/src/main/java/com/digitalpetri/util/UnsignedByteOps.java b/byteops-unsigned/src/main/java/com/digitalpetri/util/UnsignedByteOps.java index 9f73cc8..2443917 100644 --- a/byteops-unsigned/src/main/java/com/digitalpetri/util/UnsignedByteOps.java +++ b/byteops-unsigned/src/main/java/com/digitalpetri/util/UnsignedByteOps.java @@ -358,68 +358,103 @@ public void setBooleanArray(T bytes, int index, boolean[] values) { } @Override - public void setBooleanArray(T bytes, int index, Boolean[] values) { - delegate.setBooleanArray(bytes, index, values); + public void setByteArray(T bytes, int index, byte[] values) { + delegate.setByteArray(bytes, index, values); } @Override - public void setByteArray(T bytes, int index, byte[] values) { - delegate.setByteArray(bytes, index, values); + public void setShortArray(T bytes, int index, short[] values) { + delegate.setShortArray(bytes, index, values); } @Override - public void setByteArray(T bytes, int index, Byte[] values) { - delegate.setByteArray(bytes, index, values); + public void setIntArray(T bytes, int index, int[] values) { + delegate.setIntArray(bytes, index, values); } @Override - public void setShortArray(T bytes, int index, short[] values) { - delegate.setShortArray(bytes, index, values); + public void setLongArray(T bytes, int index, long[] values) { + delegate.setLongArray(bytes, index, values); } @Override - public void setShortArray(T bytes, int index, Short[] values) { - delegate.setShortArray(bytes, index, values); + public void setFloatArray(T bytes, int index, float[] values) { + delegate.setFloatArray(bytes, index, values); } @Override - public void setIntArray(T bytes, int index, int[] values) { - delegate.setIntArray(bytes, index, values); + public void setDoubleArray(T bytes, int index, double[] values) { + delegate.setDoubleArray(bytes, index, values); } @Override - public void setIntArray(T bytes, int index, Integer[] values) { - delegate.setIntArray(bytes, index, values); + public Boolean[] getBoxedBooleanArray(T bytes, int index, int length) { + return delegate.getBoxedBooleanArray(bytes, index, length); } @Override - public void setLongArray(T bytes, int index, long[] values) { - delegate.setLongArray(bytes, index, values); + public void setBoxedBooleanArray(T bytes, int index, Boolean[] values) { + delegate.setBoxedBooleanArray(bytes, index, values); } @Override - public void setLongArray(T bytes, int index, Long[] values) { - delegate.setLongArray(bytes, index, values); + public Byte[] getBoxedByteArray(T bytes, int index, int length) { + return delegate.getBoxedByteArray(bytes, index, length); } @Override - public void setFloatArray(T bytes, int index, float[] values) { - delegate.setFloatArray(bytes, index, values); + public void setBoxedByteArray(T bytes, int index, Byte[] values) { + delegate.setBoxedByteArray(bytes, index, values); } @Override - public void setFloatArray(T bytes, int index, Float[] values) { - delegate.setFloatArray(bytes, index, values); + public Short[] getBoxedShortArray(T bytes, int index, int length) { + return delegate.getBoxedShortArray(bytes, index, length); } @Override - public void setDoubleArray(T bytes, int index, double[] values) { - delegate.setDoubleArray(bytes, index, values); + public void setBoxedShortArray(T bytes, int index, Short[] values) { + delegate.setBoxedShortArray(bytes, index, values); } @Override - public void setDoubleArray(T bytes, int index, Double[] values) { - delegate.setDoubleArray(bytes, index, values); + public Integer[] getBoxedIntArray(T bytes, int index, int length) { + return delegate.getBoxedIntArray(bytes, index, length); + } + + @Override + public void setBoxedIntArray(T bytes, int index, Integer[] values) { + delegate.setBoxedIntArray(bytes, index, values); + } + + @Override + public Long[] getBoxedLongArray(T bytes, int index, int length) { + return delegate.getBoxedLongArray(bytes, index, length); + } + + @Override + public void setBoxedLongArray(T bytes, int index, Long[] values) { + delegate.setBoxedLongArray(bytes, index, values); + } + + @Override + public Float[] getBoxedFloatArray(T bytes, int index, int length) { + return delegate.getBoxedFloatArray(bytes, index, length); + } + + @Override + public void setBoxedFloatArray(T bytes, int index, Float[] values) { + delegate.setBoxedFloatArray(bytes, index, values); + } + + @Override + public Double[] getBoxedDoubleArray(T bytes, int index, int length) { + return delegate.getBoxedDoubleArray(bytes, index, length); + } + + @Override + public void setBoxedDoubleArray(T bytes, int index, Double[] values) { + delegate.setBoxedDoubleArray(bytes, index, values); } // endregion diff --git a/byteops/src/main/java/com/digitalpetri/util/AbstractByteOps.java b/byteops/src/main/java/com/digitalpetri/util/AbstractByteOps.java index 20e30ee..eb30cf7 100644 --- a/byteops/src/main/java/com/digitalpetri/util/AbstractByteOps.java +++ b/byteops/src/main/java/com/digitalpetri/util/AbstractByteOps.java @@ -185,93 +185,174 @@ public void setBooleanArray(T bytes, int index, boolean[] values) { } @Override - public void setBooleanArray(T bytes, int index, Boolean[] values) { + public void setByteArray(T bytes, int index, byte[] values) { for (int i = 0; i < values.length; i++) { - setBoolean(bytes, index + i, values[i]); + setByte(bytes, index + i, values[i]); } } @Override - public void setByteArray(T bytes, int index, byte[] values) { + public void setShortArray(T bytes, int index, short[] values) { for (int i = 0; i < values.length; i++) { - setByte(bytes, index + i, values[i]); + setShort(bytes, index + i * 2, values[i]); } } @Override - public void setByteArray(T bytes, int index, Byte[] values) { + public void setIntArray(T bytes, int index, int[] values) { for (int i = 0; i < values.length; i++) { - setByte(bytes, index + i, values[i]); + setInt(bytes, index + i * 4, values[i]); } } @Override - public void setShortArray(T bytes, int index, short[] values) { + public void setLongArray(T bytes, int index, long[] values) { for (int i = 0; i < values.length; i++) { - setShort(bytes, index + i * 2, values[i]); + setLong(bytes, index + i * 8, values[i]); } } @Override - public void setShortArray(T bytes, int index, Short[] values) { + public void setFloatArray(T bytes, int index, float[] values) { for (int i = 0; i < values.length; i++) { - setShort(bytes, index + i * 2, values[i]); + setFloat(bytes, index + i * 4, values[i]); } } @Override - public void setIntArray(T bytes, int index, int[] values) { + public void setDoubleArray(T bytes, int index, double[] values) { for (int i = 0; i < values.length; i++) { - setInt(bytes, index + i * 4, values[i]); + setDouble(bytes, index + i * 8, values[i]); } } + // region Boxed Array Support + @Override - public void setIntArray(T bytes, int index, Integer[] values) { + public Boolean[] getBoxedBooleanArray(T bytes, int index, int length) { + var value = new Boolean[length]; + + for (int i = 0; i < length; i++) { + value[i] = getBoolean(bytes, index + i); + } + + return value; + } + + @Override + public Byte[] getBoxedByteArray(T bytes, int index, int length) { + var value = new Byte[length]; + + for (int i = 0; i < length; i++) { + value[i] = getByte(bytes, index + i); + } + + return value; + } + + @Override + public Short[] getBoxedShortArray(T bytes, int index, int length) { + var value = new Short[length]; + + for (int i = 0; i < length; i++) { + value[i] = getShort(bytes, index + i * 2); + } + + return value; + } + + @Override + public Integer[] getBoxedIntArray(T bytes, int index, int length) { + var value = new Integer[length]; + + for (int i = 0; i < length; i++) { + value[i] = getInt(bytes, index + i * 4); + } + + return value; + } + + @Override + public Long[] getBoxedLongArray(T bytes, int index, int length) { + var value = new Long[length]; + + for (int i = 0; i < length; i++) { + value[i] = getLong(bytes, index + i * 8); + } + + return value; + } + + @Override + public Float[] getBoxedFloatArray(T bytes, int index, int length) { + var value = new Float[length]; + + for (int i = 0; i < length; i++) { + value[i] = getFloat(bytes, index + i * 4); + } + + return value; + } + + @Override + public Double[] getBoxedDoubleArray(T bytes, int index, int length) { + var value = new Double[length]; + + for (int i = 0; i < length; i++) { + value[i] = getDouble(bytes, index + i * 8); + } + + return value; + } + + @Override + public void setBoxedBooleanArray(T bytes, int index, Boolean[] values) { for (int i = 0; i < values.length; i++) { - setInt(bytes, index + i * 4, values[i]); + setBoolean(bytes, index + i, values[i]); } } @Override - public void setLongArray(T bytes, int index, long[] values) { + public void setBoxedByteArray(T bytes, int index, Byte[] values) { for (int i = 0; i < values.length; i++) { - setLong(bytes, index + i * 8, values[i]); + setByte(bytes, index + i, values[i]); } } @Override - public void setLongArray(T bytes, int index, Long[] values) { + public void setBoxedShortArray(T bytes, int index, Short[] values) { for (int i = 0; i < values.length; i++) { - setLong(bytes, index + i * 8, values[i]); + setShort(bytes, index + i * 2, values[i]); } } @Override - public void setFloatArray(T bytes, int index, float[] values) { + public void setBoxedIntArray(T bytes, int index, Integer[] values) { for (int i = 0; i < values.length; i++) { - setFloat(bytes, index + i * 4, values[i]); + setInt(bytes, index + i * 4, values[i]); } } @Override - public void setFloatArray(T bytes, int index, Float[] values) { + public void setBoxedLongArray(T bytes, int index, Long[] values) { for (int i = 0; i < values.length; i++) { - setFloat(bytes, index + i * 4, values[i]); + setLong(bytes, index + i * 8, values[i]); } } @Override - public void setDoubleArray(T bytes, int index, double[] values) { + public void setBoxedFloatArray(T bytes, int index, Float[] values) { for (int i = 0; i < values.length; i++) { - setDouble(bytes, index + i * 8, values[i]); + setFloat(bytes, index + i * 4, values[i]); } } @Override - public void setDoubleArray(T bytes, int index, Double[] values) { + public void setBoxedDoubleArray(T bytes, int index, Double[] values) { for (int i = 0; i < values.length; i++) { setDouble(bytes, index + i * 8, values[i]); } } + + // endregion } diff --git a/byteops/src/main/java/com/digitalpetri/util/ByteOps.java b/byteops/src/main/java/com/digitalpetri/util/ByteOps.java index e4bf581..aaf43f2 100644 --- a/byteops/src/main/java/com/digitalpetri/util/ByteOps.java +++ b/byteops/src/main/java/com/digitalpetri/util/ByteOps.java @@ -212,112 +212,184 @@ public interface ByteOps { void setBooleanArray(T bytes, int index, boolean[] values); /** - * Set the Boolean array at the given {@code index} in {@code bytes}. + * Set the byte array at the given {@code index} in {@code bytes}. * * @param bytes the bytes to set the values in. * @param index the index into {@code bytes} to set the values at. - * @param values the Boolean array to set. + * @param values the byte array to set. */ - void setBooleanArray(T bytes, int index, Boolean[] values); + void setByteArray(T bytes, int index, byte[] values); /** - * Set the byte array at the given {@code index} in {@code bytes}. + * Set the short array at the given {@code index} in {@code bytes}. * * @param bytes the bytes to set the values in. * @param index the index into {@code bytes} to set the values at. - * @param values the byte array to set. + * @param values the short array to set. */ - void setByteArray(T bytes, int index, byte[] values); + void setShortArray(T bytes, int index, short[] values); /** - * Set the Byte array at the given {@code index} in {@code bytes}. + * Set the int array at the given {@code index} in {@code bytes}. * * @param bytes the bytes to set the values in. * @param index the index into {@code bytes} to set the values at. - * @param values the Byte array to set. + * @param values the int array to set. */ - void setByteArray(T bytes, int index, Byte[] values); + void setIntArray(T bytes, int index, int[] values); /** - * Set the short array at the given {@code index} in {@code bytes}. + * Set the long array at the given {@code index} in {@code bytes}. * * @param bytes the bytes to set the values in. * @param index the index into {@code bytes} to set the values at. - * @param values the short array to set. + * @param values the long array to set. */ - void setShortArray(T bytes, int index, short[] values); + void setLongArray(T bytes, int index, long[] values); /** - * Set the Short array at the given {@code index} in {@code bytes}. + * Set the float array at the given {@code index} in {@code bytes}. * * @param bytes the bytes to set the values in. * @param index the index into {@code bytes} to set the values at. - * @param values the Short array to set. + * @param values the float array to set. */ - void setShortArray(T bytes, int index, Short[] values); + void setFloatArray(T bytes, int index, float[] values); /** - * Set the int array at the given {@code index} in {@code bytes}. + * Set the double array at the given {@code index} in {@code bytes}. * * @param bytes the bytes to set the values in. * @param index the index into {@code bytes} to set the values at. - * @param values the int array to set. + * @param values the double array to set. */ - void setIntArray(T bytes, int index, int[] values); + void setDoubleArray(T bytes, int index, double[] values); + + // region Boxed Array Support /** - * Set the Integer array at the given {@code index} in {@code bytes}. + * Get the Boolean array at the given {@code index} in {@code bytes}. + * + * @param bytes the bytes to get the value from. + * @param index the index into {@code bytes} to get the value at. + * @param length the length of the array. + * @return the Boolean array at the given {@code index}. + */ + Boolean[] getBoxedBooleanArray(T bytes, int index, int length); + + /** + * Get the Byte array at the given {@code index} in {@code bytes}. + * + * @param bytes the bytes to get the value from. + * @param index the index into {@code bytes} to get the value at. + * @param length the length of the array. + * @return the Byte array at the given {@code index}. + */ + Byte[] getBoxedByteArray(T bytes, int index, int length); + + /** + * Get the Short array at the given {@code index} in {@code bytes}. + * + * @param bytes the bytes to get the value from. + * @param index the index into {@code bytes} to get the value at. + * @param length the length of the array. + * @return the Short array at the given {@code index}. + */ + Short[] getBoxedShortArray(T bytes, int index, int length); + + /** + * Get the Integer array at the given {@code index} in {@code bytes}. + * + * @param bytes the bytes to get the value from. + * @param index the index into {@code bytes} to get the value at. + * @param length the length of the array. + * @return the Integer array at the given {@code index}. + */ + Integer[] getBoxedIntArray(T bytes, int index, int length); + + /** + * Get the Long array at the given {@code index} in {@code bytes}. + * + * @param bytes the bytes to get the value from. + * @param index the index into {@code bytes} to get the value at. + * @param length the length of the array. + * @return the Long array at the given {@code index}. + */ + Long[] getBoxedLongArray(T bytes, int index, int length); + + /** + * Get the Float array at the given {@code index} in {@code bytes}. + * + * @param bytes the bytes to get the value from. + * @param index the index into {@code bytes} to get the value at. + * @param length the length of the array. + * @return the Float array at the given {@code index}. + */ + Float[] getBoxedFloatArray(T bytes, int index, int length); + + /** + * Get the Double array at the given {@code index} in {@code bytes}. + * + * @param bytes the bytes to get the value from. + * @param index the index into {@code bytes} to get the value at. + * @param length the length of the array. + * @return the Double array at the given {@code index}. + */ + Double[] getBoxedDoubleArray(T bytes, int index, int length); + + /** + * Set the Boolean array at the given {@code index} in {@code bytes}. * * @param bytes the bytes to set the values in. * @param index the index into {@code bytes} to set the values at. - * @param values the Integer array to set. + * @param values the Boolean array to set. */ - void setIntArray(T bytes, int index, Integer[] values); + void setBoxedBooleanArray(T bytes, int index, Boolean[] values); /** - * Set the long array at the given {@code index} in {@code bytes}. + * Set the Byte array at the given {@code index} in {@code bytes}. * * @param bytes the bytes to set the values in. * @param index the index into {@code bytes} to set the values at. - * @param values the long array to set. + * @param values the Byte array to set. */ - void setLongArray(T bytes, int index, long[] values); + void setBoxedByteArray(T bytes, int index, Byte[] values); /** - * Set the Long array at the given {@code index} in {@code bytes}. + * Set the Short array at the given {@code index} in {@code bytes}. * * @param bytes the bytes to set the values in. * @param index the index into {@code bytes} to set the values at. - * @param values the Long array to set. + * @param values the Short array to set. */ - void setLongArray(T bytes, int index, Long[] values); + void setBoxedShortArray(T bytes, int index, Short[] values); /** - * Set the float array at the given {@code index} in {@code bytes}. + * Set the Integer array at the given {@code index} in {@code bytes}. * * @param bytes the bytes to set the values in. * @param index the index into {@code bytes} to set the values at. - * @param values the float array to set. + * @param values the Integer array to set. */ - void setFloatArray(T bytes, int index, float[] values); + void setBoxedIntArray(T bytes, int index, Integer[] values); /** - * Set the Float array at the given {@code index} in {@code bytes}. + * Set the Long array at the given {@code index} in {@code bytes}. * * @param bytes the bytes to set the values in. * @param index the index into {@code bytes} to set the values at. - * @param values the Float array to set. + * @param values the Long array to set. */ - void setFloatArray(T bytes, int index, Float[] values); + void setBoxedLongArray(T bytes, int index, Long[] values); /** - * Set the double array at the given {@code index} in {@code bytes}. + * Set the Float array at the given {@code index} in {@code bytes}. * * @param bytes the bytes to set the values in. * @param index the index into {@code bytes} to set the values at. - * @param values the double array to set. + * @param values the Float array to set. */ - void setDoubleArray(T bytes, int index, double[] values); + void setBoxedFloatArray(T bytes, int index, Float[] values); /** * Set the Double array at the given {@code index} in {@code bytes}. @@ -326,5 +398,7 @@ public interface ByteOps { * @param index the index into {@code bytes} to set the values at. * @param values the Double array to set. */ - void setDoubleArray(T bytes, int index, Double[] values); + void setBoxedDoubleArray(T bytes, int index, Double[] values); + + // endregion } diff --git a/byteops/src/test/java/com/digitalpetri/util/AbstractByteOpsTest.java b/byteops/src/test/java/com/digitalpetri/util/AbstractByteOpsTest.java index 7514cbe..09c21f8 100644 --- a/byteops/src/test/java/com/digitalpetri/util/AbstractByteOpsTest.java +++ b/byteops/src/test/java/com/digitalpetri/util/AbstractByteOpsTest.java @@ -279,6 +279,104 @@ void getDoubleArray() { }, ds); } + + @Test + void getBoxedBooleanArray() { + T bytes = getBytes(new byte[] {0x00, 0x01}); + + Boolean[] bs = byteOps.getBoxedBooleanArray(bytes, 0, 2); + + assertFalse(bs[0]); + assertTrue(bs[1]); + assertArrayEquals(new Boolean[] {false, true}, bs); + } + + @Test + void getBoxedByteArray() { + T bytes = getBytes(new byte[] {0x00, 0x01}); + + Byte[] bs = byteOps.getBoxedByteArray(bytes, 0, 2); + + assertEquals((byte) 0x00, bs[0]); + assertEquals((byte) 0x01, bs[1]); + assertArrayEquals(new Byte[] {0x00, 0x01}, bs); + } + + @Test + void getBoxedShortArray() { + T bytes = getBytes(new byte[] {0x00, 0x01, 0x02, 0x03}); + + Short[] ss = byteOps.getBoxedShortArray(bytes, 0, 2); + + assertEquals((short) 0x0001, ss[0]); + assertEquals((short) 0x0203, ss[1]); + assertArrayEquals(new Short[] {0x0001, 0x0203}, ss); + } + + @Test + void getBoxedIntArray() { + T bytes = getBytes(new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}); + + Integer[] is = byteOps.getBoxedIntArray(bytes, 0, 2); + + assertEquals(0x0001_0203, is[0]); + assertEquals(0x0405_0607, is[1]); + assertArrayEquals(new Integer[] {0x0001_0203, 0x0405_0607}, is); + } + + @Test + void getBoxedLongArray() { + T bytes = + getBytes( + new byte[] { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F + }); + + Long[] ls = byteOps.getBoxedLongArray(bytes, 0, 2); + + assertEquals(0x0001_0203_0405_0607L, ls[0]); + assertEquals(0x0809_0A0B_0C0D_0E0FL, ls[1]); + assertArrayEquals(new Long[] {0x0001_0203_0405_0607L, 0x0809_0A0B_0C0D_0E0FL}, ls); + } + + @Test + void getBoxedFloatArray() { + T bytes = + getBytes( + new byte[] { + 0x3F, (byte) 0x80, 0x00, 0x00, + 0x3F, (byte) 0x80, 0x00, 0x00 + }); + + Float[] fs = byteOps.getBoxedFloatArray(bytes, 0, 2); + + assertEquals(Float.intBitsToFloat(0x3F80_0000), fs[0]); + assertEquals(Float.intBitsToFloat(0x3F80_0000), fs[1]); + assertArrayEquals( + new Float[] {Float.intBitsToFloat(0x3F80_0000), Float.intBitsToFloat(0x3F80_0000)}, fs); + } + + @Test + void getBoxedDoubleArray() { + T bytes = + getBytes( + new byte[] { + 0x3F, (byte) 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x3F, (byte) 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }); + + Double[] ds = byteOps.getBoxedDoubleArray(bytes, 0, 2); + + assertEquals(Double.longBitsToDouble(0x3FF0_0000_0000_0000L), ds[0]); + assertEquals(Double.longBitsToDouble(0x3FF0_0000_0000_0000L), ds[1]); + assertArrayEquals( + new Double[] { + Double.longBitsToDouble(0x3FF0_0000_0000_0000L), + Double.longBitsToDouble(0x3FF0_0000_0000_0000L) + }, + ds); + } } @Nested @@ -429,6 +527,153 @@ void setDoubleArray() { assertEquals(Double.longBitsToDouble(0x3FF0_0000_0000_0000L), byteOps.getDouble(bytes, 0)); assertEquals(Double.longBitsToDouble(0x4000_0000_0000_0000L), byteOps.getDouble(bytes, 8)); } + + @Test + void setBoxedBooleanArray() { + T bytes = getBytes(new byte[] {0x00, 0x00}); + + byteOps.setBoxedBooleanArray(bytes, 0, new Boolean[] {true, true}); + + assertTrue(byteOps.getBoolean(bytes, 0)); + assertTrue(byteOps.getBoolean(bytes, 1)); + } + + @Test + void setBoxedByteArray() { + T bytes = getBytes(new byte[] {0x00, 0x00}); + + byteOps.setBoxedByteArray(bytes, 0, new Byte[] {0x01, 0x02}); + + assertEquals(0x01, byteOps.getByte(bytes, 0)); + assertEquals(0x02, byteOps.getByte(bytes, 1)); + } + + @Test + void setBoxedShortArray() { + T bytes = getBytes(new byte[] {0x00, 0x00, 0x00, 0x00}); + + byteOps.setBoxedShortArray(bytes, 0, new Short[] {0x0102, 0x0304}); + + assertEquals(0x01, byteOps.getByte(bytes, 0)); + assertEquals(0x02, byteOps.getByte(bytes, 1)); + assertEquals(0x03, byteOps.getByte(bytes, 2)); + assertEquals(0x04, byteOps.getByte(bytes, 3)); + assertEquals(0x0102, byteOps.getShort(bytes, 0)); + assertEquals(0x0304, byteOps.getShort(bytes, 2)); + } + + @Test + void setBoxedIntArray() { + T bytes = getBytes(new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); + + byteOps.setBoxedIntArray(bytes, 0, new Integer[] {0x0102_0304, 0x0506_0708}); + + assertEquals(0x01, byteOps.getByte(bytes, 0)); + assertEquals(0x02, byteOps.getByte(bytes, 1)); + assertEquals(0x03, byteOps.getByte(bytes, 2)); + assertEquals(0x04, byteOps.getByte(bytes, 3)); + assertEquals(0x05, byteOps.getByte(bytes, 4)); + assertEquals(0x06, byteOps.getByte(bytes, 5)); + assertEquals(0x07, byteOps.getByte(bytes, 6)); + assertEquals(0x08, byteOps.getByte(bytes, 7)); + assertEquals(0x0102_0304, byteOps.getInt(bytes, 0)); + assertEquals(0x0506_0708, byteOps.getInt(bytes, 4)); + } + + @Test + void setBoxedLongArray() { + T bytes = + getBytes( + new byte[] { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }); + + byteOps.setBoxedLongArray( + bytes, 0, new Long[] {0x0102_0304_0506_0708L, 0x090A_0B0C_0D0E_0F10L}); + + assertEquals(0x01, byteOps.getByte(bytes, 0)); + assertEquals(0x02, byteOps.getByte(bytes, 1)); + assertEquals(0x03, byteOps.getByte(bytes, 2)); + assertEquals(0x04, byteOps.getByte(bytes, 3)); + assertEquals(0x05, byteOps.getByte(bytes, 4)); + assertEquals(0x06, byteOps.getByte(bytes, 5)); + assertEquals(0x07, byteOps.getByte(bytes, 6)); + assertEquals(0x08, byteOps.getByte(bytes, 7)); + assertEquals(0x09, byteOps.getByte(bytes, 8)); + assertEquals(0x0A, byteOps.getByte(bytes, 9)); + assertEquals(0x0B, byteOps.getByte(bytes, 10)); + assertEquals(0x0C, byteOps.getByte(bytes, 11)); + assertEquals(0x0D, byteOps.getByte(bytes, 12)); + assertEquals(0x0E, byteOps.getByte(bytes, 13)); + assertEquals(0x0F, byteOps.getByte(bytes, 14)); + assertEquals(0x10, byteOps.getByte(bytes, 15)); + assertEquals(0x0102_0304_0506_0708L, byteOps.getLong(bytes, 0)); + assertEquals(0x090A_0B0C_0D0E_0F10L, byteOps.getLong(bytes, 8)); + } + + @Test + void setBoxedFloatArray() { + T bytes = + getBytes( + new byte[] { + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00 + }); + + byteOps.setBoxedFloatArray( + bytes, + 0, + new Float[] {Float.intBitsToFloat(0x3F80_0000), Float.intBitsToFloat(0x4000_0000)}); + + assertEquals(0x3F, byteOps.getByte(bytes, 0)); + assertEquals((byte) 0x80, byteOps.getByte(bytes, 1)); + assertEquals(0x00, byteOps.getByte(bytes, 2)); + assertEquals(0x00, byteOps.getByte(bytes, 3)); + assertEquals(0x40, byteOps.getByte(bytes, 4)); + assertEquals(0x00, byteOps.getByte(bytes, 5)); + assertEquals(0x00, byteOps.getByte(bytes, 6)); + assertEquals(0x00, byteOps.getByte(bytes, 7)); + assertEquals(Float.intBitsToFloat(0x3F80_0000), byteOps.getFloat(bytes, 0)); + assertEquals(Float.intBitsToFloat(0x4000_0000), byteOps.getFloat(bytes, 4)); + } + + @Test + void setBoxedDoubleArray() { + T bytes = + getBytes( + new byte[] { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }); + + byteOps.setBoxedDoubleArray( + bytes, + 0, + new Double[] { + Double.longBitsToDouble(0x3FF0_0000_0000_0000L), + Double.longBitsToDouble(0x4000_0000_0000_0000L) + }); + + assertEquals(0x3F, byteOps.getByte(bytes, 0)); + assertEquals((byte) 0xF0, byteOps.getByte(bytes, 1)); + assertEquals(0x00, byteOps.getByte(bytes, 2)); + assertEquals(0x00, byteOps.getByte(bytes, 3)); + assertEquals(0x00, byteOps.getByte(bytes, 4)); + assertEquals(0x00, byteOps.getByte(bytes, 5)); + assertEquals(0x00, byteOps.getByte(bytes, 6)); + assertEquals(0x00, byteOps.getByte(bytes, 7)); + assertEquals(0x40, byteOps.getByte(bytes, 8)); + assertEquals(0x00, byteOps.getByte(bytes, 9)); + assertEquals(0x00, byteOps.getByte(bytes, 10)); + assertEquals(0x00, byteOps.getByte(bytes, 11)); + assertEquals(0x00, byteOps.getByte(bytes, 12)); + assertEquals(0x00, byteOps.getByte(bytes, 13)); + assertEquals(0x00, byteOps.getByte(bytes, 14)); + assertEquals(0x00, byteOps.getByte(bytes, 15)); + assertEquals(Double.longBitsToDouble(0x3FF0_0000_0000_0000L), byteOps.getDouble(bytes, 0)); + assertEquals(Double.longBitsToDouble(0x4000_0000_0000_0000L), byteOps.getDouble(bytes, 8)); + } } } @@ -966,6 +1211,104 @@ void getDoubleArray() { }, ds); } + + @Test + void getBoxedBooleanArray() { + T bytes = getBytes(new byte[] {0x00, 0x01}); + + Boolean[] bs = byteOps.getBoxedBooleanArray(bytes, 0, 2); + + assertFalse(bs[0]); + assertTrue(bs[1]); + assertArrayEquals(new Boolean[] {false, true}, bs); + } + + @Test + void getBoxedByteArray() { + T bytes = getBytes(new byte[] {0x00, 0x01}); + + Byte[] bs = byteOps.getBoxedByteArray(bytes, 0, 2); + + assertEquals((byte) 0x00, bs[0]); + assertEquals((byte) 0x01, bs[1]); + assertArrayEquals(new Byte[] {0x00, 0x01}, bs); + } + + @Test + void getBoxedShortArray() { + T bytes = getBytes(new byte[] {0x00, 0x01, 0x02, 0x03}); + + Short[] ss = byteOps.getBoxedShortArray(bytes, 0, 2); + + assertEquals((short) 0x0100, ss[0]); + assertEquals((short) 0x0302, ss[1]); + assertArrayEquals(new Short[] {0x0100, 0x0302}, ss); + } + + @Test + void getBoxedIntArray() { + T bytes = getBytes(new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}); + + Integer[] is = byteOps.getBoxedIntArray(bytes, 0, 2); + + assertEquals(0x0302_0100, is[0]); + assertEquals(0x0706_0504, is[1]); + assertArrayEquals(new Integer[] {0x0302_0100, 0x0706_0504}, is); + } + + @Test + void getBoxedLongArray() { + T bytes = + getBytes( + new byte[] { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F + }); + + Long[] ls = byteOps.getBoxedLongArray(bytes, 0, 2); + + assertEquals(0x0706_0504_0302_0100L, ls[0]); + assertEquals(0x0F0E_0D0C_0B0A_0908L, ls[1]); + assertArrayEquals(new Long[] {0x0706_0504_0302_0100L, 0x0F0E_0D0C_0B0A_0908L}, ls); + } + + @Test + void getBoxedFloatArray() { + T bytes = + getBytes( + new byte[] { + 0x00, 0x00, (byte) 0x80, 0x3F, + 0x00, 0x00, (byte) 0x80, 0x3F + }); + + Float[] fs = byteOps.getBoxedFloatArray(bytes, 0, 2); + + assertEquals(Float.intBitsToFloat(0x3F80_0000), fs[0]); + assertEquals(Float.intBitsToFloat(0x3F80_0000), fs[1]); + assertArrayEquals( + new Float[] {Float.intBitsToFloat(0x3F80_0000), Float.intBitsToFloat(0x3F80_0000)}, fs); + } + + @Test + void getBoxedDoubleArray() { + T bytes = + getBytes( + new byte[] { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xF0, 0x3F, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xF0, 0x3F + }); + + Double[] ds = byteOps.getBoxedDoubleArray(bytes, 0, 2); + + assertEquals(Double.longBitsToDouble(0x3FF0_0000_0000_0000L), ds[0]); + assertEquals(Double.longBitsToDouble(0x3FF0_0000_0000_0000L), ds[1]); + assertArrayEquals( + new Double[] { + Double.longBitsToDouble(0x3FF0_0000_0000_0000L), + Double.longBitsToDouble(0x3FF0_0000_0000_0000L) + }, + ds); + } } @Nested @@ -1095,6 +1438,153 @@ void setDoubleArray() { }, byteOps.getDoubleArray(bytes, 0, 2)); } + + @Test + void setBoxedBooleanArray() { + T bytes = getBytes(new byte[] {0x00, 0x00}); + + byteOps.setBoxedBooleanArray(bytes, 0, new Boolean[] {true, true}); + + assertTrue(byteOps.getBoolean(bytes, 0)); + assertTrue(byteOps.getBoolean(bytes, 1)); + } + + @Test + void setBoxedByteArray() { + T bytes = getBytes(new byte[] {0x00, 0x00}); + + byteOps.setBoxedByteArray(bytes, 0, new Byte[] {0x01, 0x02}); + + assertEquals(0x01, byteOps.getByte(bytes, 0)); + assertEquals(0x02, byteOps.getByte(bytes, 1)); + } + + @Test + void setBoxedShortArray() { + T bytes = getBytes(new byte[4]); + + byteOps.setBoxedShortArray(bytes, 0, new Short[] {0x0102, 0x0304}); + + assertEquals(0x02, byteOps.getByte(bytes, 0)); + assertEquals(0x01, byteOps.getByte(bytes, 1)); + assertEquals(0x04, byteOps.getByte(bytes, 2)); + assertEquals(0x03, byteOps.getByte(bytes, 3)); + assertEquals(0x0102, byteOps.getShort(bytes, 0)); + assertEquals(0x0304, byteOps.getShort(bytes, 2)); + assertArrayEquals(new Short[] {0x0102, 0x0304}, byteOps.getBoxedShortArray(bytes, 0, 2)); + } + + @Test + void setBoxedIntArray() { + T bytes = getBytes(new byte[8]); + + byteOps.setBoxedIntArray(bytes, 0, new Integer[] {0x0102_0304, 0x0506_0708}); + + assertEquals(0x04, byteOps.getByte(bytes, 0)); + assertEquals(0x03, byteOps.getByte(bytes, 1)); + assertEquals(0x02, byteOps.getByte(bytes, 2)); + assertEquals(0x01, byteOps.getByte(bytes, 3)); + assertEquals(0x08, byteOps.getByte(bytes, 4)); + assertEquals(0x07, byteOps.getByte(bytes, 5)); + assertEquals(0x06, byteOps.getByte(bytes, 6)); + assertEquals(0x05, byteOps.getByte(bytes, 7)); + assertEquals(0x0102_0304, byteOps.getInt(bytes, 0)); + assertEquals(0x0506_0708, byteOps.getInt(bytes, 4)); + assertArrayEquals( + new Integer[] {0x0102_0304, 0x0506_0708}, byteOps.getBoxedIntArray(bytes, 0, 2)); + } + + @Test + void setBoxedLongArray() { + T bytes = getBytes(new byte[16]); + + byteOps.setBoxedLongArray( + bytes, 0, new Long[] {0x0102_0304_0506_0708L, 0x090A_0B0C_0D0E_0F10L}); + + assertEquals(0x08, byteOps.getByte(bytes, 0)); + assertEquals(0x07, byteOps.getByte(bytes, 1)); + assertEquals(0x06, byteOps.getByte(bytes, 2)); + assertEquals(0x05, byteOps.getByte(bytes, 3)); + assertEquals(0x04, byteOps.getByte(bytes, 4)); + assertEquals(0x03, byteOps.getByte(bytes, 5)); + assertEquals(0x02, byteOps.getByte(bytes, 6)); + assertEquals(0x01, byteOps.getByte(bytes, 7)); + assertEquals(0x10, byteOps.getByte(bytes, 8)); + assertEquals(0x0F, byteOps.getByte(bytes, 9)); + assertEquals(0x0E, byteOps.getByte(bytes, 10)); + assertEquals(0x0D, byteOps.getByte(bytes, 11)); + assertEquals(0x0C, byteOps.getByte(bytes, 12)); + assertEquals(0x0B, byteOps.getByte(bytes, 13)); + assertEquals(0x0A, byteOps.getByte(bytes, 14)); + assertEquals(0x09, byteOps.getByte(bytes, 15)); + assertEquals(0x0102_0304_0506_0708L, byteOps.getLong(bytes, 0)); + assertEquals(0x090A_0B0C_0D0E_0F10L, byteOps.getLong(bytes, 8)); + assertArrayEquals( + new Long[] {0x0102_0304_0506_0708L, 0x090A_0B0C_0D0E_0F10L}, + byteOps.getBoxedLongArray(bytes, 0, 2)); + } + + @Test + void setBoxedFloatArray() { + T bytes = getBytes(new byte[8]); + + byteOps.setBoxedFloatArray( + bytes, + 0, + new Float[] {Float.intBitsToFloat(0x3F80_0000), Float.intBitsToFloat(0x4000_0000)}); + + assertEquals(0x00, byteOps.getByte(bytes, 0)); + assertEquals(0x00, byteOps.getByte(bytes, 1)); + assertEquals((byte) 0x80, byteOps.getByte(bytes, 2)); + assertEquals(0x3F, byteOps.getByte(bytes, 3)); + assertEquals(0x00, byteOps.getByte(bytes, 4)); + assertEquals(0x00, byteOps.getByte(bytes, 5)); + assertEquals(0x00, byteOps.getByte(bytes, 6)); + assertEquals(0x40, byteOps.getByte(bytes, 7)); + assertEquals(Float.intBitsToFloat(0x3F80_0000), byteOps.getFloat(bytes, 0)); + assertEquals(Float.intBitsToFloat(0x4000_0000), byteOps.getFloat(bytes, 4)); + assertArrayEquals( + new Float[] {Float.intBitsToFloat(0x3F80_0000), Float.intBitsToFloat(0x4000_0000)}, + byteOps.getBoxedFloatArray(bytes, 0, 2)); + } + + @Test + void setBoxedDoubleArray() { + T bytes = getBytes(new byte[16]); + + byteOps.setBoxedDoubleArray( + bytes, + 0, + new Double[] { + Double.longBitsToDouble(0x3FF0_0000_0000_0000L), + Double.longBitsToDouble(0x4000_0000_0000_0000L) + }); + + assertEquals(0x00, byteOps.getByte(bytes, 0)); + assertEquals(0x00, byteOps.getByte(bytes, 1)); + assertEquals(0x00, byteOps.getByte(bytes, 2)); + assertEquals(0x00, byteOps.getByte(bytes, 3)); + assertEquals(0x00, byteOps.getByte(bytes, 4)); + assertEquals(0x00, byteOps.getByte(bytes, 5)); + assertEquals((byte) 0xF0, byteOps.getByte(bytes, 6)); + assertEquals(0x3F, byteOps.getByte(bytes, 7)); + assertEquals(0x00, byteOps.getByte(bytes, 8)); + assertEquals(0x00, byteOps.getByte(bytes, 9)); + assertEquals(0x00, byteOps.getByte(bytes, 10)); + assertEquals(0x00, byteOps.getByte(bytes, 11)); + assertEquals(0x00, byteOps.getByte(bytes, 12)); + assertEquals(0x00, byteOps.getByte(bytes, 13)); + assertEquals(0x00, byteOps.getByte(bytes, 14)); + assertEquals(0x40, byteOps.getByte(bytes, 15)); + assertEquals(Double.longBitsToDouble(0x3FF0_0000_0000_0000L), byteOps.getDouble(bytes, 0)); + assertEquals(Double.longBitsToDouble(0x4000_0000_0000_0000L), byteOps.getDouble(bytes, 8)); + assertArrayEquals( + new Double[] { + Double.longBitsToDouble(0x3FF0_0000_0000_0000L), + Double.longBitsToDouble(0x4000_0000_0000_0000L) + }, + byteOps.getBoxedDoubleArray(bytes, 0, 2)); + } } }