Skip to content

Commit 7ee0f0c

Browse files
committed
Add RandomAccessFileMode.io(String)
1 parent 3af23a8 commit 7ee0f0c

2 files changed

Lines changed: 43 additions & 13 deletions

File tree

src/main/java/org/apache/commons/io/RandomAccessFileMode.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,18 @@ public static RandomAccessFileMode valueOfMode(final String mode) {
151151
*
152152
* @param file the file object
153153
* @return a random access file
154-
* @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}.
154+
* @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
155155
*/
156156
public RandomAccessFile create(final File file) throws FileNotFoundException {
157-
return new RandomAccessFileAccessor(file, mode);
157+
return new IORandomAccessFile(file, mode);
158158
}
159159

160160
/**
161161
* Constructs a random access file to read from, and optionally to write to, the file specified by the {@link File} argument.
162162
*
163163
* @param file the file object
164164
* @return a random access file
165-
* @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}.
165+
* @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
166166
*/
167167
public RandomAccessFile create(final Path file) throws FileNotFoundException {
168168
return create(Objects.requireNonNull(file.toFile(), "file"));
@@ -171,12 +171,12 @@ public RandomAccessFile create(final Path file) throws FileNotFoundException {
171171
/**
172172
* Constructs a random access file to read from, and optionally to write to, the file specified by the {@link File} argument.
173173
*
174-
* @param file the file object
174+
* @param name the file object
175175
* @return a random access file
176-
* @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}.
176+
* @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
177177
*/
178-
public RandomAccessFile create(final String file) throws FileNotFoundException {
179-
return new RandomAccessFileAccessor(file, mode);
178+
public RandomAccessFile create(final String name) throws FileNotFoundException {
179+
return new IORandomAccessFile(name, mode);
180180
}
181181

182182
/**
@@ -230,4 +230,16 @@ public boolean implies(final RandomAccessFileMode other) {
230230
return getLevel() >= other.getLevel();
231231
}
232232

233+
/**
234+
* Constructs a random access file to read from, and optionally to write to, the file specified by the {@link File} argument.
235+
*
236+
* @param name the file object
237+
* @return a random access file
238+
* @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
239+
* @since 2.18.0
240+
*/
241+
public IORandomAccessFile io(final String name) throws FileNotFoundException {
242+
return new IORandomAccessFile(name, mode);
243+
}
244+
233245
}

src/test/java/org/apache/commons/io/RandomAccessFileModeTest.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
*/
4141
public class RandomAccessFileModeTest {
4242

43+
private static final byte[] BYTES_FIXTURE = "Foo".getBytes(StandardCharsets.US_ASCII);
44+
45+
private static final String FIXTURE = "test.txt";
46+
4347
/**
4448
* Temporary directory.
4549
*/
@@ -53,8 +57,8 @@ private byte[] read(final RandomAccessFile randomAccessFile) throws IOException
5357
@ParameterizedTest
5458
@EnumSource(RandomAccessFileMode.class)
5559
public void testCreateFile(final RandomAccessFileMode randomAccessFileMode) throws IOException {
56-
final byte[] expected = "Foo".getBytes(StandardCharsets.US_ASCII);
57-
final Path fixture = Files.write(tempDir.resolve("test.txt"), expected);
60+
final byte[] expected = BYTES_FIXTURE;
61+
final Path fixture = writeFixture(expected);
5862
try (RandomAccessFile randomAccessFile = randomAccessFileMode.create(fixture.toFile())) {
5963
assertArrayEquals(expected, read(randomAccessFile));
6064
}
@@ -63,8 +67,8 @@ public void testCreateFile(final RandomAccessFileMode randomAccessFileMode) thro
6367
@ParameterizedTest
6468
@EnumSource(RandomAccessFileMode.class)
6569
public void testCreatePath(final RandomAccessFileMode randomAccessFileMode) throws IOException {
66-
final byte[] expected = "Foo".getBytes(StandardCharsets.US_ASCII);
67-
final Path fixture = Files.write(tempDir.resolve("test.txt"), expected);
70+
final byte[] expected = BYTES_FIXTURE;
71+
final Path fixture = writeFixture(expected);
6872
try (RandomAccessFile randomAccessFile = randomAccessFileMode.create(fixture)) {
6973
assertArrayEquals(expected, read(randomAccessFile));
7074
}
@@ -73,8 +77,8 @@ public void testCreatePath(final RandomAccessFileMode randomAccessFileMode) thro
7377
@ParameterizedTest
7478
@EnumSource(RandomAccessFileMode.class)
7579
public void testCreateString(final RandomAccessFileMode randomAccessFileMode) throws IOException {
76-
final byte[] expected = "Foo".getBytes(StandardCharsets.US_ASCII);
77-
final Path fixture = Files.write(tempDir.resolve("test.txt"), expected);
80+
final byte[] expected = BYTES_FIXTURE;
81+
final Path fixture = writeFixture(expected);
7882
try (RandomAccessFile randomAccessFile = randomAccessFileMode.create(fixture.toString())) {
7983
assertArrayEquals(expected, read(randomAccessFile));
8084
}
@@ -96,6 +100,16 @@ public void testImplies() {
96100
assertFalse(RandomAccessFileMode.READ_ONLY.implies(RandomAccessFileMode.READ_WRITE_SYNC_ALL));
97101
}
98102

103+
@ParameterizedTest
104+
@EnumSource(RandomAccessFileMode.class)
105+
public void testIoString(final RandomAccessFileMode randomAccessFileMode) throws IOException {
106+
final byte[] expected = BYTES_FIXTURE;
107+
final Path fixture = writeFixture(expected);
108+
try (IORandomAccessFile randomAccessFile = randomAccessFileMode.io(fixture.toString())) {
109+
assertArrayEquals(expected, read(randomAccessFile));
110+
}
111+
}
112+
99113
/**
100114
* Tests the standard {@link Enum#toString()} behavior.
101115
*/
@@ -145,4 +159,8 @@ public void testValueOfOpenOptions() {
145159
assertEquals(RandomAccessFileMode.READ_WRITE_SYNC_ALL,
146160
RandomAccessFileMode.valueOf(StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.SYNC));
147161
}
162+
163+
private Path writeFixture(final byte[] bytes) throws IOException {
164+
return Files.write(tempDir.resolve(FIXTURE), bytes, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
165+
}
148166
}

0 commit comments

Comments
 (0)