@@ -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