This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
commit 6258b40a1832cd46379210737ddbaddaaf6a1d28 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Dec 17 16:22:10 2023 -0500 Sort members --- .../java/org/apache/commons/io/EndianUtils.java | 26 ++++++++++---------- .../org/apache/commons/io/EndianUtilsTest.java | 28 +++++++++++----------- .../io/input/CharSequenceInputStreamTest.java | 22 ++++++++--------- 3 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/main/java/org/apache/commons/io/EndianUtils.java b/src/main/java/org/apache/commons/io/EndianUtils.java index ae7b1bd9..9a0cc9d8 100644 --- a/src/main/java/org/apache/commons/io/EndianUtils.java +++ b/src/main/java/org/apache/commons/io/EndianUtils.java @@ -41,19 +41,6 @@ import java.io.OutputStream; */ public class EndianUtils { - /** - * Validates if the provided byte array have enough data. - * @param data the input byte array - * @param offset the input offset - * @param byteNeeded the needed number of bytes - * @throws IllegalArgumentException if the byte array does not have enough data - */ - private static void validateByteArrayOffset(final byte[] data, final int offset, final int byteNeeded) { - if (data.length < offset + byteNeeded) { - throw new IllegalArgumentException("Data only has " + data.length + "bytes, needed " + (offset + byteNeeded) + "bytes."); - } - } - /** * Reads the next byte from the input stream. * @param input the stream @@ -313,6 +300,19 @@ public class EndianUtils { ((value >> 8 & 0xff) << 0)); } + /** + * Validates if the provided byte array have enough data. + * @param data the input byte array + * @param offset the input offset + * @param byteNeeded the needed number of bytes + * @throws IllegalArgumentException if the byte array does not have enough data + */ + private static void validateByteArrayOffset(final byte[] data, final int offset, final int byteNeeded) { + if (data.length < offset + byteNeeded) { + throw new IllegalArgumentException("Data only has " + data.length + "bytes, needed " + (offset + byteNeeded) + "bytes."); + } + } + /** * Writes a "double" value to a byte array at a given offset. The value is * converted to the opposed endian system while writing. diff --git a/src/test/java/org/apache/commons/io/EndianUtilsTest.java b/src/test/java/org/apache/commons/io/EndianUtilsTest.java index e93adac3..98a72c6a 100644 --- a/src/test/java/org/apache/commons/io/EndianUtilsTest.java +++ b/src/test/java/org/apache/commons/io/EndianUtilsTest.java @@ -43,6 +43,20 @@ public class EndianUtilsTest { assertThrows(EOFException.class, () -> EndianUtils.readSwappedDouble(input)); } + @Test + public void testInvalidOffset() throws IOException { + final byte[] bytes = new byte[0]; + + assertThrows(IllegalArgumentException.class, () -> EndianUtils.readSwappedInteger(bytes, 0)); + assertThrows(IllegalArgumentException.class, () -> EndianUtils.readSwappedLong(bytes, 0)); + assertThrows(IllegalArgumentException.class, () -> EndianUtils.readSwappedShort(bytes, 0)); + assertThrows(IllegalArgumentException.class, () -> EndianUtils.readSwappedUnsignedInteger(bytes, 0)); + assertThrows(IllegalArgumentException.class, () -> EndianUtils.readSwappedUnsignedShort(bytes, 0)); + assertThrows(IllegalArgumentException.class, () -> EndianUtils.writeSwappedInteger(bytes, 0, 0)); + assertThrows(IllegalArgumentException.class, () -> EndianUtils.writeSwappedLong(bytes, 0, 0l)); + assertThrows(IllegalArgumentException.class, () -> EndianUtils.writeSwappedShort(bytes, 0, (short) 0)); + } + @Test public void testReadSwappedDouble() throws IOException { final byte[] bytes = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; @@ -308,18 +322,4 @@ public class EndianUtilsTest { assertEquals( 0x01, bytes[1] ); } - @Test - public void testInvalidOffset() throws IOException { - final byte[] bytes = new byte[0]; - - assertThrows(IllegalArgumentException.class, () -> EndianUtils.readSwappedInteger(bytes, 0)); - assertThrows(IllegalArgumentException.class, () -> EndianUtils.readSwappedLong(bytes, 0)); - assertThrows(IllegalArgumentException.class, () -> EndianUtils.readSwappedShort(bytes, 0)); - assertThrows(IllegalArgumentException.class, () -> EndianUtils.readSwappedUnsignedInteger(bytes, 0)); - assertThrows(IllegalArgumentException.class, () -> EndianUtils.readSwappedUnsignedShort(bytes, 0)); - assertThrows(IllegalArgumentException.class, () -> EndianUtils.writeSwappedInteger(bytes, 0, 0)); - assertThrows(IllegalArgumentException.class, () -> EndianUtils.writeSwappedLong(bytes, 0, 0l)); - assertThrows(IllegalArgumentException.class, () -> EndianUtils.writeSwappedShort(bytes, 0, (short) 0)); - } - } diff --git a/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java b/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java index 5c309e59..be5c0b3f 100644 --- a/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java @@ -78,6 +78,17 @@ public class CharSequenceInputStreamTest { "Shift_JIS".equalsIgnoreCase(csName); } + @Test + // IO-781 available() returns 2 but only 1 byte is read afterwards + public void testAvailable() throws IOException { + final Charset charset = Charset.forName("Big5"); + final CharSequenceInputStream in = new CharSequenceInputStream("\uD800\uDC00", charset); + final int available = in.available(); + final byte[] data = new byte[available]; + final int bytesRead = in.read(data); + assertEquals(available, bytesRead); + } + @ParameterizedTest(name = "{0}") @MethodSource(CharsetsTest.AVAIL_CHARSETS) public void testAvailable(final String csName) throws Exception { @@ -511,15 +522,4 @@ public class CharSequenceInputStreamTest { assertEquals(-1, r.read(), csName); } } - - @Test - // IO-781 available() returns 2 but only 1 byte is read afterwards - public void testAvailable() throws IOException { - final Charset charset = Charset.forName("Big5"); - final CharSequenceInputStream in = new CharSequenceInputStream("\uD800\uDC00", charset); - final int available = in.available(); - final byte[] data = new byte[available]; - final int bytesRead = in.read(data); - assertEquals(available, bytesRead); - } }