You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/10/12 14:20:56 UTC
(commons-io) branch master updated: Add RandomAccessFileMode.io(String)
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
The following commit(s) were added to refs/heads/master by this push:
new 7ee0f0cdb Add RandomAccessFileMode.io(String)
7ee0f0cdb is described below
commit 7ee0f0cdb079ca000b7e612d65d2087e47efe1ce
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Oct 12 10:20:51 2024 -0400
Add RandomAccessFileMode.io(String)
---
.../apache/commons/io/RandomAccessFileMode.java | 26 ++++++++++++++-----
.../commons/io/RandomAccessFileModeTest.java | 30 +++++++++++++++++-----
2 files changed, 43 insertions(+), 13 deletions(-)
diff --git a/src/main/java/org/apache/commons/io/RandomAccessFileMode.java b/src/main/java/org/apache/commons/io/RandomAccessFileMode.java
index 458a203e4..88c4859fc 100644
--- a/src/main/java/org/apache/commons/io/RandomAccessFileMode.java
+++ b/src/main/java/org/apache/commons/io/RandomAccessFileMode.java
@@ -151,10 +151,10 @@ public enum RandomAccessFileMode {
*
* @param file the file object
* @return a random access file
- * @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}.
+ * @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
*/
public RandomAccessFile create(final File file) throws FileNotFoundException {
- return new RandomAccessFileAccessor(file, mode);
+ return new IORandomAccessFile(file, mode);
}
/**
@@ -162,7 +162,7 @@ public enum RandomAccessFileMode {
*
* @param file the file object
* @return a random access file
- * @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}.
+ * @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
*/
public RandomAccessFile create(final Path file) throws FileNotFoundException {
return create(Objects.requireNonNull(file.toFile(), "file"));
@@ -171,12 +171,12 @@ public enum RandomAccessFileMode {
/**
* Constructs a random access file to read from, and optionally to write to, the file specified by the {@link File} argument.
*
- * @param file the file object
+ * @param name the file object
* @return a random access file
- * @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}.
+ * @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
*/
- public RandomAccessFile create(final String file) throws FileNotFoundException {
- return new RandomAccessFileAccessor(file, mode);
+ public RandomAccessFile create(final String name) throws FileNotFoundException {
+ return new IORandomAccessFile(name, mode);
}
/**
@@ -230,4 +230,16 @@ public enum RandomAccessFileMode {
return getLevel() >= other.getLevel();
}
+ /**
+ * Constructs a random access file to read from, and optionally to write to, the file specified by the {@link File} argument.
+ *
+ * @param name the file object
+ * @return a random access file
+ * @throws FileNotFoundException See {@link IORandomAccessFile#IORandomAccessFile(File, String)}.
+ * @since 2.18.0
+ */
+ public IORandomAccessFile io(final String name) throws FileNotFoundException {
+ return new IORandomAccessFile(name, mode);
+ }
+
}
diff --git a/src/test/java/org/apache/commons/io/RandomAccessFileModeTest.java b/src/test/java/org/apache/commons/io/RandomAccessFileModeTest.java
index 881509fa7..394d5db9c 100644
--- a/src/test/java/org/apache/commons/io/RandomAccessFileModeTest.java
+++ b/src/test/java/org/apache/commons/io/RandomAccessFileModeTest.java
@@ -40,6 +40,10 @@ import org.junit.jupiter.params.provider.EnumSource;
*/
public class RandomAccessFileModeTest {
+ private static final byte[] BYTES_FIXTURE = "Foo".getBytes(StandardCharsets.US_ASCII);
+
+ private static final String FIXTURE = "test.txt";
+
/**
* Temporary directory.
*/
@@ -53,8 +57,8 @@ public class RandomAccessFileModeTest {
@ParameterizedTest
@EnumSource(RandomAccessFileMode.class)
public void testCreateFile(final RandomAccessFileMode randomAccessFileMode) throws IOException {
- final byte[] expected = "Foo".getBytes(StandardCharsets.US_ASCII);
- final Path fixture = Files.write(tempDir.resolve("test.txt"), expected);
+ final byte[] expected = BYTES_FIXTURE;
+ final Path fixture = writeFixture(expected);
try (RandomAccessFile randomAccessFile = randomAccessFileMode.create(fixture.toFile())) {
assertArrayEquals(expected, read(randomAccessFile));
}
@@ -63,8 +67,8 @@ public class RandomAccessFileModeTest {
@ParameterizedTest
@EnumSource(RandomAccessFileMode.class)
public void testCreatePath(final RandomAccessFileMode randomAccessFileMode) throws IOException {
- final byte[] expected = "Foo".getBytes(StandardCharsets.US_ASCII);
- final Path fixture = Files.write(tempDir.resolve("test.txt"), expected);
+ final byte[] expected = BYTES_FIXTURE;
+ final Path fixture = writeFixture(expected);
try (RandomAccessFile randomAccessFile = randomAccessFileMode.create(fixture)) {
assertArrayEquals(expected, read(randomAccessFile));
}
@@ -73,8 +77,8 @@ public class RandomAccessFileModeTest {
@ParameterizedTest
@EnumSource(RandomAccessFileMode.class)
public void testCreateString(final RandomAccessFileMode randomAccessFileMode) throws IOException {
- final byte[] expected = "Foo".getBytes(StandardCharsets.US_ASCII);
- final Path fixture = Files.write(tempDir.resolve("test.txt"), expected);
+ final byte[] expected = BYTES_FIXTURE;
+ final Path fixture = writeFixture(expected);
try (RandomAccessFile randomAccessFile = randomAccessFileMode.create(fixture.toString())) {
assertArrayEquals(expected, read(randomAccessFile));
}
@@ -96,6 +100,16 @@ public class RandomAccessFileModeTest {
assertFalse(RandomAccessFileMode.READ_ONLY.implies(RandomAccessFileMode.READ_WRITE_SYNC_ALL));
}
+ @ParameterizedTest
+ @EnumSource(RandomAccessFileMode.class)
+ public void testIoString(final RandomAccessFileMode randomAccessFileMode) throws IOException {
+ final byte[] expected = BYTES_FIXTURE;
+ final Path fixture = writeFixture(expected);
+ try (IORandomAccessFile randomAccessFile = randomAccessFileMode.io(fixture.toString())) {
+ assertArrayEquals(expected, read(randomAccessFile));
+ }
+ }
+
/**
* Tests the standard {@link Enum#toString()} behavior.
*/
@@ -145,4 +159,8 @@ public class RandomAccessFileModeTest {
assertEquals(RandomAccessFileMode.READ_WRITE_SYNC_ALL,
RandomAccessFileMode.valueOf(StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.SYNC));
}
+
+ private Path writeFixture(final byte[] bytes) throws IOException {
+ return Files.write(tempDir.resolve(FIXTURE), bytes, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
+ }
}