Skip to content

Commit e798377

Browse files
committed
Add RandomAccessFileOutputStream.getRandomAccessFile()
1 parent 222fd91 commit e798377

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ The <action> type attribute can be add,update,fix,remove.
5757
<action dev="ggregory" type="add" due-to="Gary Gregory">ReversedLinesFileReader implements IOIterable&lt;String&gt;.</action>
5858
<action dev="ggregory" type="add" due-to="Gary Gregory">Add AbstractByteArrayOutputStream.write(CharSequence, Charset).</action>
5959
<action dev="ggregory" type="add" due-to="Gary Gregory">Add AbstractByteArrayOutputStream.write(byte[]).</action>
60+
<action dev="ggregory" type="add" due-to="Gary Gregory">Add RandomAccessFileOutputStream.getRandomAccessFile().</action>
6061
<!-- UPDATE -->
6162
<action dev="ggregory" type="update" due-to="Dependabot, Gary Gregory">Bump commons.bytebuddy.version from 1.15.10 to 1.15.11 #710.</action>
6263
</release>

src/main/java/org/apache/commons/io/output/RandomAccessFileOutputStream.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ public void flush() throws IOException {
9696
super.flush();
9797
}
9898

99+
/**
100+
* Gets the underlying random access file.
101+
*
102+
* @return the underlying random access file.
103+
* @since 2.19.0
104+
*/
105+
public RandomAccessFile getRandomAccessFile() {
106+
return randomAccessFile;
107+
}
108+
99109
@Override
100110
public void write(final int b) throws IOException {
101111
randomAccessFile.write(b);

src/test/java/org/apache/commons/io/output/RandomAccessFileOutputStreamTest.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
package org.apache.commons.io.output;
1919

2020
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2122
import static org.junit.jupiter.api.Assertions.assertThrows;
2223

2324
import java.io.IOException;
25+
import java.io.RandomAccessFile;
2426
import java.nio.charset.Charset;
2527
import java.nio.charset.StandardCharsets;
2628
import java.nio.file.Files;
@@ -56,6 +58,7 @@ public void testClose() throws IOException {
5658
assertEquals(EXPECTED, new String(Files.readAllBytes(fixturePath), charset));
5759
// @formatter:on
5860
try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) {
61+
validateState(os);
5962
}
6063
}
6164

@@ -77,6 +80,7 @@ public void testFlush() throws IOException {
7780
assertEquals(EXPECTED, new String(Files.readAllBytes(fixturePath), charset));
7881
// @formatter:on
7982
try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) {
83+
validateState(os);
8084
}
8185
}
8286

@@ -94,6 +98,7 @@ public void testWriteByteArray() throws IOException {
9498
assertEquals(EXPECTED, new String(Files.readAllBytes(fixturePath), charset));
9599
// @formatter:on
96100
try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) {
101+
validateState(os);
97102
}
98103
}
99104

@@ -111,6 +116,7 @@ public void testWriteByteArrayAt() throws IOException {
111116
assertEquals(EXPECTED.subSequence(1, EXPECTED.length() - 1), new String(Files.readAllBytes(fixturePath), charset));
112117
// @formatter:on
113118
try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) {
119+
validateState(os);
114120
}
115121
}
116122

@@ -122,7 +128,7 @@ public void testWriteGet() throws IOException {
122128
.setPath(fixturePath)
123129
.setOpenOptions(StandardOpenOption.WRITE)
124130
.get()) {
125-
// doNothing
131+
validateState(os);
126132
}
127133
// @formatter:on
128134
}
@@ -131,7 +137,7 @@ public void testWriteGet() throws IOException {
131137
public void testWriteGetDefault() throws IOException {
132138
assertThrows(IllegalStateException.class, () -> {
133139
try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().get()) {
134-
// doNothing
140+
validateState(os);
135141
}
136142
});
137143
}
@@ -148,7 +154,7 @@ public void testWriteGetPathOnly() throws IOException {
148154
try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder()
149155
.setPath(fixturePath)
150156
.get()) {
151-
// doNothing
157+
validateState(os);
152158
}
153159
// @formatter:on
154160
}
@@ -162,6 +168,7 @@ public void testWriteInt() throws IOException {
162168
.setPath(fixturePath)
163169
.setOpenOptions(StandardOpenOption.WRITE)
164170
.get()) {
171+
validateState(os);
165172
final byte[] bytes = EXPECTED.getBytes(charset);
166173
for (final byte element : bytes) {
167174
os.write(element);
@@ -170,7 +177,15 @@ public void testWriteInt() throws IOException {
170177
assertEquals(EXPECTED, new String(Files.readAllBytes(fixturePath), charset));
171178
// @formatter:on
172179
try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) {
180+
validateState(os);
173181
}
174182
}
175183

184+
@SuppressWarnings("resource")
185+
private void validateState(final RandomAccessFileOutputStream os) throws IOException {
186+
final RandomAccessFile randomAccessFile = os.getRandomAccessFile();
187+
assertNotNull(randomAccessFile);
188+
assertNotNull(randomAccessFile.getFD());
189+
}
190+
176191
}

0 commit comments

Comments
 (0)