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 5cabfc7e8ebec323f3d565555aec04238b32544c Author: Gary Gregory <[email protected]> AuthorDate: Tue Jul 1 10:47:04 2025 -0400 Sort members --- .../commons/io/input/CharSequenceInputStream.java | 38 ++--- .../java/org/apache/commons/io/FileUtilsTest.java | 96 ++++++------ .../commons/io/input/QueueInputStreamTest.java | 166 ++++++++++----------- .../commons/io/jmh/QueueStreamBenchmark.java | 22 +-- 4 files changed, 161 insertions(+), 161 deletions(-) diff --git a/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java b/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java index 4a9bca41d..866610549 100644 --- a/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java +++ b/src/main/java/org/apache/commons/io/input/CharSequenceInputStream.java @@ -167,6 +167,25 @@ private static CharsetEncoder newEncoder(final Charset charset) { private int cBufMark; // position in cBuf private final CharsetEncoder charsetEncoder; + private CharSequenceInputStream(final Builder builder) { + this.charsetEncoder = builder.charsetEncoder; + // Ensure that buffer is long enough to hold a complete character + this.bBuf = ByteBuffer.allocate(ReaderInputStream.checkMinBufferSize(builder.charsetEncoder, builder.getBufferSize())); + this.bBuf.flip(); + this.cBuf = CharBuffer.wrap(Uncheck.get(() -> builder.getCharSequence())); + this.cBufMark = NO_MARK; + this.bBufMark = NO_MARK; + try { + fillBuffer(); + } catch (final CharacterCodingException ex) { + // Reset everything without filling the buffer + // so the same exception can be thrown again later. + this.bBuf.clear(); + this.bBuf.flip(); + this.cBuf.rewind(); + } + } + /** * Constructs a new instance with a buffer size of {@link IOUtils#DEFAULT_BUFFER_SIZE}. * @@ -194,25 +213,6 @@ public CharSequenceInputStream(final CharSequence cs, final Charset charset, fin this(builder().setCharSequence(cs).setCharset(charset).setBufferSize(bufferSize)); } - private CharSequenceInputStream(final Builder builder) { - this.charsetEncoder = builder.charsetEncoder; - // Ensure that buffer is long enough to hold a complete character - this.bBuf = ByteBuffer.allocate(ReaderInputStream.checkMinBufferSize(builder.charsetEncoder, builder.getBufferSize())); - this.bBuf.flip(); - this.cBuf = CharBuffer.wrap(Uncheck.get(() -> builder.getCharSequence())); - this.cBufMark = NO_MARK; - this.bBufMark = NO_MARK; - try { - fillBuffer(); - } catch (final CharacterCodingException ex) { - // Reset everything without filling the buffer - // so the same exception can be thrown again later. - this.bBuf.clear(); - this.bBuf.flip(); - this.cBuf.rewind(); - } - } - /** * Constructs a new instance with a buffer size of {@link IOUtils#DEFAULT_BUFFER_SIZE}. * diff --git a/src/test/java/org/apache/commons/io/FileUtilsTest.java b/src/test/java/org/apache/commons/io/FileUtilsTest.java index 4c6413756..ece78bcd8 100644 --- a/src/test/java/org/apache/commons/io/FileUtilsTest.java +++ b/src/test/java/org/apache/commons/io/FileUtilsTest.java @@ -1617,6 +1617,23 @@ void testForceDeleteAFileDoesNotExist() { } + @Test + public void testForceDeleteBrokenSymlink() throws Exception { + final ImmutablePair<Path, Path> pair = createTempSymbolicLinkedRelativeDir(); + final Path symlinkedDir = pair.getLeft(); + final Path targetDir = pair.getRight(); + + Files.delete(targetDir); + assertFalse(Files.exists(symlinkedDir)); + assertTrue(Files.isSymbolicLink(symlinkedDir)); + + FileUtils.forceDelete(symlinkedDir.toFile()); + + // check targeted symlink is gone + assertFalse(Files.exists(symlinkedDir)); + assertFalse(Files.isSymbolicLink(symlinkedDir)); + } + @Test void testForceDeleteDir() throws Exception { final File testDirectory = tempDirFile; @@ -1690,6 +1707,25 @@ void testForceDeleteReadOnlyFile() throws Exception { } } + @Test + public void testForceDeleteSymlink() throws Exception { + final ImmutablePair<Path, Path> pair = createTempSymbolicLinkedRelativeDir(); + final Path symlinkedDir = pair.getLeft(); + final Path targetDir = pair.getRight(); + + assertTrue(Files.exists(symlinkedDir)); + assertTrue(Files.isSymbolicLink(symlinkedDir)); + assertTrue(Files.exists(targetDir)); + + FileUtils.forceDelete(symlinkedDir.toFile()); + + // check targeted symlink is gone + assertFalse(Files.exists(symlinkedDir)); + assertFalse(Files.isSymbolicLink(symlinkedDir)); + // dir targeted by symlink is not deleted + assertTrue(Files.exists(targetDir)); + } + /** * TODO Passes on macOS, fails on Linux and Windows with AccessDeniedException. */ @@ -1761,42 +1797,6 @@ void testForceDeleteUnwritableFile() throws Exception { } } - @Test - public void testForceDeleteBrokenSymlink() throws Exception { - final ImmutablePair<Path, Path> pair = createTempSymbolicLinkedRelativeDir(); - final Path symlinkedDir = pair.getLeft(); - final Path targetDir = pair.getRight(); - - Files.delete(targetDir); - assertFalse(Files.exists(symlinkedDir)); - assertTrue(Files.isSymbolicLink(symlinkedDir)); - - FileUtils.forceDelete(symlinkedDir.toFile()); - - // check targeted symlink is gone - assertFalse(Files.exists(symlinkedDir)); - assertFalse(Files.isSymbolicLink(symlinkedDir)); - } - - @Test - public void testForceDeleteSymlink() throws Exception { - final ImmutablePair<Path, Path> pair = createTempSymbolicLinkedRelativeDir(); - final Path symlinkedDir = pair.getLeft(); - final Path targetDir = pair.getRight(); - - assertTrue(Files.exists(symlinkedDir)); - assertTrue(Files.isSymbolicLink(symlinkedDir)); - assertTrue(Files.exists(targetDir)); - - FileUtils.forceDelete(symlinkedDir.toFile()); - - // check targeted symlink is gone - assertFalse(Files.exists(symlinkedDir)); - assertFalse(Files.isSymbolicLink(symlinkedDir)); - // dir targeted by symlink is not deleted - assertTrue(Files.exists(targetDir)); - } - @Test void testForceMkdir() throws Exception { // Tests with existing directory @@ -2826,12 +2826,12 @@ void testReadFileToStringWithEncoding() throws Exception { } @Test - void testReadLinesUTF8() throws Exception { - final File file = TestUtils.newFile(tempDirFile, "lines.txt"); - final String[] data = { "hello", "\u1234", "", "this is", "some text" }; - TestUtils.createLineFileUtf8(file, data); - final List<String> lines = FileUtils.readLines(file, UTF_8); - assertEquals(Arrays.asList(data), lines); + @EnabledIf("isPosixFilePermissionsSupported") + void testReadLines_IOExceptionOnPosixFileSystem() throws Exception { + final File file = TestUtils.newFile(tempDirFile, "cant-read.txt"); + TestUtils.createFile(file, 100); + Files.setPosixFilePermissions(file.toPath(), PosixFilePermissions.fromString("---------")); + assertThrows(IOException.class, () -> FileUtils.readLines(file)); } @Test @@ -2856,12 +2856,12 @@ void testReadLinesErrors() { } @Test - @EnabledIf("isPosixFilePermissionsSupported") - void testReadLines_IOExceptionOnPosixFileSystem() throws Exception { - final File file = TestUtils.newFile(tempDirFile, "cant-read.txt"); - TestUtils.createFile(file, 100); - Files.setPosixFilePermissions(file.toPath(), PosixFilePermissions.fromString("---------")); - assertThrows(IOException.class, () -> FileUtils.readLines(file)); + void testReadLinesUTF8() throws Exception { + final File file = TestUtils.newFile(tempDirFile, "lines.txt"); + final String[] data = { "hello", "\u1234", "", "this is", "some text" }; + TestUtils.createLineFileUtf8(file, data); + final List<String> lines = FileUtils.readLines(file, UTF_8); + assertEquals(Arrays.asList(data), lines); } @Test diff --git a/src/test/java/org/apache/commons/io/input/QueueInputStreamTest.java b/src/test/java/org/apache/commons/io/input/QueueInputStreamTest.java index c73e2a9dc..5ea695095 100644 --- a/src/test/java/org/apache/commons/io/input/QueueInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/QueueInputStreamTest.java @@ -84,10 +84,45 @@ public static Stream<Arguments> inputData() { // @formatter:on } + @TestFactory + public DynamicTest[] bulkReadErrorHandlingTests() { + final QueueInputStream queueInputStream = new QueueInputStream(); + return new DynamicTest[] { + dynamicTest("Offset too big", () -> + assertThrows(IndexOutOfBoundsException.class, () -> + queueInputStream.read(EMPTY_BYTE_ARRAY, 1, 0))), + + dynamicTest("Offset negative", () -> + assertThrows(IndexOutOfBoundsException.class, () -> + queueInputStream.read(EMPTY_BYTE_ARRAY, -1, 0))), + + dynamicTest("Length too big", () -> + assertThrows(IndexOutOfBoundsException.class, () -> + queueInputStream.read(EMPTY_BYTE_ARRAY, 0, 1))), + + dynamicTest("Length negative", () -> + assertThrows(IndexOutOfBoundsException.class, () -> + queueInputStream.read(EMPTY_BYTE_ARRAY, 0, -1))), + }; + } + private int defaultBufferSize() { return 8192; } + private void doTestReadLineByLine(final String inputData, final InputStream inputStream, final OutputStream outputStream) throws IOException { + final String[] lines = inputData.split("\n"); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, UTF_8))) { + for (final String line : lines) { + outputStream.write(line.getBytes(UTF_8)); + outputStream.write('\n'); + + final String actualLine = reader.readLine(); + assertEquals(line, actualLine); + } + } + } + private String readUnbuffered(final InputStream inputStream) throws IOException { return readUnbuffered(inputStream, Integer.MAX_VALUE); } @@ -146,74 +181,30 @@ void testBufferedReads(final String inputData) throws IOException { @ParameterizedTest(name = "inputData={0}") @MethodSource("inputData") - void testReadLineByLineQueue(final String inputData) throws IOException { - final String[] lines = inputData.split("\n"); + void testBufferedReadWrite(final String inputData) throws IOException { final BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(); - try (QueueInputStream inputStream = QueueInputStream.builder() - .setBlockingQueue(queue) - .setTimeout(Duration.ofHours(1)) - .get(); - QueueOutputStream outputStream = inputStream.newQueueOutputStream()) { - - doTestReadLineByLine(inputData, inputStream, outputStream); + try (BufferedInputStream inputStream = new BufferedInputStream(new QueueInputStream(queue)); + BufferedOutputStream outputStream = new BufferedOutputStream(new QueueOutputStream(queue), defaultBufferSize())) { + outputStream.write(inputData.getBytes(StandardCharsets.UTF_8)); + outputStream.flush(); + final String dataCopy = IOUtils.toString(inputStream, StandardCharsets.UTF_8); + assertEquals(inputData, dataCopy); } } @ParameterizedTest(name = "inputData={0}") @MethodSource("inputData") - void testReadLineByLineFile(final String inputData) throws IOException { - final Path tempFile = Files.createTempFile(getClass().getSimpleName(), ".txt"); - try (InputStream inputStream = Files.newInputStream(tempFile); - OutputStream outputStream = Files.newOutputStream(tempFile)) { - - doTestReadLineByLine(inputData, inputStream, outputStream); - } finally { - Files.delete(tempFile); - } - } - - private void doTestReadLineByLine(final String inputData, final InputStream inputStream, final OutputStream outputStream) throws IOException { - final String[] lines = inputData.split("\n"); - try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, UTF_8))) { - for (final String line : lines) { - outputStream.write(line.getBytes(UTF_8)); - outputStream.write('\n'); - - final String actualLine = reader.readLine(); - assertEquals(line, actualLine); - } + void testBufferedWrites(final String inputData) throws IOException { + final BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(); + try (QueueInputStream inputStream = new QueueInputStream(queue); + BufferedOutputStream outputStream = new BufferedOutputStream(new QueueOutputStream(queue), defaultBufferSize())) { + outputStream.write(inputData.getBytes(StandardCharsets.UTF_8)); + outputStream.flush(); + final String actualData = readUnbuffered(inputStream); + assertEquals(inputData, actualData); } } - @TestFactory - public DynamicTest[] bulkReadErrorHandlingTests() { - final QueueInputStream queueInputStream = new QueueInputStream(); - return new DynamicTest[] { - dynamicTest("Offset too big", () -> - assertThrows(IndexOutOfBoundsException.class, () -> - queueInputStream.read(EMPTY_BYTE_ARRAY, 1, 0))), - - dynamicTest("Offset negative", () -> - assertThrows(IndexOutOfBoundsException.class, () -> - queueInputStream.read(EMPTY_BYTE_ARRAY, -1, 0))), - - dynamicTest("Length too big", () -> - assertThrows(IndexOutOfBoundsException.class, () -> - queueInputStream.read(EMPTY_BYTE_ARRAY, 0, 1))), - - dynamicTest("Length negative", () -> - assertThrows(IndexOutOfBoundsException.class, () -> - queueInputStream.read(EMPTY_BYTE_ARRAY, 0, -1))), - }; - } - - @Test - void testBulkReadZeroLength() { - final QueueInputStream queueInputStream = new QueueInputStream(); - final int read = queueInputStream.read(EMPTY_BYTE_ARRAY, 0, 0); - assertEquals(0, read); - } - @ParameterizedTest(name = "inputData={0}") @MethodSource("inputData") void testBulkReadWaiting(final String inputData) throws IOException { @@ -256,30 +247,11 @@ public Integer poll(final long timeout, final TimeUnit unit) throws InterruptedE } } - @ParameterizedTest(name = "inputData={0}") - @MethodSource("inputData") - void testBufferedReadWrite(final String inputData) throws IOException { - final BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(); - try (BufferedInputStream inputStream = new BufferedInputStream(new QueueInputStream(queue)); - BufferedOutputStream outputStream = new BufferedOutputStream(new QueueOutputStream(queue), defaultBufferSize())) { - outputStream.write(inputData.getBytes(StandardCharsets.UTF_8)); - outputStream.flush(); - final String dataCopy = IOUtils.toString(inputStream, StandardCharsets.UTF_8); - assertEquals(inputData, dataCopy); - } - } - - @ParameterizedTest(name = "inputData={0}") - @MethodSource("inputData") - void testBufferedWrites(final String inputData) throws IOException { - final BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(); - try (QueueInputStream inputStream = new QueueInputStream(queue); - BufferedOutputStream outputStream = new BufferedOutputStream(new QueueOutputStream(queue), defaultBufferSize())) { - outputStream.write(inputData.getBytes(StandardCharsets.UTF_8)); - outputStream.flush(); - final String actualData = readUnbuffered(inputStream); - assertEquals(inputData, actualData); - } + @Test + void testBulkReadZeroLength() { + final QueueInputStream queueInputStream = new QueueInputStream(); + final int read = queueInputStream.read(EMPTY_BYTE_ARRAY, 0, 0); + assertEquals(0, read); } @Test @@ -299,6 +271,34 @@ void testReadAfterClose(final String inputData) throws IOException { assertEquals(IOUtils.EOF, shadow.read()); } + @ParameterizedTest(name = "inputData={0}") + @MethodSource("inputData") + void testReadLineByLineFile(final String inputData) throws IOException { + final Path tempFile = Files.createTempFile(getClass().getSimpleName(), ".txt"); + try (InputStream inputStream = Files.newInputStream(tempFile); + OutputStream outputStream = Files.newOutputStream(tempFile)) { + + doTestReadLineByLine(inputData, inputStream, outputStream); + } finally { + Files.delete(tempFile); + } + } + + @ParameterizedTest(name = "inputData={0}") + @MethodSource("inputData") + void testReadLineByLineQueue(final String inputData) throws IOException { + final String[] lines = inputData.split("\n"); + final BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(); + try (QueueInputStream inputStream = QueueInputStream.builder() + .setBlockingQueue(queue) + .setTimeout(Duration.ofHours(1)) + .get(); + QueueOutputStream outputStream = inputStream.newQueueOutputStream()) { + + doTestReadLineByLine(inputData, inputStream, outputStream); + } + } + @Test void testResetArguments() throws IOException { try (QueueInputStream queueInputStream = QueueInputStream.builder().setTimeout(null).get()) { diff --git a/src/test/java/org/apache/commons/io/jmh/QueueStreamBenchmark.java b/src/test/java/org/apache/commons/io/jmh/QueueStreamBenchmark.java index a42437f74..a64841f85 100644 --- a/src/test/java/org/apache/commons/io/jmh/QueueStreamBenchmark.java +++ b/src/test/java/org/apache/commons/io/jmh/QueueStreamBenchmark.java @@ -52,17 +52,6 @@ public class QueueStreamBenchmark { private final byte[] input = RandomUtils.insecure().randomBytes(CAPACITY); private final byte[] output = new byte[BUFFER_SIZE]; - @Benchmark - @Group("streams") - public void output() throws Exception { - int sent = 0; - while (sent < CAPACITY) { - final int len = Math.min(CAPACITY - sent, BUFFER_SIZE); - outputStream.write(input, sent, len); - sent += len; - } - } - @Benchmark @Group("streams") public void input(final Blackhole bh) throws Exception { @@ -73,4 +62,15 @@ public void input(final Blackhole bh) throws Exception { received += len; } } + + @Benchmark + @Group("streams") + public void output() throws Exception { + int sent = 0; + while (sent < CAPACITY) { + final int len = Math.min(CAPACITY - sent, BUFFER_SIZE); + outputStream.write(input, sent, len); + sent += len; + } + } }
