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
commit 2f18e22169757096469d693dc73db53825168a06 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Mon Nov 11 10:34:50 2024 -0500 Sort members --- src/main/java/org/apache/commons/io/IOUtils.java | 28 +++---- .../commons/io/input/AutoCloseInputStream.java | 8 +- .../io/input/BufferedFileChannelInputStream.java | 12 +-- .../apache/commons/io/input/ProxyInputStream.java | 24 +++--- .../io/output/RandomAccessFileOutputStream.java | 10 +-- .../commons/io/input/BoundedInputStreamTest.java | 90 +++++++++++----------- .../io/input/UnsynchronizedBufferedReaderTest.java | 58 +++++++------- .../output/RandomAccessFileOutputStreamTest.java | 18 ++--- 8 files changed, 124 insertions(+), 124 deletions(-) diff --git a/src/main/java/org/apache/commons/io/IOUtils.java b/src/main/java/org/apache/commons/io/IOUtils.java index 15c0bdfd6..72bc4c4ca 100644 --- a/src/main/java/org/apache/commons/io/IOUtils.java +++ b/src/main/java/org/apache/commons/io/IOUtils.java @@ -2162,6 +2162,20 @@ public class IOUtils { } } + /** + * Gets the contents of a {@link CharSequence} as a list of Strings, one entry per line. + * + * @param csq the {@link CharSequence} to read, not null + * @return the list of Strings, never null + * @throws UncheckedIOException if an I/O error occurs + * @since 2.18.0 + */ + public static List<String> readLines(final CharSequence csq) throws UncheckedIOException { + try (CharSequenceReader reader = new CharSequenceReader(csq)) { + return readLines(reader); + } + } + /** * Gets the contents of an {@link InputStream} as a list of Strings, * one entry per line, using the default character encoding of the platform. @@ -2244,20 +2258,6 @@ public class IOUtils { return toBufferedReader(reader).lines().collect(Collectors.toList()); } - /** - * Gets the contents of a {@link CharSequence} as a list of Strings, one entry per line. - * - * @param csq the {@link CharSequence} to read, not null - * @return the list of Strings, never null - * @throws UncheckedIOException if an I/O error occurs - * @since 2.18.0 - */ - public static List<String> readLines(final CharSequence csq) throws UncheckedIOException { - try (CharSequenceReader reader = new CharSequenceReader(csq)) { - return readLines(reader); - } - } - /** * Gets the contents of a resource as a byte array. * <p> diff --git a/src/main/java/org/apache/commons/io/input/AutoCloseInputStream.java b/src/main/java/org/apache/commons/io/input/AutoCloseInputStream.java index 2f70b6d2b..de7451fdd 100644 --- a/src/main/java/org/apache/commons/io/input/AutoCloseInputStream.java +++ b/src/main/java/org/apache/commons/io/input/AutoCloseInputStream.java @@ -97,6 +97,10 @@ public class AutoCloseInputStream extends ProxyInputStream { return new Builder(); } + private AutoCloseInputStream(final Builder builder) throws IOException { + super(builder); + } + /** * Constructs an automatically closing proxy for the given input stream. * @@ -109,10 +113,6 @@ public class AutoCloseInputStream extends ProxyInputStream { super(ClosedInputStream.ifNull(in)); } - private AutoCloseInputStream(final Builder builder) throws IOException { - super(builder); - } - /** * Automatically closes the stream if the end of stream was reached. * diff --git a/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java b/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java index bede06b46..b92eba0ad 100644 --- a/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java +++ b/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java @@ -157,6 +157,12 @@ public final class BufferedFileChannelInputStream extends InputStream { this(file.toPath(), bufferSize); } + private BufferedFileChannelInputStream(final FileChannel fileChannel, final int bufferSize) { + this.fileChannel = Objects.requireNonNull(fileChannel, "path"); + byteBuffer = ByteBuffer.allocateDirect(bufferSize); + byteBuffer.flip(); + } + /** * Constructs a new instance for the given Path. * @@ -183,12 +189,6 @@ public final class BufferedFileChannelInputStream extends InputStream { this(FileChannel.open(path, StandardOpenOption.READ), bufferSize); } - private BufferedFileChannelInputStream(final FileChannel fileChannel, final int bufferSize) { - this.fileChannel = Objects.requireNonNull(fileChannel, "path"); - byteBuffer = ByteBuffer.allocateDirect(bufferSize); - byteBuffer.flip(); - } - @Override public synchronized int available() throws IOException { if (!fileChannel.isOpen()) { diff --git a/src/main/java/org/apache/commons/io/input/ProxyInputStream.java b/src/main/java/org/apache/commons/io/input/ProxyInputStream.java index 8c359c4ac..e529a21f3 100644 --- a/src/main/java/org/apache/commons/io/input/ProxyInputStream.java +++ b/src/main/java/org/apache/commons/io/input/ProxyInputStream.java @@ -102,18 +102,6 @@ public abstract class ProxyInputStream extends FilterInputStream { private final IOIntConsumer afterRead; - /** - * Constructs a new ProxyInputStream. - * - * @param proxy the InputStream to proxy. - */ - public ProxyInputStream(final InputStream proxy) { - // the delegate is stored in a protected superclass variable named 'in'. - super(proxy); - this.exceptionHandler = Erase::rethrow; - this.afterRead = IOIntConsumer.NOOP; - } - /** * Constructs a new ProxyInputStream. * @@ -127,6 +115,18 @@ public abstract class ProxyInputStream extends FilterInputStream { this(builder.getInputStream(), builder); } + /** + * Constructs a new ProxyInputStream. + * + * @param proxy the InputStream to proxy. + */ + public ProxyInputStream(final InputStream proxy) { + // the delegate is stored in a protected superclass variable named 'in'. + super(proxy); + this.exceptionHandler = Erase::rethrow; + this.afterRead = IOIntConsumer.NOOP; + } + /** * Constructs a new ProxyInputStream. * diff --git a/src/main/java/org/apache/commons/io/output/RandomAccessFileOutputStream.java b/src/main/java/org/apache/commons/io/output/RandomAccessFileOutputStream.java index 4aa1e1d30..0edaba63e 100644 --- a/src/main/java/org/apache/commons/io/output/RandomAccessFileOutputStream.java +++ b/src/main/java/org/apache/commons/io/output/RandomAccessFileOutputStream.java @@ -84,8 +84,9 @@ public final class RandomAccessFileOutputStream extends OutputStream { } @Override - public void write(final int b) throws IOException { - randomAccessFile.write(b); + public void close() throws IOException { + this.randomAccessFile.close(); + super.close(); } @SuppressWarnings("resource") @@ -96,9 +97,8 @@ public final class RandomAccessFileOutputStream extends OutputStream { } @Override - public void close() throws IOException { - this.randomAccessFile.close(); - super.close(); + public void write(final int b) throws IOException { + randomAccessFile.write(b); } } diff --git a/src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java b/src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java index f432d7a96..aa2849486 100644 --- a/src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/BoundedInputStreamTest.java @@ -51,6 +51,35 @@ public class BoundedInputStreamTest { } } + @Test + public void testAfterReadConsumer() throws Exception { + final byte[] hello = "Hello".getBytes(StandardCharsets.UTF_8); + final AtomicBoolean boolRef = new AtomicBoolean(); + // @formatter:off + try (InputStream bounded = BoundedInputStream.builder() + .setInputStream(new ByteArrayInputStream(hello)) + .setMaxCount(hello.length) + .setAfterRead(i -> boolRef.set(true)) + .get()) { + IOUtils.consume(bounded); + } + // @formatter:on + assertTrue(boolRef.get()); + // Throwing + final String message = "test exception message"; + // @formatter:off + try (InputStream bounded = BoundedInputStream.builder() + .setInputStream(new ByteArrayInputStream(hello)) + .setMaxCount(hello.length) + .setAfterRead(i -> { + throw new CustomIOException(message); + }) + .get()) { + assertEquals(message, assertThrowsExactly(CustomIOException.class, () -> IOUtils.consume(bounded)).getMessage()); + } + // @formatter:on + } + @SuppressWarnings("resource") @Test public void testAvailableAfterClose() throws Exception { @@ -80,22 +109,6 @@ public class BoundedInputStreamTest { ProxyInputStreamTest.testCloseHandleIOException(BoundedInputStream.builder()); } - @SuppressWarnings("deprecation") - @Test - public void testPublicConstructors() throws IOException { - final byte[] helloWorld = "Hello World".getBytes(StandardCharsets.UTF_8); - try (ByteArrayInputStream baos = new ByteArrayInputStream(helloWorld); - BoundedInputStream inputStream = new BoundedInputStream(baos)) { - assertSame(baos, inputStream.unwrap()); - } - final long maxCount = 2; - try (ByteArrayInputStream baos = new ByteArrayInputStream(helloWorld); - BoundedInputStream inputStream = new BoundedInputStream(baos, maxCount)) { - assertSame(baos, inputStream.unwrap()); - assertSame(maxCount, inputStream.getMaxCount()); - } - } - @ParameterizedTest @ValueSource(longs = { -100, -1, 0, 1, 2, 4, 8, 16, 32, 64 }) public void testCounts(final long startCount) throws Exception { @@ -287,35 +300,6 @@ public class BoundedInputStreamTest { } } - @Test - public void testAfterReadConsumer() throws Exception { - final byte[] hello = "Hello".getBytes(StandardCharsets.UTF_8); - final AtomicBoolean boolRef = new AtomicBoolean(); - // @formatter:off - try (InputStream bounded = BoundedInputStream.builder() - .setInputStream(new ByteArrayInputStream(hello)) - .setMaxCount(hello.length) - .setAfterRead(i -> boolRef.set(true)) - .get()) { - IOUtils.consume(bounded); - } - // @formatter:on - assertTrue(boolRef.get()); - // Throwing - final String message = "test exception message"; - // @formatter:off - try (InputStream bounded = BoundedInputStream.builder() - .setInputStream(new ByteArrayInputStream(hello)) - .setMaxCount(hello.length) - .setAfterRead(i -> { - throw new CustomIOException(message); - }) - .get()) { - assertEquals(message, assertThrowsExactly(CustomIOException.class, () -> IOUtils.consume(bounded)).getMessage()); - } - // @formatter:on - } - @Test public void testOnMaxCountConsumer() throws Exception { final byte[] hello = "Hello".getBytes(StandardCharsets.UTF_8); @@ -444,6 +428,22 @@ public class BoundedInputStreamTest { } } + @SuppressWarnings("deprecation") + @Test + public void testPublicConstructors() throws IOException { + final byte[] helloWorld = "Hello World".getBytes(StandardCharsets.UTF_8); + try (ByteArrayInputStream baos = new ByteArrayInputStream(helloWorld); + BoundedInputStream inputStream = new BoundedInputStream(baos)) { + assertSame(baos, inputStream.unwrap()); + } + final long maxCount = 2; + try (ByteArrayInputStream baos = new ByteArrayInputStream(helloWorld); + BoundedInputStream inputStream = new BoundedInputStream(baos, maxCount)) { + assertSame(baos, inputStream.unwrap()); + assertSame(maxCount, inputStream.getMaxCount()); + } + } + @SuppressWarnings("resource") @Test public void testReadAfterClose() throws Exception { 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 de1cc2297..6f6ed3fdb 100644 --- a/src/test/java/org/apache/commons/io/input/UnsynchronizedBufferedReaderTest.java +++ b/src/test/java/org/apache/commons/io/input/UnsynchronizedBufferedReaderTest.java @@ -233,33 +233,39 @@ public class UnsynchronizedBufferedReaderTest { } /** - * Tests {@link UnsynchronizedBufferedReader#read()}. + * Tests {@link UnsynchronizedBufferedReader#peek()}. * * @throws IOException test failure. */ @Test - public void testRead() throws IOException { + public void testPeek() throws IOException { // Test for method int UnsynchronizedBufferedReader.read() br = new UnsynchronizedBufferedReader(new StringReader(testString)); + final int p = br.peek(); + assertEquals(testString.charAt(0), p); final int r = br.read(); assertEquals(testString.charAt(0), r); br = new UnsynchronizedBufferedReader(new StringReader(new String(new char[] { '\u8765' }))); + assertEquals(br.peek(), '\u8765'); assertEquals(br.read(), '\u8765'); - // + // chars '\0'...'\255' final char[] chars = new char[256]; for (int i = 0; i < 256; i++) { chars[i] = (char) i; } - try (Reader in = new UnsynchronizedBufferedReader(new StringReader(new String(chars)), 12)) { + try (UnsynchronizedBufferedReader in = new UnsynchronizedBufferedReader(new StringReader(new String(chars)), 12)) { + assertEquals(0, in.peek()); // Fill the buffer assertEquals(0, in.read()); // Fill the buffer final char[] buf = new char[14]; in.read(buf, 0, 14); // Read greater than the buffer assertTrue(new String(buf).equals(new String(chars, 1, 14))); + assertEquals(15, in.peek()); // Check next byte assertEquals(15, in.read()); // Check next byte } // // regression test for HARMONY-841 - try (Reader reader = new UnsynchronizedBufferedReader(new CharArrayReader(new char[5], 1, 0), 2)) { + try (UnsynchronizedBufferedReader reader = new UnsynchronizedBufferedReader(new CharArrayReader(new char[5], 1, 0), 2)) { + assertEquals(reader.peek(), -1); assertEquals(reader.read(), -1); } } @@ -270,15 +276,17 @@ public class UnsynchronizedBufferedReaderTest { * @throws IOException test failure. */ @Test - public void testPeek() throws IOException { + public void testPeekArray() throws IOException { // Test for method int UnsynchronizedBufferedReader.read() + final char[] peekBuf1 = new char[1]; br = new UnsynchronizedBufferedReader(new StringReader(testString)); - final int p = br.peek(); - assertEquals(testString.charAt(0), p); + assertEquals(peekBuf1.length, br.peek(peekBuf1)); + assertEquals(testString.charAt(0), peekBuf1[0]); final int r = br.read(); assertEquals(testString.charAt(0), r); br = new UnsynchronizedBufferedReader(new StringReader(new String(new char[] { '\u8765' }))); - assertEquals(br.peek(), '\u8765'); + assertEquals(peekBuf1.length, br.peek(peekBuf1)); + assertEquals(peekBuf1[0], '\u8765'); assertEquals(br.read(), '\u8765'); // chars '\0'...'\255' final char[] chars = new char[256]; @@ -286,12 +294,17 @@ public class UnsynchronizedBufferedReaderTest { chars[i] = (char) i; } try (UnsynchronizedBufferedReader in = new UnsynchronizedBufferedReader(new StringReader(new String(chars)), 12)) { - assertEquals(0, in.peek()); // Fill the buffer + assertEquals(peekBuf1.length, in.peek(peekBuf1)); // Fill the buffer + assertEquals(peekBuf1[0], 0); assertEquals(0, in.read()); // Fill the buffer + final char[] peekBuf14 = new char[14]; + assertEquals(peekBuf14.length, in.peek(peekBuf14)); // Peek greater than the buffer + assertTrue(new String(peekBuf14).equals(new String(chars, 1, 14))); final char[] buf = new char[14]; in.read(buf, 0, 14); // Read greater than the buffer assertTrue(new String(buf).equals(new String(chars, 1, 14))); - assertEquals(15, in.peek()); // Check next byte + assertEquals(peekBuf1.length, in.peek(peekBuf1)); // Check next byte + assertEquals(15, peekBuf1[0]); assertEquals(15, in.read()); // Check next byte } // @@ -303,46 +316,33 @@ public class UnsynchronizedBufferedReaderTest { } /** - * Tests {@link UnsynchronizedBufferedReader#peek()}. + * Tests {@link UnsynchronizedBufferedReader#read()}. * * @throws IOException test failure. */ @Test - public void testPeekArray() throws IOException { + public void testRead() throws IOException { // Test for method int UnsynchronizedBufferedReader.read() - final char[] peekBuf1 = new char[1]; br = new UnsynchronizedBufferedReader(new StringReader(testString)); - assertEquals(peekBuf1.length, br.peek(peekBuf1)); - assertEquals(testString.charAt(0), peekBuf1[0]); final int r = br.read(); assertEquals(testString.charAt(0), r); br = new UnsynchronizedBufferedReader(new StringReader(new String(new char[] { '\u8765' }))); - assertEquals(peekBuf1.length, br.peek(peekBuf1)); - assertEquals(peekBuf1[0], '\u8765'); assertEquals(br.read(), '\u8765'); - // chars '\0'...'\255' + // final char[] chars = new char[256]; for (int i = 0; i < 256; i++) { chars[i] = (char) i; } - try (UnsynchronizedBufferedReader in = new UnsynchronizedBufferedReader(new StringReader(new String(chars)), 12)) { - assertEquals(peekBuf1.length, in.peek(peekBuf1)); // Fill the buffer - assertEquals(peekBuf1[0], 0); + try (Reader in = new UnsynchronizedBufferedReader(new StringReader(new String(chars)), 12)) { assertEquals(0, in.read()); // Fill the buffer - final char[] peekBuf14 = new char[14]; - assertEquals(peekBuf14.length, in.peek(peekBuf14)); // Peek greater than the buffer - assertTrue(new String(peekBuf14).equals(new String(chars, 1, 14))); final char[] buf = new char[14]; in.read(buf, 0, 14); // Read greater than the buffer assertTrue(new String(buf).equals(new String(chars, 1, 14))); - assertEquals(peekBuf1.length, in.peek(peekBuf1)); // Check next byte - assertEquals(15, peekBuf1[0]); assertEquals(15, in.read()); // Check next byte } // // regression test for HARMONY-841 - try (UnsynchronizedBufferedReader reader = new UnsynchronizedBufferedReader(new CharArrayReader(new char[5], 1, 0), 2)) { - assertEquals(reader.peek(), -1); + try (Reader reader = new UnsynchronizedBufferedReader(new CharArrayReader(new char[5], 1, 0), 2)) { assertEquals(reader.read(), -1); } } diff --git a/src/test/java/org/apache/commons/io/output/RandomAccessFileOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/RandomAccessFileOutputStreamTest.java index 0a1dadc2f..5984a0d83 100644 --- a/src/test/java/org/apache/commons/io/output/RandomAccessFileOutputStreamTest.java +++ b/src/test/java/org/apache/commons/io/output/RandomAccessFileOutputStreamTest.java @@ -127,6 +127,15 @@ public class RandomAccessFileOutputStreamTest { // @formatter:on } + @Test + public void testWriteGetDefault() throws IOException { + assertThrows(IllegalStateException.class, () -> { + try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().get()) { + // doNothing + } + }); + } + /** * Tests that the default OpenOption is WRITE. * @@ -144,15 +153,6 @@ public class RandomAccessFileOutputStreamTest { // @formatter:on } - @Test - public void testWriteGetDefault() throws IOException { - assertThrows(IllegalStateException.class, () -> { - try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().get()) { - // doNothing - } - }); - } - @Test public void testWriteInt() throws IOException { final Path fixturePath = temporaryFolder.resolve("testWriteInt.txt");