Skip to content

Commit

Permalink
Add RandomAccessFileOutputStream.getRandomAccessFile()
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Jan 1, 2025
1 parent 222fd91 commit e798377
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">ReversedLinesFileReader implements IOIterable&lt;String&gt;.</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add AbstractByteArrayOutputStream.write(CharSequence, Charset).</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add AbstractByteArrayOutputStream.write(byte[]).</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add RandomAccessFileOutputStream.getRandomAccessFile().</action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Dependabot, Gary Gregory">Bump commons.bytebuddy.version from 1.15.10 to 1.15.11 #710.</action>
</release>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -122,7 +128,7 @@ public void testWriteGet() throws IOException {
.setPath(fixturePath)
.setOpenOptions(StandardOpenOption.WRITE)
.get()) {
// doNothing
validateState(os);
}
// @formatter:on
}
Expand All @@ -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);
}
});
}
Expand All @@ -148,7 +154,7 @@ public void testWriteGetPathOnly() throws IOException {
try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder()
.setPath(fixturePath)
.get()) {
// doNothing
validateState(os);
}
// @formatter:on
}
Expand All @@ -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);
Expand All @@ -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());
}

}

0 comments on commit e798377

Please sign in to comment.