diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 526f8f58702..621dc27b2b0 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -57,6 +57,7 @@ The type attribute can be add,update,fix,remove. ReversedLinesFileReader implements IOIterable<String>. Add AbstractByteArrayOutputStream.write(CharSequence, Charset). Add AbstractByteArrayOutputStream.write(byte[]). + Add RandomAccessFileOutputStream.getRandomAccessFile(). Bump commons.bytebuddy.version from 1.15.10 to 1.15.11 #710. 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 0edaba63e45..ca2177b6356 100644 --- a/src/main/java/org/apache/commons/io/output/RandomAccessFileOutputStream.java +++ b/src/main/java/org/apache/commons/io/output/RandomAccessFileOutputStream.java @@ -96,6 +96,16 @@ public void flush() throws IOException { super.flush(); } + /** + * Gets the underlying random access file. + * + * @return the underlying random access file. + * @since 2.19.0 + */ + public RandomAccessFile getRandomAccessFile() { + return randomAccessFile; + } + @Override public void write(final int b) throws IOException { randomAccessFile.write(b); 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 5984a0d8362..76b6b23f6b4 100644 --- a/src/test/java/org/apache/commons/io/output/RandomAccessFileOutputStreamTest.java +++ b/src/test/java/org/apache/commons/io/output/RandomAccessFileOutputStreamTest.java @@ -18,9 +18,11 @@ package org.apache.commons.io.output; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.IOException; +import java.io.RandomAccessFile; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -56,6 +58,7 @@ public void testClose() throws IOException { assertEquals(EXPECTED, new String(Files.readAllBytes(fixturePath), charset)); // @formatter:on try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) { + validateState(os); } } @@ -77,6 +80,7 @@ public void testFlush() throws IOException { assertEquals(EXPECTED, new String(Files.readAllBytes(fixturePath), charset)); // @formatter:on try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) { + validateState(os); } } @@ -94,6 +98,7 @@ public void testWriteByteArray() throws IOException { assertEquals(EXPECTED, new String(Files.readAllBytes(fixturePath), charset)); // @formatter:on try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) { + validateState(os); } } @@ -111,6 +116,7 @@ public void testWriteByteArrayAt() throws IOException { assertEquals(EXPECTED.subSequence(1, EXPECTED.length() - 1), new String(Files.readAllBytes(fixturePath), charset)); // @formatter:on try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) { + validateState(os); } } @@ -122,7 +128,7 @@ public void testWriteGet() throws IOException { .setPath(fixturePath) .setOpenOptions(StandardOpenOption.WRITE) .get()) { - // doNothing + validateState(os); } // @formatter:on } @@ -131,7 +137,7 @@ public void testWriteGet() throws IOException { public void testWriteGetDefault() throws IOException { assertThrows(IllegalStateException.class, () -> { try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().get()) { - // doNothing + validateState(os); } }); } @@ -148,7 +154,7 @@ public void testWriteGetPathOnly() throws IOException { try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder() .setPath(fixturePath) .get()) { - // doNothing + validateState(os); } // @formatter:on } @@ -162,6 +168,7 @@ public void testWriteInt() throws IOException { .setPath(fixturePath) .setOpenOptions(StandardOpenOption.WRITE) .get()) { + validateState(os); final byte[] bytes = EXPECTED.getBytes(charset); for (final byte element : bytes) { os.write(element); @@ -170,7 +177,15 @@ public void testWriteInt() throws IOException { assertEquals(EXPECTED, new String(Files.readAllBytes(fixturePath), charset)); // @formatter:on try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) { + validateState(os); } } + @SuppressWarnings("resource") + private void validateState(final RandomAccessFileOutputStream os) throws IOException { + final RandomAccessFile randomAccessFile = os.getRandomAccessFile(); + assertNotNull(randomAccessFile); + assertNotNull(randomAccessFile.getFD()); + } + }