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-io.git
The following commit(s) were added to refs/heads/master by this push:
new f51d19cab Sort members
f51d19cab is described below
commit f51d19cabad7b8c67e2992a2ab22465658249485
Author: Gary Gregory <[email protected]>
AuthorDate: Tue Oct 14 15:02:23 2025 -0400
Sort members
---
src/main/java/org/apache/commons/io/IOUtils.java | 12 +-
.../java/org/apache/commons/io/FileUtilsTest.java | 12 +-
.../java/org/apache/commons/io/IOUtilsTest.java | 184 ++++++++++-----------
.../io/input/UnsynchronizedBufferedReaderTest.java | 54 +++---
4 files changed, 131 insertions(+), 131 deletions(-)
diff --git a/src/main/java/org/apache/commons/io/IOUtils.java
b/src/main/java/org/apache/commons/io/IOUtils.java
index 52eb5c971..a1f9b0709 100644
--- a/src/main/java/org/apache/commons/io/IOUtils.java
+++ b/src/main/java/org/apache/commons/io/IOUtils.java
@@ -500,6 +500,12 @@ public static void checkFromIndexSize(final char[] array,
final int off, final i
checkFromIndexSize(off, len, Objects.requireNonNull(array, "char
array").length);
}
+ static void checkFromIndexSize(final int off, final int len, final int
arrayLength) {
+ if ((off | len | arrayLength) < 0 || arrayLength - len < off) {
+ throw new IndexOutOfBoundsException(String.format("Range [%s, %<s
+ %s) out of bounds for length %s", off, len, arrayLength));
+ }
+ }
+
/**
* Validates that the sub-range {@code [off, off + len)} is within the
bounds of the given string.
*
@@ -536,12 +542,6 @@ public static void checkFromIndexSize(final String str,
final int off, final int
checkFromIndexSize(off, len, Objects.requireNonNull(str,
"str").length());
}
- static void checkFromIndexSize(final int off, final int len, final int
arrayLength) {
- if ((off | len | arrayLength) < 0 || arrayLength - len < off) {
- throw new IndexOutOfBoundsException(String.format("Range [%s, %<s
+ %s) out of bounds for length %s", off, len, arrayLength));
- }
- }
-
/**
* Validates that the sub-sequence {@code [fromIndex, toIndex)} is within
the bounds of the given {@link CharSequence}.
*
diff --git a/src/test/java/org/apache/commons/io/FileUtilsTest.java
b/src/test/java/org/apache/commons/io/FileUtilsTest.java
index ced4b1066..ba9065d4f 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsTest.java
@@ -170,9 +170,15 @@ List<File> list(final File startDirectory) throws
IOException {
*/
private static final ListDirectoryWalker LIST_WALKER = new
ListDirectoryWalker();
+ private static void setDosReadOnly(Path p, boolean readOnly) throws
IOException {
+ if
(Files.getFileStore(p).supportsFileAttributeView(DosFileAttributeView.class)) {
+ Files.setAttribute(p, "dos:readonly", readOnly,
LinkOption.NOFOLLOW_LINKS);
+ }
+ }
private File testFile1;
private File testFile2;
private long testFile1Size;
+
private long testFile2Size;
private void assertContentMatchesAfterCopyURLToFileFor(final String
resourceName, final File destination) throws IOException {
@@ -1758,12 +1764,6 @@ void testForceDeleteReadOnlyFile() throws Exception {
}
}
- private static void setDosReadOnly(Path p, boolean readOnly) throws
IOException {
- if
(Files.getFileStore(p).supportsFileAttributeView(DosFileAttributeView.class)) {
- Files.setAttribute(p, "dos:readonly", readOnly,
LinkOption.NOFOLLOW_LINKS);
- }
- }
-
@Test
public void testForceDeleteSymlink() throws Exception {
final ImmutablePair<Path, Path> pair =
createTempSymbolicLinkedRelativeDir();
diff --git a/src/test/java/org/apache/commons/io/IOUtilsTest.java
b/src/test/java/org/apache/commons/io/IOUtilsTest.java
index 8a830ec31..e9fed5c0c 100644
--- a/src/test/java/org/apache/commons/io/IOUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/IOUtilsTest.java
@@ -138,6 +138,71 @@ public static void beforeAll() {
IO.clear();
}
+ static Stream<Arguments>
invalidRead_InputStream_Offset_ArgumentsProvider() {
+ final InputStream input = new ByteArrayInputStream(new byte[10]);
+ final byte[] b = new byte[10];
+ return Stream.of(
+ // input is null
+ Arguments.of(null, b, 0, 1, NullPointerException.class),
+ // b is null
+ Arguments.of(input, null, 0, 1, NullPointerException.class),
+ // off is negative
+ Arguments.of(input, b, -1, 1, IndexOutOfBoundsException.class),
+ // len is negative
+ Arguments.of(input, b, 0, -1, IndexOutOfBoundsException.class),
+ // off + len is too big
+ Arguments.of(input, b, 1, 10, IndexOutOfBoundsException.class),
+ // off + len is too big
+ Arguments.of(input, b, 10, 1, IndexOutOfBoundsException.class)
+ );
+ }
+
+ static Stream<Arguments> testCheckFromIndexSizeInvalidCases() {
+ return Stream.of(
+ Arguments.of(-1, 0, 42),
+ Arguments.of(0, -1, 42),
+ Arguments.of(0, 0, -1),
+ // off + len > arrayLength
+ Arguments.of(1, 42, 42),
+ Arguments.of(Integer.MAX_VALUE, 1, Integer.MAX_VALUE)
+ );
+ }
+
+ static Stream<Arguments> testCheckFromIndexSizeValidCases() {
+ return Stream.of(
+ // Valid cases
+ Arguments.of(0, 0, 42),
+ Arguments.of(0, 1, 42),
+ Arguments.of(0, 42, 42),
+ Arguments.of(41, 1, 42),
+ Arguments.of(42, 0, 42)
+ );
+ }
+
+ static Stream<Arguments> testCheckFromToIndexInvalidCases() {
+ return Stream.of(
+ Arguments.of(-1, 0, 42),
+ Arguments.of(0, -1, 42),
+ Arguments.of(0, 0, -1),
+ // from > to
+ Arguments.of(1, 0, 42),
+ // to > arrayLength
+ Arguments.of(0, 43, 42),
+ Arguments.of(1, 43, 42)
+ );
+ }
+
+ static Stream<Arguments> testCheckFromToIndexValidCases() {
+ return Stream.of(
+ // Valid cases
+ Arguments.of(0, 0, 42),
+ Arguments.of(0, 1, 42),
+ Arguments.of(0, 42, 42),
+ Arguments.of(41, 42, 42),
+ Arguments.of(42, 42, 42)
+ );
+ }
+
private static Stream<Arguments>
testToByteArray_InputStream_Size_BufferSize_Succeeds() {
final byte[] data = new byte[1024];
for (int i = 0; i < 1024; i++) {
@@ -354,34 +419,6 @@ void testByteArrayWithNegativeSize() {
assertThrows(NegativeArraySizeException.class, () ->
IOUtils.byteArray(-1));
}
- static Stream<Arguments> testCheckFromIndexSizeValidCases() {
- return Stream.of(
- // Valid cases
- Arguments.of(0, 0, 42),
- Arguments.of(0, 1, 42),
- Arguments.of(0, 42, 42),
- Arguments.of(41, 1, 42),
- Arguments.of(42, 0, 42)
- );
- }
-
- @ParameterizedTest
- @MethodSource
- void testCheckFromIndexSizeValidCases(int off, int len, int arrayLength) {
- assertDoesNotThrow(() -> IOUtils.checkFromIndexSize(off, len,
arrayLength));
- }
-
- static Stream<Arguments> testCheckFromIndexSizeInvalidCases() {
- return Stream.of(
- Arguments.of(-1, 0, 42),
- Arguments.of(0, -1, 42),
- Arguments.of(0, 0, -1),
- // off + len > arrayLength
- Arguments.of(1, 42, 42),
- Arguments.of(Integer.MAX_VALUE, 1, Integer.MAX_VALUE)
- );
- }
-
@ParameterizedTest
@MethodSource
void testCheckFromIndexSizeInvalidCases(int off, int len, int arrayLength)
{
@@ -402,34 +439,10 @@ void testCheckFromIndexSizeInvalidCases(int off, int len,
int arrayLength) {
}
}
- static Stream<Arguments> testCheckFromToIndexValidCases() {
- return Stream.of(
- // Valid cases
- Arguments.of(0, 0, 42),
- Arguments.of(0, 1, 42),
- Arguments.of(0, 42, 42),
- Arguments.of(41, 42, 42),
- Arguments.of(42, 42, 42)
- );
- }
-
@ParameterizedTest
@MethodSource
- void testCheckFromToIndexValidCases(int from, int to, int arrayLength) {
- assertDoesNotThrow(() -> IOUtils.checkFromToIndex(from, to,
arrayLength));
- }
-
- static Stream<Arguments> testCheckFromToIndexInvalidCases() {
- return Stream.of(
- Arguments.of(-1, 0, 42),
- Arguments.of(0, -1, 42),
- Arguments.of(0, 0, -1),
- // from > to
- Arguments.of(1, 0, 42),
- // to > arrayLength
- Arguments.of(0, 43, 42),
- Arguments.of(1, 43, 42)
- );
+ void testCheckFromIndexSizeValidCases(int off, int len, int arrayLength) {
+ assertDoesNotThrow(() -> IOUtils.checkFromIndexSize(off, len,
arrayLength));
}
@ParameterizedTest
@@ -452,6 +465,12 @@ void testCheckFromToIndexInvalidCases(int from, int to,
int arrayLength) {
}
}
+ @ParameterizedTest
+ @MethodSource
+ void testCheckFromToIndexValidCases(int from, int to, int arrayLength) {
+ assertDoesNotThrow(() -> IOUtils.checkFromToIndex(from, to,
arrayLength));
+ }
+
@Test
void testClose() {
assertDoesNotThrow(() -> IOUtils.close((Closeable) null));
@@ -1147,6 +1166,12 @@ void testCopyLarge_SkipWithInvalidOffset() throws
IOException {
}
}
+ @ParameterizedTest
+ @MethodSource("invalidRead_InputStream_Offset_ArgumentsProvider")
+ void testRead_InputStream_Offset_ArgumentsValidation(InputStream input,
byte[] b, int off, int len, Class<? extends Throwable> expected) {
+ assertThrows(expected, () -> IOUtils.read(input, b, off, len));
+ }
+
@Test
void testRead_ReadableByteChannel() throws Exception {
final ByteBuffer buffer = ByteBuffer.allocate(FILE_SIZE);
@@ -1164,31 +1189,6 @@ void testRead_ReadableByteChannel() throws Exception {
}
}
- static Stream<Arguments>
invalidRead_InputStream_Offset_ArgumentsProvider() {
- final InputStream input = new ByteArrayInputStream(new byte[10]);
- final byte[] b = new byte[10];
- return Stream.of(
- // input is null
- Arguments.of(null, b, 0, 1, NullPointerException.class),
- // b is null
- Arguments.of(input, null, 0, 1, NullPointerException.class),
- // off is negative
- Arguments.of(input, b, -1, 1, IndexOutOfBoundsException.class),
- // len is negative
- Arguments.of(input, b, 0, -1, IndexOutOfBoundsException.class),
- // off + len is too big
- Arguments.of(input, b, 1, 10, IndexOutOfBoundsException.class),
- // off + len is too big
- Arguments.of(input, b, 10, 1, IndexOutOfBoundsException.class)
- );
- }
-
- @ParameterizedTest
- @MethodSource("invalidRead_InputStream_Offset_ArgumentsProvider")
- void testRead_InputStream_Offset_ArgumentsValidation(InputStream input,
byte[] b, int off, int len, Class<? extends Throwable> expected) {
- assertThrows(expected, () -> IOUtils.read(input, b, off, len));
- }
-
@Test
void testReadFully_InputStream__ReturnByteArray() throws Exception {
final byte[] bytes = "abcd1234".getBytes(StandardCharsets.UTF_8);
@@ -1221,6 +1221,12 @@ void testReadFully_InputStream_Offset() throws Exception
{
IOUtils.closeQuietly(stream);
}
+ @ParameterizedTest
+ @MethodSource("invalidRead_InputStream_Offset_ArgumentsProvider")
+ void testReadFully_InputStream_Offset_ArgumentsValidation(InputStream
input, byte[] b, int off, int len, Class<? extends Throwable> expected) {
+ assertThrows(expected, () -> IOUtils.read(input, b, off, len));
+ }
+
@Test
void testReadFully_ReadableByteChannel() throws Exception {
final ByteBuffer buffer = ByteBuffer.allocate(FILE_SIZE);
@@ -1265,12 +1271,6 @@ void testReadFully_Reader_Offset() throws Exception {
IOUtils.closeQuietly(reader);
}
- @ParameterizedTest
- @MethodSource("invalidRead_InputStream_Offset_ArgumentsProvider")
- void testReadFully_InputStream_Offset_ArgumentsValidation(InputStream
input, byte[] b, int off, int len, Class<? extends Throwable> expected) {
- assertThrows(expected, () -> IOUtils.read(input, b, off, len));
- }
-
@Test
void testReadLines_CharSequence() throws IOException {
final File file = TestUtils.newFile(temporaryFolder, "lines.txt");
@@ -1745,13 +1745,6 @@ void testToByteArray_InputStream_Size() throws Exception
{
}
}
- @Test
- void testToByteArray_InputStream_Size_Truncated() throws Exception {
- try (InputStream in = new NullInputStream(0)) {
- assertThrows(EOFException.class, () -> IOUtils.toByteArray(in, 1));
- }
- }
-
@ParameterizedTest
@MethodSource
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(
}
}
+ @Test
+ void testToByteArray_InputStream_Size_Truncated() throws Exception {
+ try (InputStream in = new NullInputStream(0)) {
+ assertThrows(EOFException.class, () -> IOUtils.toByteArray(in, 1));
+ }
+ }
+
@Test
void testToByteArray_InputStream_SizeIllegal() throws Exception {
try (InputStream fin = Files.newInputStream(testFilePath)) {
diff --git
a/src/test/java/org/apache/commons/io/input/UnsynchronizedBufferedReaderTest.java
b/src/test/java/org/apache/commons/io/input/UnsynchronizedBufferedReaderTest.java
index a144bc418..2a2b21a22 100644
---
a/src/test/java/org/apache/commons/io/input/UnsynchronizedBufferedReaderTest.java
+++
b/src/test/java/org/apache/commons/io/input/UnsynchronizedBufferedReaderTest.java
@@ -347,6 +347,33 @@ void testRead() throws IOException {
}
}
+ @Test
+ void testReadArray_HARMONY_54() throws IOException {
+ // Regression for HARMONY-54
+ final char[] ch = {};
+ @SuppressWarnings("resource")
+ final UnsynchronizedBufferedReader reader = new
UnsynchronizedBufferedReader(new CharArrayReader(ch));
+ // Check exception thrown when the reader is open.
+ assertThrows(NullPointerException.class, () -> reader.read(null, 1,
0));
+
+ // Now check IOException is thrown in preference to
+ // NullPointerexception when the reader is closed.
+ reader.close();
+ assertThrows(IOException.class, () -> reader.read(null, 1, 0));
+
+ // And check that the IOException is thrown before
+ // ArrayIndexOutOfBoundException
+ assertThrows(IOException.class, () -> reader.read(ch, 0, 42));
+ }
+
+ @Test
+ void testReadArray_HARMONY_831() throws IOException {
+ // regression for HARMONY-831
+ try (Reader reader = new UnsynchronizedBufferedReader(new
PipedReader(), 9)) {
+ assertThrows(IndexOutOfBoundsException.class, () ->
reader.read(new char[] {}, 7, 0));
+ }
+ }
+
/**
* Tests {@link UnsynchronizedBufferedReader#read(char[], int, int)}.
*
@@ -437,33 +464,6 @@ public boolean ready() throws IOException {
}
}
- @Test
- void testReadArray_HARMONY_831() throws IOException {
- // regression for HARMONY-831
- try (Reader reader = new UnsynchronizedBufferedReader(new
PipedReader(), 9)) {
- assertThrows(IndexOutOfBoundsException.class, () ->
reader.read(new char[] {}, 7, 0));
- }
- }
-
- @Test
- void testReadArray_HARMONY_54() throws IOException {
- // Regression for HARMONY-54
- final char[] ch = {};
- @SuppressWarnings("resource")
- final UnsynchronizedBufferedReader reader = new
UnsynchronizedBufferedReader(new CharArrayReader(ch));
- // Check exception thrown when the reader is open.
- assertThrows(NullPointerException.class, () -> reader.read(null, 1,
0));
-
- // Now check IOException is thrown in preference to
- // NullPointerexception when the reader is closed.
- reader.close();
- assertThrows(IOException.class, () -> reader.read(null, 1, 0));
-
- // And check that the IOException is thrown before
- // ArrayIndexOutOfBoundException
- assertThrows(IOException.class, () -> reader.read(ch, 0, 42));
- }
-
/**
* Tests {@link UnsynchronizedBufferedReader#read(char[], int, int)}.
*