Skip to content

Commit f51d19c

Browse files
committed
Sort members
1 parent 5c6a2e6 commit f51d19c

File tree

4 files changed

+131
-131
lines changed

4 files changed

+131
-131
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,12 @@ public static void checkFromIndexSize(final char[] array, final int off, final i
500500
checkFromIndexSize(off, len, Objects.requireNonNull(array, "char array").length);
501501
}
502502

503+
static void checkFromIndexSize(final int off, final int len, final int arrayLength) {
504+
if ((off | len | arrayLength) < 0 || arrayLength - len < off) {
505+
throw new IndexOutOfBoundsException(String.format("Range [%s, %<s + %s) out of bounds for length %s", off, len, arrayLength));
506+
}
507+
}
508+
503509
/**
504510
* Validates that the sub-range {@code [off, off + len)} is within the bounds of the given string.
505511
*
@@ -536,12 +542,6 @@ public static void checkFromIndexSize(final String str, final int off, final int
536542
checkFromIndexSize(off, len, Objects.requireNonNull(str, "str").length());
537543
}
538544

539-
static void checkFromIndexSize(final int off, final int len, final int arrayLength) {
540-
if ((off | len | arrayLength) < 0 || arrayLength - len < off) {
541-
throw new IndexOutOfBoundsException(String.format("Range [%s, %<s + %s) out of bounds for length %s", off, len, arrayLength));
542-
}
543-
}
544-
545545
/**
546546
* Validates that the sub-sequence {@code [fromIndex, toIndex)} is within the bounds of the given {@link CharSequence}.
547547
*

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,15 @@ List<File> list(final File startDirectory) throws IOException {
170170
*/
171171
private static final ListDirectoryWalker LIST_WALKER = new ListDirectoryWalker();
172172

173+
private static void setDosReadOnly(Path p, boolean readOnly) throws IOException {
174+
if (Files.getFileStore(p).supportsFileAttributeView(DosFileAttributeView.class)) {
175+
Files.setAttribute(p, "dos:readonly", readOnly, LinkOption.NOFOLLOW_LINKS);
176+
}
177+
}
173178
private File testFile1;
174179
private File testFile2;
175180
private long testFile1Size;
181+
176182
private long testFile2Size;
177183

178184
private void assertContentMatchesAfterCopyURLToFileFor(final String resourceName, final File destination) throws IOException {
@@ -1758,12 +1764,6 @@ void testForceDeleteReadOnlyFile() throws Exception {
17581764
}
17591765
}
17601766

1761-
private static void setDosReadOnly(Path p, boolean readOnly) throws IOException {
1762-
if (Files.getFileStore(p).supportsFileAttributeView(DosFileAttributeView.class)) {
1763-
Files.setAttribute(p, "dos:readonly", readOnly, LinkOption.NOFOLLOW_LINKS);
1764-
}
1765-
}
1766-
17671767
@Test
17681768
public void testForceDeleteSymlink() throws Exception {
17691769
final ImmutablePair<Path, Path> pair = createTempSymbolicLinkedRelativeDir();

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

Lines changed: 92 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -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)) {

src/test/java/org/apache/commons/io/input/UnsynchronizedBufferedReaderTest.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,33 @@ void testRead() throws IOException {
347347
}
348348
}
349349

350+
@Test
351+
void testReadArray_HARMONY_54() throws IOException {
352+
// Regression for HARMONY-54
353+
final char[] ch = {};
354+
@SuppressWarnings("resource")
355+
final UnsynchronizedBufferedReader reader = new UnsynchronizedBufferedReader(new CharArrayReader(ch));
356+
// Check exception thrown when the reader is open.
357+
assertThrows(NullPointerException.class, () -> reader.read(null, 1, 0));
358+
359+
// Now check IOException is thrown in preference to
360+
// NullPointerexception when the reader is closed.
361+
reader.close();
362+
assertThrows(IOException.class, () -> reader.read(null, 1, 0));
363+
364+
// And check that the IOException is thrown before
365+
// ArrayIndexOutOfBoundException
366+
assertThrows(IOException.class, () -> reader.read(ch, 0, 42));
367+
}
368+
369+
@Test
370+
void testReadArray_HARMONY_831() throws IOException {
371+
// regression for HARMONY-831
372+
try (Reader reader = new UnsynchronizedBufferedReader(new PipedReader(), 9)) {
373+
assertThrows(IndexOutOfBoundsException.class, () -> reader.read(new char[] {}, 7, 0));
374+
}
375+
}
376+
350377
/**
351378
* Tests {@link UnsynchronizedBufferedReader#read(char[], int, int)}.
352379
*
@@ -437,33 +464,6 @@ public boolean ready() throws IOException {
437464
}
438465
}
439466

440-
@Test
441-
void testReadArray_HARMONY_831() throws IOException {
442-
// regression for HARMONY-831
443-
try (Reader reader = new UnsynchronizedBufferedReader(new PipedReader(), 9)) {
444-
assertThrows(IndexOutOfBoundsException.class, () -> reader.read(new char[] {}, 7, 0));
445-
}
446-
}
447-
448-
@Test
449-
void testReadArray_HARMONY_54() throws IOException {
450-
// Regression for HARMONY-54
451-
final char[] ch = {};
452-
@SuppressWarnings("resource")
453-
final UnsynchronizedBufferedReader reader = new UnsynchronizedBufferedReader(new CharArrayReader(ch));
454-
// Check exception thrown when the reader is open.
455-
assertThrows(NullPointerException.class, () -> reader.read(null, 1, 0));
456-
457-
// Now check IOException is thrown in preference to
458-
// NullPointerexception when the reader is closed.
459-
reader.close();
460-
assertThrows(IOException.class, () -> reader.read(null, 1, 0));
461-
462-
// And check that the IOException is thrown before
463-
// ArrayIndexOutOfBoundException
464-
assertThrows(IOException.class, () -> reader.read(ch, 0, 42));
465-
}
466-
467467
/**
468468
* Tests {@link UnsynchronizedBufferedReader#read(char[], int, int)}.
469469
*

0 commit comments

Comments
 (0)