@@ -138,6 +138,71 @@ public static void beforeAll() {
138138 IO .clear ();
139139 }
140140
141+ static Stream <Arguments > invalidRead_InputStream_Offset_ArgumentsProvider () {
142+ final InputStream input = new ByteArrayInputStream (new byte [10 ]);
143+ final byte [] b = new byte [10 ];
144+ return Stream .of (
145+ // input is null
146+ Arguments .of (null , b , 0 , 1 , NullPointerException .class ),
147+ // b is null
148+ Arguments .of (input , null , 0 , 1 , NullPointerException .class ),
149+ // off is negative
150+ Arguments .of (input , b , -1 , 1 , IndexOutOfBoundsException .class ),
151+ // len is negative
152+ Arguments .of (input , b , 0 , -1 , IndexOutOfBoundsException .class ),
153+ // off + len is too big
154+ Arguments .of (input , b , 1 , 10 , IndexOutOfBoundsException .class ),
155+ // off + len is too big
156+ Arguments .of (input , b , 10 , 1 , IndexOutOfBoundsException .class )
157+ );
158+ }
159+
160+ static Stream <Arguments > testCheckFromIndexSizeInvalidCases () {
161+ return Stream .of (
162+ Arguments .of (-1 , 0 , 42 ),
163+ Arguments .of (0 , -1 , 42 ),
164+ Arguments .of (0 , 0 , -1 ),
165+ // off + len > arrayLength
166+ Arguments .of (1 , 42 , 42 ),
167+ Arguments .of (Integer .MAX_VALUE , 1 , Integer .MAX_VALUE )
168+ );
169+ }
170+
171+ static Stream <Arguments > testCheckFromIndexSizeValidCases () {
172+ return Stream .of (
173+ // Valid cases
174+ Arguments .of (0 , 0 , 42 ),
175+ Arguments .of (0 , 1 , 42 ),
176+ Arguments .of (0 , 42 , 42 ),
177+ Arguments .of (41 , 1 , 42 ),
178+ Arguments .of (42 , 0 , 42 )
179+ );
180+ }
181+
182+ static Stream <Arguments > testCheckFromToIndexInvalidCases () {
183+ return Stream .of (
184+ Arguments .of (-1 , 0 , 42 ),
185+ Arguments .of (0 , -1 , 42 ),
186+ Arguments .of (0 , 0 , -1 ),
187+ // from > to
188+ Arguments .of (1 , 0 , 42 ),
189+ // to > arrayLength
190+ Arguments .of (0 , 43 , 42 ),
191+ Arguments .of (1 , 43 , 42 )
192+ );
193+ }
194+
195+ static Stream <Arguments > testCheckFromToIndexValidCases () {
196+ return Stream .of (
197+ // Valid cases
198+ Arguments .of (0 , 0 , 42 ),
199+ Arguments .of (0 , 1 , 42 ),
200+ Arguments .of (0 , 42 , 42 ),
201+ Arguments .of (41 , 42 , 42 ),
202+ Arguments .of (42 , 42 , 42 )
203+ );
204+ }
205+
141206 private static Stream <Arguments > testToByteArray_InputStream_Size_BufferSize_Succeeds () {
142207 final byte [] data = new byte [1024 ];
143208 for (int i = 0 ; i < 1024 ; i ++) {
@@ -354,34 +419,6 @@ void testByteArrayWithNegativeSize() {
354419 assertThrows (NegativeArraySizeException .class , () -> IOUtils .byteArray (-1 ));
355420 }
356421
357- static Stream <Arguments > testCheckFromIndexSizeValidCases () {
358- return Stream .of (
359- // Valid cases
360- Arguments .of (0 , 0 , 42 ),
361- Arguments .of (0 , 1 , 42 ),
362- Arguments .of (0 , 42 , 42 ),
363- Arguments .of (41 , 1 , 42 ),
364- Arguments .of (42 , 0 , 42 )
365- );
366- }
367-
368- @ ParameterizedTest
369- @ MethodSource
370- void testCheckFromIndexSizeValidCases (int off , int len , int arrayLength ) {
371- assertDoesNotThrow (() -> IOUtils .checkFromIndexSize (off , len , arrayLength ));
372- }
373-
374- static Stream <Arguments > testCheckFromIndexSizeInvalidCases () {
375- return Stream .of (
376- Arguments .of (-1 , 0 , 42 ),
377- Arguments .of (0 , -1 , 42 ),
378- Arguments .of (0 , 0 , -1 ),
379- // off + len > arrayLength
380- Arguments .of (1 , 42 , 42 ),
381- Arguments .of (Integer .MAX_VALUE , 1 , Integer .MAX_VALUE )
382- );
383- }
384-
385422 @ ParameterizedTest
386423 @ MethodSource
387424 void testCheckFromIndexSizeInvalidCases (int off , int len , int arrayLength ) {
@@ -402,34 +439,10 @@ void testCheckFromIndexSizeInvalidCases(int off, int len, int arrayLength) {
402439 }
403440 }
404441
405- static Stream <Arguments > testCheckFromToIndexValidCases () {
406- return Stream .of (
407- // Valid cases
408- Arguments .of (0 , 0 , 42 ),
409- Arguments .of (0 , 1 , 42 ),
410- Arguments .of (0 , 42 , 42 ),
411- Arguments .of (41 , 42 , 42 ),
412- Arguments .of (42 , 42 , 42 )
413- );
414- }
415-
416442 @ ParameterizedTest
417443 @ MethodSource
418- void testCheckFromToIndexValidCases (int from , int to , int arrayLength ) {
419- assertDoesNotThrow (() -> IOUtils .checkFromToIndex (from , to , arrayLength ));
420- }
421-
422- static Stream <Arguments > testCheckFromToIndexInvalidCases () {
423- return Stream .of (
424- Arguments .of (-1 , 0 , 42 ),
425- Arguments .of (0 , -1 , 42 ),
426- Arguments .of (0 , 0 , -1 ),
427- // from > to
428- Arguments .of (1 , 0 , 42 ),
429- // to > arrayLength
430- Arguments .of (0 , 43 , 42 ),
431- Arguments .of (1 , 43 , 42 )
432- );
444+ void testCheckFromIndexSizeValidCases (int off , int len , int arrayLength ) {
445+ assertDoesNotThrow (() -> IOUtils .checkFromIndexSize (off , len , arrayLength ));
433446 }
434447
435448 @ ParameterizedTest
@@ -452,6 +465,12 @@ void testCheckFromToIndexInvalidCases(int from, int to, int arrayLength) {
452465 }
453466 }
454467
468+ @ ParameterizedTest
469+ @ MethodSource
470+ void testCheckFromToIndexValidCases (int from , int to , int arrayLength ) {
471+ assertDoesNotThrow (() -> IOUtils .checkFromToIndex (from , to , arrayLength ));
472+ }
473+
455474 @ Test
456475 void testClose () {
457476 assertDoesNotThrow (() -> IOUtils .close ((Closeable ) null ));
@@ -1147,6 +1166,12 @@ void testCopyLarge_SkipWithInvalidOffset() throws IOException {
11471166 }
11481167 }
11491168
1169+ @ ParameterizedTest
1170+ @ MethodSource ("invalidRead_InputStream_Offset_ArgumentsProvider" )
1171+ void testRead_InputStream_Offset_ArgumentsValidation (InputStream input , byte [] b , int off , int len , Class <? extends Throwable > expected ) {
1172+ assertThrows (expected , () -> IOUtils .read (input , b , off , len ));
1173+ }
1174+
11501175 @ Test
11511176 void testRead_ReadableByteChannel () throws Exception {
11521177 final ByteBuffer buffer = ByteBuffer .allocate (FILE_SIZE );
@@ -1164,31 +1189,6 @@ void testRead_ReadableByteChannel() throws Exception {
11641189 }
11651190 }
11661191
1167- static Stream <Arguments > invalidRead_InputStream_Offset_ArgumentsProvider () {
1168- final InputStream input = new ByteArrayInputStream (new byte [10 ]);
1169- final byte [] b = new byte [10 ];
1170- return Stream .of (
1171- // input is null
1172- Arguments .of (null , b , 0 , 1 , NullPointerException .class ),
1173- // b is null
1174- Arguments .of (input , null , 0 , 1 , NullPointerException .class ),
1175- // off is negative
1176- Arguments .of (input , b , -1 , 1 , IndexOutOfBoundsException .class ),
1177- // len is negative
1178- Arguments .of (input , b , 0 , -1 , IndexOutOfBoundsException .class ),
1179- // off + len is too big
1180- Arguments .of (input , b , 1 , 10 , IndexOutOfBoundsException .class ),
1181- // off + len is too big
1182- Arguments .of (input , b , 10 , 1 , IndexOutOfBoundsException .class )
1183- );
1184- }
1185-
1186- @ ParameterizedTest
1187- @ MethodSource ("invalidRead_InputStream_Offset_ArgumentsProvider" )
1188- void testRead_InputStream_Offset_ArgumentsValidation (InputStream input , byte [] b , int off , int len , Class <? extends Throwable > expected ) {
1189- assertThrows (expected , () -> IOUtils .read (input , b , off , len ));
1190- }
1191-
11921192 @ Test
11931193 void testReadFully_InputStream__ReturnByteArray () throws Exception {
11941194 final byte [] bytes = "abcd1234" .getBytes (StandardCharsets .UTF_8 );
@@ -1221,6 +1221,12 @@ void testReadFully_InputStream_Offset() throws Exception {
12211221 IOUtils .closeQuietly (stream );
12221222 }
12231223
1224+ @ ParameterizedTest
1225+ @ MethodSource ("invalidRead_InputStream_Offset_ArgumentsProvider" )
1226+ void testReadFully_InputStream_Offset_ArgumentsValidation (InputStream input , byte [] b , int off , int len , Class <? extends Throwable > expected ) {
1227+ assertThrows (expected , () -> IOUtils .read (input , b , off , len ));
1228+ }
1229+
12241230 @ Test
12251231 void testReadFully_ReadableByteChannel () throws Exception {
12261232 final ByteBuffer buffer = ByteBuffer .allocate (FILE_SIZE );
@@ -1265,12 +1271,6 @@ void testReadFully_Reader_Offset() throws Exception {
12651271 IOUtils .closeQuietly (reader );
12661272 }
12671273
1268- @ ParameterizedTest
1269- @ MethodSource ("invalidRead_InputStream_Offset_ArgumentsProvider" )
1270- void testReadFully_InputStream_Offset_ArgumentsValidation (InputStream input , byte [] b , int off , int len , Class <? extends Throwable > expected ) {
1271- assertThrows (expected , () -> IOUtils .read (input , b , off , len ));
1272- }
1273-
12741274 @ Test
12751275 void testReadLines_CharSequence () throws IOException {
12761276 final File file = TestUtils .newFile (temporaryFolder , "lines.txt" );
@@ -1745,13 +1745,6 @@ void testToByteArray_InputStream_Size() throws Exception {
17451745 }
17461746 }
17471747
1748- @ Test
1749- void testToByteArray_InputStream_Size_Truncated () throws Exception {
1750- try (InputStream in = new NullInputStream (0 )) {
1751- assertThrows (EOFException .class , () -> IOUtils .toByteArray (in , 1 ));
1752- }
1753- }
1754-
17551748 @ ParameterizedTest
17561749 @ MethodSource
17571750 void testToByteArray_InputStream_Size_BufferSize_Succeeds (final byte [] data , final int size , final int bufferSize ) throws IOException {
@@ -1770,6 +1763,13 @@ void testToByteArray_InputStream_Size_BufferSize_Throws(
17701763 }
17711764 }
17721765
1766+ @ Test
1767+ void testToByteArray_InputStream_Size_Truncated () throws Exception {
1768+ try (InputStream in = new NullInputStream (0 )) {
1769+ assertThrows (EOFException .class , () -> IOUtils .toByteArray (in , 1 ));
1770+ }
1771+ }
1772+
17731773 @ Test
17741774 void testToByteArray_InputStream_SizeIllegal () throws Exception {
17751775 try (InputStream fin = Files .newInputStream (testFilePath )) {
0 commit comments