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-codec.git
commit 275d73395c6433d6f67f825a96174d85ff4a8e3e Author: Gary Gregory <[email protected]> AuthorDate: Sat Mar 7 08:22:02 2026 -0500 Reuse IOUtils.toByteArray(InputStream) --- .../codec/binary/Base32InputStreamTest.java | 2 +- .../codec/binary/Base64InputStreamTest.java | 4 +- .../apache/commons/codec/binary/BaseNTestData.java | 65 +--------------------- 3 files changed, 6 insertions(+), 65 deletions(-) diff --git a/src/test/java/org/apache/commons/codec/binary/Base32InputStreamTest.java b/src/test/java/org/apache/commons/codec/binary/Base32InputStreamTest.java index 5d7c33d5..7a50ff7d 100644 --- a/src/test/java/org/apache/commons/codec/binary/Base32InputStreamTest.java +++ b/src/test/java/org/apache/commons/codec/binary/Base32InputStreamTest.java @@ -377,7 +377,7 @@ class Base32InputStreamTest { // we skip the first character read from the reader assertEquals(1, ins.skip(1)); - final byte[] decodedBytes = BaseNTestData.streamToBytes(ins, new byte[64]); + final byte[] decodedBytes = IOUtils.toByteArray(ins); final String str = StringUtils.newStringUtf8(decodedBytes); assertEquals(STRING_FIXTURE.substring(1), str); diff --git a/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java b/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java index cf8bcd43..0c5031ce 100644 --- a/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java +++ b/src/test/java/org/apache/commons/codec/binary/Base64InputStreamTest.java @@ -340,7 +340,7 @@ class Base64InputStreamTest { // we skip the first character read from the reader ins.skip(1); - final byte[] decodedBytes = BaseNTestData.streamToBytes(ins, new byte[64]); + final byte[] decodedBytes = IOUtils.toByteArray(ins); final String str = StringUtils.newStringUtf8(decodedBytes); assertEquals(STRING_FIXTURE.substring(1), str); @@ -359,7 +359,7 @@ class Base64InputStreamTest { final Base64InputStream stream = new Base64InputStream(data); // This line causes an NPE in commons-codec-1.4.jar: - final byte[] decodedBytes = BaseNTestData.streamToBytes(stream, new byte[1024]); + final byte[] decodedBytes = IOUtils.toByteArray(stream); final String decoded = StringUtils.newStringUtf8(decodedBytes); assertEquals(Base64TestData.CODEC_98_NPE_DECODED, decoded, "codec-98 NPE Base64InputStream"); diff --git a/src/test/java/org/apache/commons/codec/binary/BaseNTestData.java b/src/test/java/org/apache/commons/codec/binary/BaseNTestData.java index a38e59c2..c4064ab5 100644 --- a/src/test/java/org/apache/commons/codec/binary/BaseNTestData.java +++ b/src/test/java/org/apache/commons/codec/binary/BaseNTestData.java @@ -17,8 +17,6 @@ package org.apache.commons.codec.binary; -import java.io.IOException; -import java.io.InputStream; import java.util.Random; /** @@ -74,32 +72,12 @@ public class BaseNTestData { -22, 1, 127, -81, -4, -6, -119, 96, 35, -91, 114, 81, 91, 90, -86, -36, 34, -39, 93, -42, 69, 103, -11, 107, -87, 119, -107, -114, -45, -128, -69, 96}; - private static final int SIZE_KEY = 0; - - private static final int LAST_READ_KEY = 1; - - private static int[] fill(final byte[] buf, final int offset, final InputStream in) - throws IOException { - int read = in.read(buf, offset, buf.length - offset); - int lastRead = read; - if (read == -1) { - read = 0; - } - while (lastRead != -1 && read + offset < buf.length) { - lastRead = in.read(buf, offset + read, buf.length - read - offset); - if (lastRead != -1) { - read += lastRead; - } - } - return new int[]{offset + read, lastRead}; - } - /** * Returns an encoded and decoded copy of the same random data. * - * @param codec the codec to use - * @param size amount of random data to generate and encode - * @return two byte[] arrays: [0] = decoded, [1] = encoded + * @param codec the codec to use. + * @param size amount of random data to generate and encode. + * @return two byte[] arrays: [0] = decoded, [1] = encoded. */ static byte[][] randomData(final BaseNCodec codec, final int size) { final Random r = new Random(); @@ -108,41 +86,4 @@ public class BaseNTestData { final byte[] encoded = codec.encode(decoded); return new byte[][] {decoded, encoded}; } - - private static byte[] resizeArray(final byte[] bytes) { - final byte[] biggerBytes = new byte[bytes.length * 2]; - System.arraycopy(bytes, 0, biggerBytes, 0, bytes.length); - return biggerBytes; - } - - /** - * Reads all bytes from an InputStream into a byte array - * in chunks of {@code buf.length}. - * - * @param in the input stream. - * @param buf the byte array to use for chunking - * @return the bytes read from the input stream - * @throws IOException if an error occurs whilst reading the input stream - */ - static byte[] streamToBytes(final InputStream in, byte[] buf) throws IOException { - try { - int[] status = fill(buf, 0, in); - int size = status[SIZE_KEY]; - int lastRead = status[LAST_READ_KEY]; - while (lastRead != -1) { - buf = resizeArray(buf); - status = fill(buf, size, in); - size = status[SIZE_KEY]; - lastRead = status[LAST_READ_KEY]; - } - if (buf.length != size) { - final byte[] smallerBuf = new byte[size]; - System.arraycopy(buf, 0, smallerBuf, 0, size); - buf = smallerBuf; - } - } finally { - in.close(); - } - return buf; - } }
