Skip to content

Commit 44593a4

Browse files
committed
Sort members
1 parent 2330b08 commit 44593a4

File tree

2 files changed

+86
-86
lines changed

2 files changed

+86
-86
lines changed

src/main/java/org/apache/commons/io/IOUtils.java

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,30 @@ public static long copyLarge(final Reader reader, final Writer writer, final lon
16341634
return totalRead;
16351635
}
16361636

1637+
/**
1638+
* Copies up to {@code size} bytes from the given {@link InputStream} into a new {@link UnsynchronizedByteArrayOutputStream}.
1639+
*
1640+
* @param input The {@link InputStream} to read; must not be {@code null}.
1641+
* @param limit The maximum number of bytes to read; must be {@code >= 0}.
1642+
* The actual bytes read are validated to equal {@code size}.
1643+
* @param bufferSize The buffer size of the output stream; must be {@code > 0}.
1644+
* @return a ByteArrayOutputStream containing the read bytes.
1645+
*/
1646+
private static UnsynchronizedByteArrayOutputStream copyToOutputStream(
1647+
final InputStream input, final long limit, final int bufferSize) throws IOException {
1648+
try (UnsynchronizedByteArrayOutputStream output = UnsynchronizedByteArrayOutputStream.builder()
1649+
.setBufferSize(bufferSize)
1650+
.get();
1651+
InputStream boundedInput = BoundedInputStream.builder()
1652+
.setMaxCount(limit)
1653+
.setPropagateClose(false)
1654+
.setInputStream(input)
1655+
.get()) {
1656+
output.write(boundedInput);
1657+
return output;
1658+
}
1659+
}
1660+
16371661
/**
16381662
* Fills the given array with 0s.
16391663
*
@@ -2687,30 +2711,6 @@ public static byte[] toByteArray(final InputStream input, final int size) throws
26872711
return toByteArray(Objects.requireNonNull(input, "input")::read, size);
26882712
}
26892713

2690-
/**
2691-
* Reads exactly {@code size} bytes from the given {@link InputStream} into a new {@code byte[]}.
2692-
*
2693-
* <p>This variant always allocates the whole requested array size,
2694-
* for a dynamic growing variant use {@link #toByteArray(InputStream, int, int)},
2695-
* which enforces stricter memory usage constraints.</p>
2696-
*
2697-
* @param input the {@link InputStream} to read; must not be {@code null}.
2698-
* @param size the exact number of bytes to read; must be {@code >= 0} and {@code <= Integer.MAX_VALUE}.
2699-
* @return a new byte array of length {@code size}.
2700-
* @throws IllegalArgumentException if {@code size} is negative or does not fit into an int.
2701-
* @throws EOFException if the stream ends before {@code size} bytes are read.
2702-
* @throws IOException if an I/O error occurs while reading.
2703-
* @throws NullPointerException if {@code input} is {@code null}.
2704-
* @see #toByteArray(InputStream, int, int)
2705-
* @since 2.1
2706-
*/
2707-
public static byte[] toByteArray(final InputStream input, final long size) throws IOException {
2708-
if (size > Integer.MAX_VALUE) {
2709-
throw new IllegalArgumentException("Size cannot be greater than Integer max value: " + size);
2710-
}
2711-
return toByteArray(input, (int) size);
2712-
}
2713-
27142714
/**
27152715
* Reads exactly {@code size} bytes from the given {@link InputStream} into a new {@code byte[]}.
27162716
*
@@ -2750,27 +2750,27 @@ public static byte[] toByteArray(final InputStream input, final int size, final
27502750
}
27512751

27522752
/**
2753-
* Copies up to {@code size} bytes from the given {@link InputStream} into a new {@link UnsynchronizedByteArrayOutputStream}.
2753+
* Reads exactly {@code size} bytes from the given {@link InputStream} into a new {@code byte[]}.
27542754
*
2755-
* @param input The {@link InputStream} to read; must not be {@code null}.
2756-
* @param limit The maximum number of bytes to read; must be {@code >= 0}.
2757-
* The actual bytes read are validated to equal {@code size}.
2758-
* @param bufferSize The buffer size of the output stream; must be {@code > 0}.
2759-
* @return a ByteArrayOutputStream containing the read bytes.
2755+
* <p>This variant always allocates the whole requested array size,
2756+
* for a dynamic growing variant use {@link #toByteArray(InputStream, int, int)},
2757+
* which enforces stricter memory usage constraints.</p>
2758+
*
2759+
* @param input the {@link InputStream} to read; must not be {@code null}.
2760+
* @param size the exact number of bytes to read; must be {@code >= 0} and {@code <= Integer.MAX_VALUE}.
2761+
* @return a new byte array of length {@code size}.
2762+
* @throws IllegalArgumentException if {@code size} is negative or does not fit into an int.
2763+
* @throws EOFException if the stream ends before {@code size} bytes are read.
2764+
* @throws IOException if an I/O error occurs while reading.
2765+
* @throws NullPointerException if {@code input} is {@code null}.
2766+
* @see #toByteArray(InputStream, int, int)
2767+
* @since 2.1
27602768
*/
2761-
private static UnsynchronizedByteArrayOutputStream copyToOutputStream(
2762-
final InputStream input, final long limit, final int bufferSize) throws IOException {
2763-
try (UnsynchronizedByteArrayOutputStream output = UnsynchronizedByteArrayOutputStream.builder()
2764-
.setBufferSize(bufferSize)
2765-
.get();
2766-
InputStream boundedInput = BoundedInputStream.builder()
2767-
.setMaxCount(limit)
2768-
.setPropagateClose(false)
2769-
.setInputStream(input)
2770-
.get()) {
2771-
output.write(boundedInput);
2772-
return output;
2769+
public static byte[] toByteArray(final InputStream input, final long size) throws IOException {
2770+
if (size > Integer.MAX_VALUE) {
2771+
throw new IllegalArgumentException("Size cannot be greater than Integer max value: " + size);
27732772
}
2773+
return toByteArray(input, (int) size);
27742774
}
27752775

27762776
/**

src/test/java/org/apache/commons/io/IOUtilsTest.java

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,30 @@ public static void beforeAll() {
128128
IO.clear();
129129
}
130130

131+
private static Stream<Arguments> testToByteArray_InputStream_Size_BufferSize_Succeeds() {
132+
final byte[] data = new byte[1024];
133+
for (int i = 0; i < 1024; i++) {
134+
data[i] = (byte) i;
135+
}
136+
return Stream.of(
137+
// Eager reading
138+
Arguments.of(data.clone(), 512, 1024),
139+
// Incremental reading
140+
Arguments.of(data.clone(), 1024, 512),
141+
// No reading
142+
Arguments.of(data.clone(), 0, 128));
143+
}
144+
145+
static Stream<Arguments> testToByteArray_InputStream_Size_BufferSize_Throws() {
146+
return Stream.of(
147+
// Negative size
148+
Arguments.of(-1, 128, IllegalArgumentException.class),
149+
// Invalid buffer size
150+
Arguments.of(0, 0, IllegalArgumentException.class),
151+
// Huge size: should not cause OutOfMemoryError
152+
Arguments.of(Integer.MAX_VALUE, 128, EOFException.class));
153+
}
154+
131155
@TempDir
132156
public File temporaryFolder;
133157

@@ -1291,6 +1315,8 @@ void testResourceToString_ExistingResourceAtRootPackage() throws Exception {
12911315
assertEquals(fileSize, content.getBytes().length);
12921316
}
12931317

1318+
// Tests from IO-305
1319+
12941320
@Test
12951321
void testResourceToString_ExistingResourceAtRootPackage_WithClassLoader() throws Exception {
12961322
final long fileSize = TestResources.getFile("test-file-simple-utf8.bin").length();
@@ -1311,8 +1337,6 @@ void testResourceToString_ExistingResourceAtSubPackage() throws Exception {
13111337
assertEquals(fileSize, content.getBytes().length);
13121338
}
13131339

1314-
// Tests from IO-305
1315-
13161340
@Test
13171341
void testResourceToString_ExistingResourceAtSubPackage_WithClassLoader() throws Exception {
13181342
final long fileSize = TestResources.getFile("FileUtilsTestDataCR.dat").length();
@@ -1625,6 +1649,24 @@ void testToByteArray_InputStream_Size() throws Exception {
16251649
}
16261650
}
16271651

1652+
@ParameterizedTest
1653+
@MethodSource
1654+
void testToByteArray_InputStream_Size_BufferSize_Succeeds(byte[] data, int size, int bufferSize) throws IOException {
1655+
final ByteArrayInputStream input = new ByteArrayInputStream(data);
1656+
final byte[] expected = Arrays.copyOf(data, size);
1657+
final byte[] actual = IOUtils.toByteArray(input, size, bufferSize);
1658+
assertArrayEquals(expected, actual);
1659+
}
1660+
1661+
@ParameterizedTest
1662+
@MethodSource
1663+
void testToByteArray_InputStream_Size_BufferSize_Throws(
1664+
int size, int bufferSize, Class<? extends Exception> exceptionClass) throws IOException {
1665+
try (InputStream input = new NullInputStream(0)) {
1666+
assertThrows(exceptionClass, () -> IOUtils.toByteArray(input, size, bufferSize));
1667+
}
1668+
}
1669+
16281670
@Test
16291671
void testToByteArray_InputStream_SizeIllegal() throws Exception {
16301672
try (InputStream fin = Files.newInputStream(testFilePath)) {
@@ -1662,48 +1704,6 @@ void testToByteArray_InputStream_SizeZero() throws Exception {
16621704
}
16631705
}
16641706

1665-
@ParameterizedTest
1666-
@MethodSource
1667-
void testToByteArray_InputStream_Size_BufferSize_Succeeds(byte[] data, int size, int bufferSize) throws IOException {
1668-
final ByteArrayInputStream input = new ByteArrayInputStream(data);
1669-
final byte[] expected = Arrays.copyOf(data, size);
1670-
final byte[] actual = IOUtils.toByteArray(input, size, bufferSize);
1671-
assertArrayEquals(expected, actual);
1672-
}
1673-
1674-
private static Stream<Arguments> testToByteArray_InputStream_Size_BufferSize_Succeeds() {
1675-
final byte[] data = new byte[1024];
1676-
for (int i = 0; i < 1024; i++) {
1677-
data[i] = (byte) i;
1678-
}
1679-
return Stream.of(
1680-
// Eager reading
1681-
Arguments.of(data.clone(), 512, 1024),
1682-
// Incremental reading
1683-
Arguments.of(data.clone(), 1024, 512),
1684-
// No reading
1685-
Arguments.of(data.clone(), 0, 128));
1686-
}
1687-
1688-
@ParameterizedTest
1689-
@MethodSource
1690-
void testToByteArray_InputStream_Size_BufferSize_Throws(
1691-
int size, int bufferSize, Class<? extends Exception> exceptionClass) throws IOException {
1692-
try (InputStream input = new NullInputStream(0)) {
1693-
assertThrows(exceptionClass, () -> IOUtils.toByteArray(input, size, bufferSize));
1694-
}
1695-
}
1696-
1697-
static Stream<Arguments> testToByteArray_InputStream_Size_BufferSize_Throws() {
1698-
return Stream.of(
1699-
// Negative size
1700-
Arguments.of(-1, 128, IllegalArgumentException.class),
1701-
// Invalid buffer size
1702-
Arguments.of(0, 0, IllegalArgumentException.class),
1703-
// Huge size: should not cause OutOfMemoryError
1704-
Arguments.of(Integer.MAX_VALUE, 128, EOFException.class));
1705-
}
1706-
17071707
@Test
17081708
void testToByteArray_Reader() throws IOException {
17091709
final String charsetName = UTF_8;

0 commit comments

Comments
 (0)