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/13 18:20:21 UTC

(commons-io) branch master updated: Add RandomAccessFile support to the org.apache.commons.io.build package

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 6df85e8e0 Add RandomAccessFile support to the org.apache.commons.io.build package
6df85e8e0 is described below

commit 6df85e8e0b81f99c422b3700a973e758222c37ad
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Oct 13 14:20:15 2024 -0400

    Add RandomAccessFile support to the org.apache.commons.io.build package
    
    - Add RandomAccessFileOutputStream
    - Add AbstractOrigin.getRandomAccessFile(OpenOption...)
    - Add AbstractOriginSupplier.newRandomAccessFileOrigin(RandomAccessFile)
    - Add AbstractOriginSupplier.setRandomAccessFile(RandomAccessFile)
    - Add AbstractStreamBuilder.getFile()
    - Add AbstractStreamBuilder.getRandomAccessFile()
    - Add BufferedFileChannelInputStream.Builder.setFileChannel(FileChannel)
---
 .../apache/commons/io/build/AbstractOrigin.java    | 149 ++++++++++++++++-
 .../commons/io/build/AbstractOriginSupplier.java   |  48 ++++++
 .../commons/io/build/AbstractStreamBuilder.java    |  28 ++++
 .../io/input/BufferedFileChannelInputStream.java   |  28 +++-
 .../io/input/RandomAccessFileInputStream.java      |  18 +--
 .../io/output/RandomAccessFileOutputStream.java    | 104 ++++++++++++
 .../commons/io/build/AbstractOriginTest.java       | 115 +++++++++++++-
 ...ava => AbstractRandomAccessFileOriginTest.java} |  28 ++--
 .../commons/io/build/ByteArrayOriginTest.java      |  32 +++-
 .../commons/io/build/CharSequenceOriginTest.java   |  37 ++++-
 .../apache/commons/io/build/FileOriginTest.java    |  13 +-
 .../io/build/IORandomAccessFileOriginTest.java     |  45 ++++++
 .../commons/io/build/InputStreamOriginTest.java    |  33 +++-
 .../commons/io/build/OutputStreamOriginTest.java   |  33 +++-
 .../apache/commons/io/build/PathOriginTest.java    |  13 +-
 ...inTest.java => RandomAccessFileOriginTest.java} |  46 ++----
 .../apache/commons/io/build/ReaderOriginTest.java  |  33 +++-
 .../org/apache/commons/io/build/URIOriginTest.java |  13 +-
 .../commons/io/build/WriterStreamOriginTest.java   |  32 +++-
 .../input/BufferedFileChannelInputStreamTest.java  |   2 +
 .../output/RandomAccessFileOutputStreamTest.java   | 176 +++++++++++++++++++++
 21 files changed, 911 insertions(+), 115 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/build/AbstractOrigin.java b/src/main/java/org/apache/commons/io/build/AbstractOrigin.java
index 4912b4002..c9801d810 100644
--- a/src/main/java/org/apache/commons/io/build/AbstractOrigin.java
+++ b/src/main/java/org/apache/commons/io/build/AbstractOrigin.java
@@ -19,6 +19,7 @@ package org.apache.commons.io.build;
 
 import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -37,13 +38,16 @@ import java.nio.file.spi.FileSystemProvider;
 import java.util.Arrays;
 import java.util.Objects;
 
+import org.apache.commons.io.IORandomAccessFile;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.RandomAccessFileMode;
 import org.apache.commons.io.RandomAccessFiles;
 import org.apache.commons.io.file.spi.FileSystemProviders;
+import org.apache.commons.io.input.BufferedFileChannelInputStream;
 import org.apache.commons.io.input.CharSequenceInputStream;
 import org.apache.commons.io.input.CharSequenceReader;
 import org.apache.commons.io.input.ReaderInputStream;
+import org.apache.commons.io.output.RandomAccessFileOutputStream;
 import org.apache.commons.io.output.WriterOutputStream;
 
 /**
@@ -60,6 +64,83 @@ import org.apache.commons.io.output.WriterOutputStream;
  */
 public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends AbstractSupplier<T, B> {
 
+    /**
+     * A {@link RandomAccessFile} origin.
+     * <p>
+     * This origin cannot support File and Path since you cannot query a RandomAccessFile for those attributes; Use {@link IORandomAccessFileOrigin}
+     * instead.
+     * </p>
+     *
+     * @param <T> the type of instances to build.
+     * @param <B> the type of builder subclass.
+     */
+    public static abstract class AbstractRandomAccessFileOrigin<T extends RandomAccessFile, B extends AbstractRandomAccessFileOrigin<T, B>>
+            extends AbstractOrigin<T, B> {
+
+        /**
+         * A {@link RandomAccessFile} origin.
+         * <p>
+         * Starting from this origin, you can everything except a Path and a File.
+         * </p>
+         *
+         * @param origin The origin.
+         */
+        public AbstractRandomAccessFileOrigin(final T origin) {
+            super(origin);
+        }
+
+        @Override
+        public byte[] getByteArray() throws IOException {
+            final long longLen = origin.length();
+            if (longLen > Integer.MAX_VALUE) {
+                throw new IllegalStateException("Origin too large.");
+            }
+            return RandomAccessFiles.read(origin, 0, (int) longLen);
+        }
+
+        @Override
+        public byte[] getByteArray(final long position, final int length) throws IOException {
+            return RandomAccessFiles.read(origin, position, length);
+        }
+
+        @Override
+        public CharSequence getCharSequence(final Charset charset) throws IOException {
+            return new String(getByteArray(), charset);
+        }
+
+        @SuppressWarnings("resource")
+        @Override
+        public InputStream getInputStream(final OpenOption... options) throws IOException {
+            return BufferedFileChannelInputStream.builder().setFileChannel(origin.getChannel()).get();
+        }
+
+        @Override
+        public OutputStream getOutputStream(final OpenOption... options) throws IOException {
+            return RandomAccessFileOutputStream.builder().setRandomAccessFile(origin).get();
+        }
+
+        @Override
+        public T getRandomAccessFile(final OpenOption... openOption) {
+            // No conversion
+            return get();
+        }
+
+        @Override
+        public Reader getReader(final Charset charset) throws IOException {
+            return new InputStreamReader(getInputStream(), charset);
+        }
+
+        @Override
+        public Writer getWriter(final Charset charset, final OpenOption... options) throws IOException {
+            return new OutputStreamWriter(getOutputStream(options), charset);
+        }
+
+        @Override
+        public long size() throws IOException {
+            return origin.length();
+        }
+    }
+
     /**
      * A {@code byte[]} origin.
      */
@@ -243,6 +324,35 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends
 
     }
 
+    /**
+     * A {@link IORandomAccessFile} origin.
+     *
+     * @since 2.18.0
+     */
+    public static class IORandomAccessFileOrigin extends AbstractRandomAccessFileOrigin<IORandomAccessFile, IORandomAccessFileOrigin> {
+
+        /**
+         * A {@link RandomAccessFile} origin.
+         *
+         * @param origin The origin.
+         */
+        public IORandomAccessFileOrigin(final IORandomAccessFile origin) {
+            super(origin);
+        }
+
+        @SuppressWarnings("resource")
+        @Override
+        public File getFile() {
+            return get().getFile();
+        }
+
+        @Override
+        public Path getPath() {
+            return getFile().toPath();
+        }
+
+    }
+
     /**
      * An {@link OutputStream} origin.
      * <p>
@@ -321,6 +431,29 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends
 
     }
 
+    /**
+     * A {@link RandomAccessFile} origin.
+     * <p>
+     * This origin cannot support File and Path since you cannot query a RandomAccessFile for those attributes; Use {@link IORandomAccessFileOrigin}
+     * instead.
+     * </p>
+     */
+    public static class RandomAccessFileOrigin extends AbstractRandomAccessFileOrigin<RandomAccessFile, RandomAccessFileOrigin> {
+
+        /**
+         * A {@link RandomAccessFile} origin.
+         * <p>
+         * Starting from this origin, you can everything except a Path and a File.
+         * </p>
+         *
+         * @param origin The origin.
+         */
+        public RandomAccessFileOrigin(final RandomAccessFile origin) {
+            super(origin);
+        }
+
+    }
+
     /**
      * A {@link Reader} origin.
      * <p>
@@ -503,7 +636,7 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends
     }
 
     /**
-     * Gets this origin as a byte array, if possible.
+     * Gets a portion of this origin as a byte array, if possible.
      *
      * @param position the initial index of the range to be copied, inclusive.
      * @param length   How many bytes to copy.
@@ -581,6 +714,20 @@ public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends
                 String.format("%s#getPath() for %s origin %s", getSimpleClassName(), origin.getClass().getSimpleName(), origin));
     }
 
+    /**
+     * Gets this origin as a RandomAccessFile, if possible.
+     *
+     * @param openOption TODO
+     *
+     * @return this origin as a RandomAccessFile, if possible.
+     * @throws FileNotFoundException         See {@link RandomAccessFile#RandomAccessFile(File, String)}.
+     * @throws UnsupportedOperationException if this method is not implemented in a concrete subclass.
+     * @since 2.18.0
+     */
+    public RandomAccessFile getRandomAccessFile(final OpenOption... openOption) throws FileNotFoundException {
+        return RandomAccessFileMode.valueOf(openOption).create(getFile());
+    }
+
     /**
      * Gets a new Reader on the origin, buffered by default.
      *
diff --git a/src/main/java/org/apache/commons/io/build/AbstractOriginSupplier.java b/src/main/java/org/apache/commons/io/build/AbstractOriginSupplier.java
index bd62682a4..768514cd9 100644
--- a/src/main/java/org/apache/commons/io/build/AbstractOriginSupplier.java
+++ b/src/main/java/org/apache/commons/io/build/AbstractOriginSupplier.java
@@ -20,18 +20,22 @@ package org.apache.commons.io.build;
 import java.io.File;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.RandomAccessFile;
 import java.io.Reader;
 import java.io.Writer;
 import java.net.URI;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 
+import org.apache.commons.io.IORandomAccessFile;
 import org.apache.commons.io.build.AbstractOrigin.ByteArrayOrigin;
 import org.apache.commons.io.build.AbstractOrigin.CharSequenceOrigin;
 import org.apache.commons.io.build.AbstractOrigin.FileOrigin;
+import org.apache.commons.io.build.AbstractOrigin.IORandomAccessFileOrigin;
 import org.apache.commons.io.build.AbstractOrigin.InputStreamOrigin;
 import org.apache.commons.io.build.AbstractOrigin.OutputStreamOrigin;
 import org.apache.commons.io.build.AbstractOrigin.PathOrigin;
+import org.apache.commons.io.build.AbstractOrigin.RandomAccessFileOrigin;
 import org.apache.commons.io.build.AbstractOrigin.ReaderOrigin;
 import org.apache.commons.io.build.AbstractOrigin.URIOrigin;
 import org.apache.commons.io.build.AbstractOrigin.WriterOrigin;
@@ -126,6 +130,28 @@ public abstract class AbstractOriginSupplier<T, B extends AbstractOriginSupplier
         return new PathOrigin(Paths.get(origin));
     }
 
+    /**
+     * Constructs a new RandomAccessFile origin for a RandomAccessFile.
+     *
+     * @param origin the reader.
+     * @return a new reader origin.
+     * @since 2.18.0
+     */
+    protected static IORandomAccessFileOrigin newRandomAccessFileOrigin(final IORandomAccessFile origin) {
+        return new IORandomAccessFileOrigin(origin);
+    }
+
+    /**
+     * Constructs a new RandomAccessFile origin for a RandomAccessFile.
+     *
+     * @param origin the reader.
+     * @return a new reader origin.
+     * @since 2.18.0
+     */
+    protected static RandomAccessFileOrigin newRandomAccessFileOrigin(final RandomAccessFile origin) {
+        return new RandomAccessFileOrigin(origin);
+    }
+
     /**
      * Constructs a new reader origin for a reader.
      *
@@ -284,6 +310,28 @@ public abstract class AbstractOriginSupplier<T, B extends AbstractOriginSupplier
         return setOrigin(newPathOrigin(origin));
     }
 
+    /**
+     * Sets a new origin.
+     *
+     * @param origin the new origin.
+     * @return {@code this} instance.
+     * @since 2.18.0
+     */
+    public B setRandomAccessFile(final IORandomAccessFile origin) {
+        return setOrigin(newRandomAccessFileOrigin(origin));
+    }
+
+    /**
+     * Sets a new origin.
+     *
+     * @param origin the new origin.
+     * @return {@code this} instance.
+     * @since 2.18.0
+     */
+    public B setRandomAccessFile(final RandomAccessFile origin) {
+        return setOrigin(newRandomAccessFileOrigin(origin));
+    }
+
     /**
      * Sets a new origin.
      *
diff --git a/src/main/java/org/apache/commons/io/build/AbstractStreamBuilder.java b/src/main/java/org/apache/commons/io/build/AbstractStreamBuilder.java
index 60d2b04d7..c14b9d55c 100644
--- a/src/main/java/org/apache/commons/io/build/AbstractStreamBuilder.java
+++ b/src/main/java/org/apache/commons/io/build/AbstractStreamBuilder.java
@@ -17,9 +17,11 @@
 
 package org.apache.commons.io.build;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.RandomAccessFile;
 import java.io.Reader;
 import java.io.Writer;
 import java.nio.charset.Charset;
@@ -141,6 +143,19 @@ public abstract class AbstractStreamBuilder<T, B extends AbstractStreamBuilder<T
         return charsetDefault;
     }
 
+    /**
+     * Gets a File from the origin.
+     *
+     * @return A File
+     * @throws IllegalStateException         if the {@code origin} is {@code null}.
+     * @throws UnsupportedOperationException if the origin cannot be converted to a {@link File}.
+     * @see AbstractOrigin#getPath()
+     * @since 2.18.0
+     */
+    protected File getFile() {
+        return checkOrigin().getFile();
+    }
+
     /**
      * Gets an InputStream from the origin with OpenOption[].
      *
@@ -193,6 +208,19 @@ public abstract class AbstractStreamBuilder<T, B extends AbstractStreamBuilder<T
         return checkOrigin().getPath();
     }
 
+    /**
+     * Gets a RandomAccessFile from the origin.
+     *
+     * @return A RandomAccessFile
+     * @throws IllegalStateException         if the {@code origin} is {@code null}.
+     * @throws UnsupportedOperationException if the origin cannot be converted to a {@link RandomAccessFile}.
+     * @throws IOException                   if an I/O error occurs.
+     * @since 2.18.0
+     */
+    protected RandomAccessFile getRandomAccessFile() throws IOException {
+        return checkOrigin().getRandomAccessFile(getOpenOptions());
+    }
+
     /**
      * Gets a Reader from the origin with a Charset.
      *
diff --git a/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java b/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java
index 039ea2c10..bede06b46 100644
--- a/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java
@@ -73,6 +73,8 @@ public final class BufferedFileChannelInputStream extends InputStream {
     // @formatter:on
     public static class Builder extends AbstractStreamBuilder<BufferedFileChannelInputStream, Builder> {
 
+        private FileChannel fileChannel;
+
         /**
          * Builds a new {@link BufferedFileChannelInputStream}.
          * <p>
@@ -95,7 +97,23 @@ public final class BufferedFileChannelInputStream extends InputStream {
          */
         @Override
         public BufferedFileChannelInputStream get() throws IOException {
-            return new BufferedFileChannelInputStream(getPath(), getBufferSize());
+            return fileChannel != null ? new BufferedFileChannelInputStream(fileChannel, getBufferSize())
+                    : new BufferedFileChannelInputStream(getPath(), getBufferSize());
+        }
+
+        /**
+         * Sets the file channel.
+         * <p>
+         * This setting takes precedence over all others.
+         * </p>
+         *
+         * @param fileChannel the file channel.
+         * @return this instance.
+         * @since 2.18.0
+         */
+        public Builder setFileChannel(final FileChannel fileChannel) {
+            this.fileChannel = fileChannel;
+            return this;
         }
 
     }
@@ -159,10 +177,14 @@ public final class BufferedFileChannelInputStream extends InputStream {
      * @throws IOException If an I/O error occurs
      * @deprecated Use {@link #builder()}, {@link Builder}, and {@link Builder#get()}
      */
+    @SuppressWarnings("resource")
     @Deprecated
     public BufferedFileChannelInputStream(final Path path, final int bufferSize) throws IOException {
-        Objects.requireNonNull(path, "path");
-        fileChannel = FileChannel.open(path, StandardOpenOption.READ);
+        this(FileChannel.open(path, StandardOpenOption.READ), bufferSize);
+    }
+
+    private BufferedFileChannelInputStream(final FileChannel fileChannel, final int bufferSize) {
+        this.fileChannel = Objects.requireNonNull(fileChannel, "path");
         byteBuffer = ByteBuffer.allocateDirect(bufferSize);
         byteBuffer.flip();
     }
diff --git a/src/main/java/org/apache/commons/io/input/RandomAccessFileInputStream.java b/src/main/java/org/apache/commons/io/input/RandomAccessFileInputStream.java
index 93bd55a92..522ac4606 100644
--- a/src/main/java/org/apache/commons/io/input/RandomAccessFileInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/RandomAccessFileInputStream.java
@@ -22,7 +22,6 @@ import java.io.IOException;
 import java.io.RandomAccessFile;
 import java.util.Objects;
 
-import org.apache.commons.io.RandomAccessFileMode;
 import org.apache.commons.io.build.AbstractOrigin;
 import org.apache.commons.io.build.AbstractStreamBuilder;
 
@@ -57,7 +56,7 @@ public class RandomAccessFileInputStream extends AbstractInputStream {
     // @formatter:on
     public static class Builder extends AbstractStreamBuilder<RandomAccessFileInputStream, Builder> {
 
-        private RandomAccessFile randomAccessFile;
+        // private RandomAccessFile randomAccessFile;
         private boolean propagateClose;
 
         /**
@@ -84,14 +83,7 @@ public class RandomAccessFileInputStream extends AbstractInputStream {
         @SuppressWarnings("resource") // Caller closes depending on settings
         @Override
         public RandomAccessFileInputStream get() throws IOException {
-            if (randomAccessFile != null) {
-                if (getOrigin() != null) {
-                    throw new IllegalStateException(String.format("Only set one of RandomAccessFile (%s) or origin (%s)", randomAccessFile, getOrigin()));
-                }
-                return new RandomAccessFileInputStream(randomAccessFile, propagateClose);
-            }
-            // No need to parameterize the RandomAccessFileMode since we are only reading.
-            return new RandomAccessFileInputStream(RandomAccessFileMode.READ_ONLY.create(checkOrigin().getFile()), propagateClose);
+            return new RandomAccessFileInputStream(getRandomAccessFile(), propagateClose);
         }
 
         /**
@@ -111,9 +103,9 @@ public class RandomAccessFileInputStream extends AbstractInputStream {
          * @param randomAccessFile the RandomAccessFile to stream.
          * @return {@code this} instance.
          */
-        public Builder setRandomAccessFile(final RandomAccessFile randomAccessFile) {
-            this.randomAccessFile = randomAccessFile;
-            return this;
+        @Override // MUST keep this method for binary compatibility since the super version of this method uses a generic which compiles to Object.
+        public Builder setRandomAccessFile(final RandomAccessFile randomAccessFile) { // NOPMD see above.
+            return super.setRandomAccessFile(randomAccessFile);
         }
 
     }
diff --git a/src/main/java/org/apache/commons/io/output/RandomAccessFileOutputStream.java b/src/main/java/org/apache/commons/io/output/RandomAccessFileOutputStream.java
new file mode 100644
index 000000000..4aa1e1d30
--- /dev/null
+++ b/src/main/java/org/apache/commons/io/output/RandomAccessFileOutputStream.java
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.io.output;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.RandomAccessFile;
+import java.nio.file.StandardOpenOption;
+import java.util.Objects;
+
+import org.apache.commons.io.build.AbstractStreamBuilder;
+
+/**
+ * An {@link OutputStream} that writes to a {@link RandomAccessFile}.
+ *
+ * @since 2.18.0
+ */
+public final class RandomAccessFileOutputStream extends OutputStream {
+
+    // @formatter:off
+    /**
+     * Builds a new {@link RandomAccessFileOutputStream}.
+     * <p>
+     * For example:
+     * </p>
+     * <pre>{@code
+     * RandomAccessFileOutputStream s = RandomAccessFileOutputStream.builder()
+     *   .setFile("myfile.txt")
+     *   .setOpenOptions(StandardOpenOption.SYNC)
+     *   .get();}
+     * </pre>
+     * <p>
+     * The only super's aspect used is buffer size.
+     * </p>
+     *
+     * @see #get()
+     */
+    // @formatter:on
+    public final static class Builder extends AbstractStreamBuilder<RandomAccessFileOutputStream, Builder> {
+
+        /**
+         * Use {@link RandomAccessFileOutputStream#builder()}.
+         */
+        private Builder() {
+            setOpenOptions(StandardOpenOption.WRITE);
+        }
+
+        @SuppressWarnings("resource")
+        @Override
+        public RandomAccessFileOutputStream get() throws IOException {
+            return new RandomAccessFileOutputStream(getRandomAccessFile());
+        }
+
+    }
+
+    /**
+     * Constructs a new {@link Builder}.
+     *
+     * @return a new {@link Builder}.
+     */
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    private final RandomAccessFile randomAccessFile;
+
+    private RandomAccessFileOutputStream(final RandomAccessFile randomAccessFile) {
+        this.randomAccessFile = Objects.requireNonNull(randomAccessFile);
+    }
+
+    @Override
+    public void write(final int b) throws IOException {
+        randomAccessFile.write(b);
+    }
+
+    @SuppressWarnings("resource")
+    @Override
+    public void flush() throws IOException {
+        randomAccessFile.getChannel().force(true);
+        super.flush();
+    }
+
+    @Override
+    public void close() throws IOException {
+        this.randomAccessFile.close();
+        super.close();
+    }
+
+}
diff --git a/src/test/java/org/apache/commons/io/build/AbstractOriginTest.java b/src/test/java/org/apache/commons/io/build/AbstractOriginTest.java
index 8cd1c1ff2..0a872ffd8 100644
--- a/src/test/java/org/apache/commons/io/build/AbstractOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/AbstractOriginTest.java
@@ -14,23 +14,36 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package org.apache.commons.io.build;
 
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.RandomAccessFile;
 import java.io.Reader;
 import java.io.Writer;
 import java.nio.charset.Charset;
 import java.nio.file.Files;
+import java.nio.file.OpenOption;
+import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
 import java.util.Objects;
 
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.build.AbstractOrigin.RandomAccessFileOrigin;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
 
 /**
  * Tests {@link AbstractOrigin} and subclasses.
@@ -43,10 +56,17 @@ public abstract class AbstractOriginTest<T, B extends AbstractOrigin<T, B>> {
     protected static final String FILE_RES_RO = "/org/apache/commons/io/test-file-20byteslength.bin";
     protected static final String FILE_NAME_RO = "src/test/resources" + FILE_RES_RO;
     protected static final String FILE_NAME_RW = "target/" + AbstractOriginTest.class.getSimpleName() + ".txt";
+    private static final int RO_LENGTH = 20;
 
     protected AbstractOrigin<T, B> originRo;
     protected AbstractOrigin<T, B> originRw;
 
+    @BeforeEach
+    public void beforeEach() throws IOException {
+        setOriginRo(newOriginRo());
+        setOriginRw(newOriginRw());
+    }
+
     protected AbstractOrigin<T, B> getOriginRo() {
         return Objects.requireNonNull(originRo, "originRo");
     }
@@ -55,6 +75,15 @@ public abstract class AbstractOriginTest<T, B extends AbstractOrigin<T, B>> {
         return Objects.requireNonNull(originRw, "originRw");
     }
 
+    @SuppressWarnings("resource")
+    private boolean isValid(final RandomAccessFile raf) throws IOException {
+        return Objects.requireNonNull(raf).getFD().valid();
+    }
+
+    protected abstract B newOriginRo() throws IOException;
+
+    protected abstract B newOriginRw() throws IOException;
+
     protected void setOriginRo(final AbstractOrigin<T, B> origin) {
         this.originRo = origin;
     }
@@ -89,8 +118,17 @@ public abstract class AbstractOriginTest<T, B extends AbstractOrigin<T, B>> {
     }
 
     @Test
-    public void testGetFile() {
-        assertNotNull(getOriginRo().getFile());
+    public void testGetFile() throws IOException {
+        testGetFile(getOriginRo().getFile(), RO_LENGTH);
+        FileUtils.touch(getOriginRw().getFile());
+        testGetFile(getOriginRw().getFile(), 0);
+    }
+
+    private void testGetFile(final File file, final long expectedLen) throws IOException {
+        assertNotNull(file);
+        assertTrue(file.exists(), () -> "File does not exist: " + file);
+        final int length = FileUtils.readFileToByteArray(file).length;
+        assertEquals(length, expectedLen, () -> String.format("File %s, actual length=%,d", file, length));
     }
 
     @Test
@@ -108,8 +146,77 @@ public abstract class AbstractOriginTest<T, B extends AbstractOrigin<T, B>> {
     }
 
     @Test
-    public void testGetPath() {
-        assertNotNull(getOriginRo().getPath());
+    public void testGetPath() throws IOException {
+        testGetPath(getOriginRo().getPath(), RO_LENGTH);
+        FileUtils.touch(getOriginRw().getPath().toFile());
+        testGetPath(getOriginRw().getPath(), 0);
+    }
+
+    private void testGetPath(final Path path, final long expectedLen) throws IOException {
+        assertNotNull(path);
+        assertTrue(Files.exists(path));
+        final int length = Files.readAllBytes(path).length;
+        assertEquals(length, expectedLen, () -> String.format("Path %s, actual length=%,d", path, length));
+    }
+
+    @Test
+    public void testGetRandomAccessFile() throws IOException {
+        // Default
+        try (final RandomAccessFile raf = getOriginRo().getRandomAccessFile()) {
+            assertNotNull(raf);
+            assertTrue(isValid(raf));
+        }
+        final boolean isRafOriginRo = getOriginRo() instanceof RandomAccessFileOrigin;
+        final boolean isRafOriginRw = getOriginRw() instanceof RandomAccessFileOrigin;
+        // Same as above, but underlying resource is now closed.
+        try (final RandomAccessFile raf = getOriginRo().getRandomAccessFile()) {
+            assertNotNull(raf);
+            assertFalse(isRafOriginRo && isValid(raf));
+        }
+        // Read
+        try (final RandomAccessFile raf = getOriginRo().getRandomAccessFile(StandardOpenOption.READ)) {
+            assertNotNull(raf);
+            assertFalse(isRafOriginRo && isValid(raf));
+        }
+        // Write, first access
+        try (final RandomAccessFile raf = getOriginRw().getRandomAccessFile(StandardOpenOption.WRITE)) {
+            assertNotNull(raf);
+            if (isRafOriginRw || (getOriginRw().getFile() != null)) {
+                assertTrue(isValid(raf), () -> getOriginRw().toString());
+            } else {
+                // Can't get there from here.
+                assertFalse(isValid(raf), () -> getOriginRw().toString());
+            }
+        }
+        // Read, Write, underlying resource is now closed.
+        try (final RandomAccessFile raf = getOriginRw().getRandomAccessFile(StandardOpenOption.READ, StandardOpenOption.WRITE)) {
+            assertNotNull(raf);
+            assertFalse(isRafOriginRw && isValid(raf));
+        }
+    }
+
+    @ParameterizedTest
+    @EnumSource(StandardOpenOption.class)
+    public void testGetRandomAccessFile(final OpenOption openOption) throws IOException {
+        // Default
+        try (final RandomAccessFile raf = getOriginRw().getRandomAccessFile()) {
+            assertNotNull(raf);
+            assertTrue(isValid(raf));
+        }
+        // Same as above, but underlying resource is now closed.
+        final boolean isRafOrigin = getOriginRw() instanceof RandomAccessFileOrigin;
+        try (final RandomAccessFile raf = getOriginRw().getRandomAccessFile()) {
+            assertNotNull(raf);
+            assertFalse(isRafOrigin && isValid(raf));
+        }
+        try (final RandomAccessFile raf = getOriginRw().getRandomAccessFile(openOption)) {
+            assertNotNull(raf);
+            assertFalse(isRafOrigin && isValid(raf));
+        }
+        try (final RandomAccessFile raf = getOriginRw().getRandomAccessFile(openOption)) {
+            assertNotNull(raf);
+            assertFalse(isRafOrigin && isValid(raf));
+        }
     }
 
     @Test
diff --git a/src/test/java/org/apache/commons/io/build/PathOriginTest.java b/src/test/java/org/apache/commons/io/build/AbstractRandomAccessFileOriginTest.java
similarity index 54%
copy from src/test/java/org/apache/commons/io/build/PathOriginTest.java
copy to src/test/java/org/apache/commons/io/build/AbstractRandomAccessFileOriginTest.java
index 0ca6dce44..f0545851c 100644
--- a/src/test/java/org/apache/commons/io/build/PathOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/AbstractRandomAccessFileOriginTest.java
@@ -14,27 +14,25 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package org.apache.commons.io.build;
 
-import java.nio.file.Path;
-import java.nio.file.Paths;
+import java.io.RandomAccessFile;
 
-import org.apache.commons.io.build.AbstractOrigin.PathOrigin;
-import org.junit.jupiter.api.BeforeEach;
+import org.apache.commons.io.IORandomAccessFile;
+import org.apache.commons.io.build.AbstractOrigin.AbstractRandomAccessFileOrigin;
+import org.apache.commons.io.build.AbstractOrigin.IORandomAccessFileOrigin;
+import org.apache.commons.io.build.AbstractOrigin.RandomAccessFileOrigin;
 
 /**
- * Tests {@link PathOrigin}.
- *
- * A PathOrigin can convert into all other aspects.
+ * Tests {@link RandomAccessFileOrigin} and {@link IORandomAccessFileOrigin}.
  *
- * @see Path
+ * @param <T> the type of instances to build.
+ * @param <B> the type of builder subclass.
+ * @see RandomAccessFile
+ * @see IORandomAccessFile
  */
-public class PathOriginTest extends AbstractOriginTest<Path, PathOrigin> {
-
-    @BeforeEach
-    public void beforeEach() {
-        setOriginRo(new PathOrigin(Paths.get(FILE_NAME_RO)));
-        setOriginRw(new PathOrigin(Paths.get(FILE_NAME_RW)));
-    }
+public abstract class AbstractRandomAccessFileOriginTest<T extends RandomAccessFile, B extends AbstractRandomAccessFileOrigin<T, B>>
+        extends AbstractOriginTest<T, B> {
 
 }
diff --git a/src/test/java/org/apache/commons/io/build/ByteArrayOriginTest.java b/src/test/java/org/apache/commons/io/build/ByteArrayOriginTest.java
index 9653d8b67..0176758c6 100644
--- a/src/test/java/org/apache/commons/io/build/ByteArrayOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/ByteArrayOriginTest.java
@@ -20,11 +20,14 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.IOException;
 import java.nio.file.Files;
+import java.nio.file.OpenOption;
 import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
 
 import org.apache.commons.io.build.AbstractOrigin.ByteArrayOrigin;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
 
 /**
  * Tests {@link ByteArrayOrigin}.
@@ -33,10 +36,14 @@ import org.junit.jupiter.api.Test;
  */
 public class ByteArrayOriginTest extends AbstractOriginTest<byte[], ByteArrayOrigin> {
 
-    @BeforeEach
-    public void beforeEach() throws IOException {
-        setOriginRo(new ByteArrayOrigin(Files.readAllBytes(Paths.get(FILE_NAME_RO))));
-        setOriginRw(new ByteArrayOrigin(new byte[] { 1 }));
+    @Override
+    protected ByteArrayOrigin newOriginRo() throws IOException {
+        return new ByteArrayOrigin(Files.readAllBytes(Paths.get(FILE_NAME_RO)));
+    }
+
+    @Override
+    protected ByteArrayOrigin newOriginRw() {
+        return new ByteArrayOrigin(new byte[] { 1 });
     }
 
     @Override
@@ -60,6 +67,21 @@ public class ByteArrayOriginTest extends AbstractOriginTest<byte[], ByteArrayOri
         assertThrows(UnsupportedOperationException.class, super::testGetPath);
     }
 
+    @Override
+    @Test
+    public void testGetRandomAccessFile() {
+        // Cannot convert a RandomAccessFile to a File.
+        assertThrows(UnsupportedOperationException.class, super::testGetRandomAccessFile);
+    }
+
+    @Override
+    @ParameterizedTest
+    @EnumSource(StandardOpenOption.class)
+    public void testGetRandomAccessFile(final OpenOption openOption) {
+        // Cannot convert a RandomAccessFile to a File.
+        assertThrows(UnsupportedOperationException.class, () -> super.testGetRandomAccessFile(openOption));
+    }
+
     @Override
     @Test
     public void testGetWriter() {
diff --git a/src/test/java/org/apache/commons/io/build/CharSequenceOriginTest.java b/src/test/java/org/apache/commons/io/build/CharSequenceOriginTest.java
index d9b39cc11..804414d64 100644
--- a/src/test/java/org/apache/commons/io/build/CharSequenceOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/CharSequenceOriginTest.java
@@ -20,15 +20,17 @@ 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.FileNotFoundException;
 import java.io.IOException;
 import java.io.Reader;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.OpenOption;
+import java.nio.file.StandardOpenOption;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.build.AbstractOrigin.CharSequenceOrigin;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
 
 /**
  * Tests {@link CharSequenceOrigin}.
@@ -39,16 +41,20 @@ import org.junit.jupiter.api.Test;
  */
 public class CharSequenceOriginTest extends AbstractOriginTest<CharSequence, CharSequenceOrigin> {
 
-    @BeforeEach
-    public void beforeEach() throws FileNotFoundException, IOException {
-        setOriginRo(new CharSequenceOrigin(getFixtureStringFromFile()));
-        setOriginRw(new CharSequenceOrigin("World"));
-    }
-
     private String getFixtureStringFromFile() throws IOException {
         return IOUtils.resourceToString(FILE_RES_RO, StandardCharsets.UTF_8);
     }
 
+    @Override
+    protected CharSequenceOrigin newOriginRo() throws IOException {
+        return new CharSequenceOrigin(getFixtureStringFromFile());
+    }
+
+    @Override
+    protected CharSequenceOrigin newOriginRw() {
+        return new CharSequenceOrigin("World");
+    }
+
     @Override
     @Test
     public void testGetFile() {
@@ -70,6 +76,21 @@ public class CharSequenceOriginTest extends AbstractOriginTest<CharSequence, Cha
         assertThrows(UnsupportedOperationException.class, super::testGetPath);
     }
 
+    @Override
+    @Test
+    public void testGetRandomAccessFile() {
+        // Cannot convert a RandomAccessFile to a File.
+        assertThrows(UnsupportedOperationException.class, super::testGetRandomAccessFile);
+    }
+
+    @Override
+    @ParameterizedTest
+    @EnumSource(StandardOpenOption.class)
+    public void testGetRandomAccessFile(final OpenOption openOption) {
+        // Cannot convert a RandomAccessFile to a File.
+        assertThrows(UnsupportedOperationException.class, () -> super.testGetRandomAccessFile(openOption));
+    }
+
     @Test
     public void testGetReaderIgnoreCharset() throws IOException {
         // The CharSequenceOrigin ignores the given Charset.
diff --git a/src/test/java/org/apache/commons/io/build/FileOriginTest.java b/src/test/java/org/apache/commons/io/build/FileOriginTest.java
index acbe85d63..27af8fb13 100644
--- a/src/test/java/org/apache/commons/io/build/FileOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/FileOriginTest.java
@@ -19,7 +19,6 @@ package org.apache.commons.io.build;
 import java.io.File;
 
 import org.apache.commons.io.build.AbstractOrigin.FileOrigin;
-import org.junit.jupiter.api.BeforeEach;
 
 /**
  * Tests {@link FileOrigin}.
@@ -30,10 +29,14 @@ import org.junit.jupiter.api.BeforeEach;
  */
 public class FileOriginTest extends AbstractOriginTest<File, FileOrigin> {
 
-    @BeforeEach
-    public void beforeEach() {
-        setOriginRo(new FileOrigin(new File(FILE_NAME_RO)));
-        setOriginRw(new FileOrigin(new File(FILE_NAME_RW)));
+    @Override
+    protected FileOrigin newOriginRo() {
+        return new FileOrigin(new File(FILE_NAME_RO));
+    }
+
+    @Override
+    protected FileOrigin newOriginRw() {
+        return new FileOrigin(new File(FILE_NAME_RW));
     }
 
 }
diff --git a/src/test/java/org/apache/commons/io/build/IORandomAccessFileOriginTest.java b/src/test/java/org/apache/commons/io/build/IORandomAccessFileOriginTest.java
new file mode 100644
index 000000000..86a759f47
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/build/IORandomAccessFileOriginTest.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.build;
+
+import java.io.FileNotFoundException;
+import java.io.RandomAccessFile;
+
+import org.apache.commons.io.IORandomAccessFile;
+import org.apache.commons.io.RandomAccessFileMode;
+import org.apache.commons.io.build.AbstractOrigin.IORandomAccessFileOrigin;
+
+/**
+ * Tests {@link IORandomAccessFileOrigin}.
+ *
+ * @see RandomAccessFile
+ */
+public class IORandomAccessFileOriginTest extends AbstractOriginTest<IORandomAccessFile, IORandomAccessFileOrigin> {
+
+    @SuppressWarnings("resource")
+    @Override
+    protected IORandomAccessFileOrigin newOriginRo() throws FileNotFoundException {
+        return new IORandomAccessFileOrigin(RandomAccessFileMode.READ_ONLY.io(FILE_NAME_RO));
+    }
+
+    @SuppressWarnings("resource")
+    @Override
+    protected IORandomAccessFileOrigin newOriginRw() throws FileNotFoundException {
+        return new IORandomAccessFileOrigin(RandomAccessFileMode.READ_WRITE.io(FILE_NAME_RW));
+    }
+
+}
diff --git a/src/test/java/org/apache/commons/io/build/InputStreamOriginTest.java b/src/test/java/org/apache/commons/io/build/InputStreamOriginTest.java
index f00a1bd46..0e47489b8 100644
--- a/src/test/java/org/apache/commons/io/build/InputStreamOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/InputStreamOriginTest.java
@@ -21,11 +21,14 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.InputStream;
+import java.nio.file.OpenOption;
+import java.nio.file.StandardOpenOption;
 
 import org.apache.commons.io.build.AbstractOrigin.InputStreamOrigin;
 import org.apache.commons.io.input.CharSequenceInputStream;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
 
 /**
  * Tests {@link InputStreamOrigin}.
@@ -37,10 +40,15 @@ import org.junit.jupiter.api.Test;
 public class InputStreamOriginTest extends AbstractOriginTest<InputStream, InputStreamOrigin> {
 
     @SuppressWarnings("resource")
-    @BeforeEach
-    public void beforeEach() throws FileNotFoundException {
-        setOriginRo(new InputStreamOrigin(new FileInputStream(FILE_NAME_RO)));
-        setOriginRw(new InputStreamOrigin(CharSequenceInputStream.builder().setCharSequence("World").get()));
+    @Override
+    protected InputStreamOrigin newOriginRo() throws FileNotFoundException {
+        return new InputStreamOrigin(new FileInputStream(FILE_NAME_RO));
+    }
+
+    @SuppressWarnings("resource")
+    @Override
+    protected InputStreamOrigin newOriginRw() {
+        return new InputStreamOrigin(CharSequenceInputStream.builder().setCharSequence("World").get());
     }
 
     @Override
@@ -64,6 +72,21 @@ public class InputStreamOriginTest extends AbstractOriginTest<InputStream, Input
         assertThrows(UnsupportedOperationException.class, super::testGetPath);
     }
 
+    @Override
+    @Test
+    public void testGetRandomAccessFile() {
+        // Cannot convert a RandomAccessFile to a File.
+        assertThrows(UnsupportedOperationException.class, super::testGetRandomAccessFile);
+    }
+
+    @Override
+    @ParameterizedTest
+    @EnumSource(StandardOpenOption.class)
+    public void testGetRandomAccessFile(final OpenOption openOption) {
+        // Cannot convert a RandomAccessFile to a File.
+        assertThrows(UnsupportedOperationException.class, () -> super.testGetRandomAccessFile(openOption));
+    }
+
     @Override
     @Test
     public void testGetWriter() {
diff --git a/src/test/java/org/apache/commons/io/build/OutputStreamOriginTest.java b/src/test/java/org/apache/commons/io/build/OutputStreamOriginTest.java
index fbeb34cbf..74f621e6e 100644
--- a/src/test/java/org/apache/commons/io/build/OutputStreamOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/OutputStreamOriginTest.java
@@ -19,11 +19,14 @@ package org.apache.commons.io.build;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.OutputStream;
+import java.nio.file.OpenOption;
+import java.nio.file.StandardOpenOption;
 
 import org.apache.commons.io.build.AbstractOrigin.OutputStreamOrigin;
 import org.apache.commons.io.output.ByteArrayOutputStream;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
 
 /**
  * Tests {@link OutputStreamOrigin}.
@@ -34,11 +37,14 @@ import org.junit.jupiter.api.Test;
  */
 public class OutputStreamOriginTest extends AbstractOriginTest<OutputStream, OutputStreamOrigin> {
 
-    @SuppressWarnings("resource")
-    @BeforeEach
-    public void beforeEach() {
-        setOriginRo(new OutputStreamOrigin(new ByteArrayOutputStream()));
-        setOriginRw(new OutputStreamOrigin(new ByteArrayOutputStream()));
+    @Override
+    protected OutputStreamOrigin newOriginRo() {
+        return new OutputStreamOrigin(new ByteArrayOutputStream());
+    }
+
+    @Override
+    protected OutputStreamOrigin newOriginRw() {
+        return new OutputStreamOrigin(new ByteArrayOutputStream());
     }
 
     @Override
@@ -97,6 +103,21 @@ public class OutputStreamOriginTest extends AbstractOriginTest<OutputStream, Out
         assertThrows(UnsupportedOperationException.class, super::testGetPath);
     }
 
+    @Override
+    @Test
+    public void testGetRandomAccessFile() {
+        // Cannot convert a RandomAccessFile to a File.
+        assertThrows(UnsupportedOperationException.class, super::testGetRandomAccessFile);
+    }
+
+    @Override
+    @ParameterizedTest
+    @EnumSource(StandardOpenOption.class)
+    public void testGetRandomAccessFile(final OpenOption openOption) {
+        // Cannot convert a RandomAccessFile to a File.
+        assertThrows(UnsupportedOperationException.class, () -> super.testGetRandomAccessFile(openOption));
+    }
+
     @Override
     @Test
     public void testGetReader() {
diff --git a/src/test/java/org/apache/commons/io/build/PathOriginTest.java b/src/test/java/org/apache/commons/io/build/PathOriginTest.java
index 0ca6dce44..182c25574 100644
--- a/src/test/java/org/apache/commons/io/build/PathOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/PathOriginTest.java
@@ -20,7 +20,6 @@ import java.nio.file.Path;
 import java.nio.file.Paths;
 
 import org.apache.commons.io.build.AbstractOrigin.PathOrigin;
-import org.junit.jupiter.api.BeforeEach;
 
 /**
  * Tests {@link PathOrigin}.
@@ -31,10 +30,14 @@ import org.junit.jupiter.api.BeforeEach;
  */
 public class PathOriginTest extends AbstractOriginTest<Path, PathOrigin> {
 
-    @BeforeEach
-    public void beforeEach() {
-        setOriginRo(new PathOrigin(Paths.get(FILE_NAME_RO)));
-        setOriginRw(new PathOrigin(Paths.get(FILE_NAME_RW)));
+    @Override
+    protected PathOrigin newOriginRo() {
+        return new PathOrigin(Paths.get(FILE_NAME_RO));
+    }
+
+    @Override
+    protected PathOrigin newOriginRw() {
+        return new PathOrigin(Paths.get(FILE_NAME_RW));
     }
 
 }
diff --git a/src/test/java/org/apache/commons/io/build/ReaderOriginTest.java b/src/test/java/org/apache/commons/io/build/RandomAccessFileOriginTest.java
similarity index 55%
copy from src/test/java/org/apache/commons/io/build/ReaderOriginTest.java
copy to src/test/java/org/apache/commons/io/build/RandomAccessFileOriginTest.java
index 601cc3034..9924498cc 100644
--- a/src/test/java/org/apache/commons/io/build/ReaderOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/RandomAccessFileOriginTest.java
@@ -14,61 +14,49 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package org.apache.commons.io.build;
 
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.Reader;
+import java.io.RandomAccessFile;
 
-import org.apache.commons.io.build.AbstractOrigin.ReaderOrigin;
-import org.apache.commons.io.input.CharSequenceReader;
-import org.junit.jupiter.api.BeforeEach;
+import org.apache.commons.io.RandomAccessFileMode;
+import org.apache.commons.io.build.AbstractOrigin.RandomAccessFileOrigin;
 import org.junit.jupiter.api.Test;
 
 /**
- * Tests {@link ReaderOrigin}.
- *
- * A ReaderOrigin can convert into some of the other aspects.
+ * Tests {@link RandomAccessFileOrigin}.
  *
- * @see Reader
+ * @see RandomAccessFile
  */
-public class ReaderOriginTest extends AbstractOriginTest<Reader, ReaderOrigin> {
+public class RandomAccessFileOriginTest extends AbstractRandomAccessFileOriginTest<RandomAccessFile, RandomAccessFileOrigin> {
 
     @SuppressWarnings("resource")
-    @BeforeEach
-    public void beforeEach() throws FileNotFoundException {
-        setOriginRo(new ReaderOrigin(new FileReader(FILE_NAME_RO)));
-        setOriginRw(new ReaderOrigin(new CharSequenceReader("World")));
+    @Override
+    protected RandomAccessFileOrigin newOriginRo() throws FileNotFoundException {
+        return new RandomAccessFileOrigin(RandomAccessFileMode.READ_ONLY.create(FILE_NAME_RO));
     }
 
+    @SuppressWarnings("resource")
     @Override
-    @Test
-    public void testGetFile() {
-        // Cannot convert a Reader to a File.
-        assertThrows(UnsupportedOperationException.class, super::testGetFile);
+    protected RandomAccessFileOrigin newOriginRw() throws FileNotFoundException {
+        return new RandomAccessFileOrigin(RandomAccessFileMode.READ_WRITE.create(FILE_NAME_RW));
     }
 
     @Override
     @Test
-    public void testGetOutputStream() {
-        // Cannot convert a Reader to an OutputStream.
-        assertThrows(UnsupportedOperationException.class, super::testGetOutputStream);
+    public void testGetFile() {
+        // Cannot convert a Writer to a File.
+        assertThrows(UnsupportedOperationException.class, super::testGetFile);
     }
 
     @Override
     @Test
     public void testGetPath() {
-        // Cannot convert a Reader to a Path.
+        // Cannot convert a Writer to a Path.
         assertThrows(UnsupportedOperationException.class, super::testGetPath);
     }
 
-    @Override
-    @Test
-    public void testGetWriter() {
-        // Cannot convert a Reader to a Writer.
-        assertThrows(UnsupportedOperationException.class, super::testGetWriter);
-    }
-
 }
diff --git a/src/test/java/org/apache/commons/io/build/ReaderOriginTest.java b/src/test/java/org/apache/commons/io/build/ReaderOriginTest.java
index 601cc3034..587088272 100644
--- a/src/test/java/org/apache/commons/io/build/ReaderOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/ReaderOriginTest.java
@@ -21,11 +21,14 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.Reader;
+import java.nio.file.OpenOption;
+import java.nio.file.StandardOpenOption;
 
 import org.apache.commons.io.build.AbstractOrigin.ReaderOrigin;
 import org.apache.commons.io.input.CharSequenceReader;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
 
 /**
  * Tests {@link ReaderOrigin}.
@@ -36,11 +39,14 @@ import org.junit.jupiter.api.Test;
  */
 public class ReaderOriginTest extends AbstractOriginTest<Reader, ReaderOrigin> {
 
-    @SuppressWarnings("resource")
-    @BeforeEach
-    public void beforeEach() throws FileNotFoundException {
-        setOriginRo(new ReaderOrigin(new FileReader(FILE_NAME_RO)));
-        setOriginRw(new ReaderOrigin(new CharSequenceReader("World")));
+    @Override
+    protected ReaderOrigin newOriginRo() throws FileNotFoundException {
+        return new ReaderOrigin(new FileReader(FILE_NAME_RO));
+    }
+
+    @Override
+    protected ReaderOrigin newOriginRw() {
+        return new ReaderOrigin(new CharSequenceReader("World"));
     }
 
     @Override
@@ -64,6 +70,21 @@ public class ReaderOriginTest extends AbstractOriginTest<Reader, ReaderOrigin> {
         assertThrows(UnsupportedOperationException.class, super::testGetPath);
     }
 
+    @Override
+    @Test
+    public void testGetRandomAccessFile() {
+        // Cannot convert a RandomAccessFile to a File.
+        assertThrows(UnsupportedOperationException.class, super::testGetRandomAccessFile);
+    }
+
+    @Override
+    @ParameterizedTest
+    @EnumSource(StandardOpenOption.class)
+    public void testGetRandomAccessFile(final OpenOption openOption) {
+        // Cannot convert a RandomAccessFile to a File.
+        assertThrows(UnsupportedOperationException.class, () -> super.testGetRandomAccessFile(openOption));
+    }
+
     @Override
     @Test
     public void testGetWriter() {
diff --git a/src/test/java/org/apache/commons/io/build/URIOriginTest.java b/src/test/java/org/apache/commons/io/build/URIOriginTest.java
index 2901af212..e33dcfd05 100644
--- a/src/test/java/org/apache/commons/io/build/URIOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/URIOriginTest.java
@@ -23,7 +23,6 @@ import java.net.URI;
 import java.nio.file.Paths;
 
 import org.apache.commons.io.build.AbstractOrigin.URIOrigin;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.ValueSource;
@@ -37,10 +36,14 @@ import org.junit.jupiter.params.provider.ValueSource;
  */
 public class URIOriginTest extends AbstractOriginTest<URI, URIOrigin> {
 
-    @BeforeEach
-    public void beforeEach() {
-        setOriginRo(new URIOrigin(Paths.get(FILE_NAME_RO).toUri()));
-        setOriginRw(new URIOrigin(Paths.get(FILE_NAME_RW).toUri()));
+    @Override
+    protected URIOrigin newOriginRo() {
+        return new URIOrigin(Paths.get(FILE_NAME_RO).toUri());
+    }
+
+    @Override
+    protected URIOrigin newOriginRw() {
+        return new URIOrigin(Paths.get(FILE_NAME_RW).toUri());
     }
 
     @ParameterizedTest
diff --git a/src/test/java/org/apache/commons/io/build/WriterStreamOriginTest.java b/src/test/java/org/apache/commons/io/build/WriterStreamOriginTest.java
index a6e48279b..1caefef70 100644
--- a/src/test/java/org/apache/commons/io/build/WriterStreamOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/WriterStreamOriginTest.java
@@ -20,10 +20,13 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.StringWriter;
 import java.io.Writer;
+import java.nio.file.OpenOption;
+import java.nio.file.StandardOpenOption;
 
 import org.apache.commons.io.build.AbstractOrigin.WriterOrigin;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
 
 /**
  * Tests {@link WriterOrigin}.
@@ -34,10 +37,14 @@ import org.junit.jupiter.api.Test;
  */
 public class WriterStreamOriginTest extends AbstractOriginTest<Writer, WriterOrigin> {
 
-    @BeforeEach
-    public void beforeEach() {
-        setOriginRo(new WriterOrigin(new StringWriter()));
-        setOriginRw(new WriterOrigin(new StringWriter()));
+    @Override
+    protected WriterOrigin newOriginRo() {
+        return newOriginRw();
+    }
+
+    @Override
+    protected WriterOrigin newOriginRw() {
+        return new WriterOrigin(new StringWriter());
     }
 
     @Override
@@ -96,6 +103,21 @@ public class WriterStreamOriginTest extends AbstractOriginTest<Writer, WriterOri
         assertThrows(UnsupportedOperationException.class, super::testGetPath);
     }
 
+    @Override
+    @Test
+    public void testGetRandomAccessFile() {
+        // Cannot convert a RandomAccessFile to a File.
+        assertThrows(UnsupportedOperationException.class, super::testGetRandomAccessFile);
+    }
+
+    @Override
+    @ParameterizedTest
+    @EnumSource(StandardOpenOption.class)
+    public void testGetRandomAccessFile(final OpenOption openOption) {
+        // Cannot convert a RandomAccessFile to a File.
+        assertThrows(UnsupportedOperationException.class, () -> super.testGetRandomAccessFile(openOption));
+    }
+
     @Override
     @Test
     public void testGetReader() {
diff --git a/src/test/java/org/apache/commons/io/input/BufferedFileChannelInputStreamTest.java b/src/test/java/org/apache/commons/io/input/BufferedFileChannelInputStreamTest.java
index 1e1f6f46d..f04b7deec 100644
--- a/src/test/java/org/apache/commons/io/input/BufferedFileChannelInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/BufferedFileChannelInputStreamTest.java
@@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.channels.FileChannel;
 import java.nio.file.StandardOpenOption;
 
 import org.junit.jupiter.api.BeforeEach;
@@ -47,6 +48,7 @@ public class BufferedFileChannelInputStreamTest extends AbstractInputStreamTest
             BufferedFileChannelInputStream.builder().setPath(inputFile).setBufferSize(123).get(), // small, unaligned buffer size
             BufferedFileChannelInputStream.builder().setURI(inputFile.toUri()).setBufferSize(1024).get(), // URI and buffer size
             BufferedFileChannelInputStream.builder().setPath(inputFile).setOpenOptions(StandardOpenOption.READ).get(), // open options
+            BufferedFileChannelInputStream.builder().setFileChannel(FileChannel.open(inputFile)).get(), // FileChannel
         };
         //@formatter:on
     }
diff --git a/src/test/java/org/apache/commons/io/output/RandomAccessFileOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/RandomAccessFileOutputStreamTest.java
new file mode 100644
index 000000000..0a1dadc2f
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/output/RandomAccessFileOutputStreamTest.java
@@ -0,0 +1,176 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.io.output;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+/**
+ * Tests {@link RandomAccessFileOutputStream}.
+ */
+public class RandomAccessFileOutputStreamTest {
+
+    private static final String EXPECTED = "Put the message in the box";
+
+    /** A temporary folder. */
+    @TempDir
+    public Path temporaryFolder;
+
+    @Test
+    public void testClose() throws IOException {
+        final Path fixturePath = temporaryFolder.resolve("testWriteInt.txt");
+        final Charset charset = StandardCharsets.US_ASCII;
+        // @formatter:off
+        try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder()
+                .setPath(fixturePath)
+                .setOpenOptions(StandardOpenOption.WRITE)
+                .get()) {
+            os.write(EXPECTED.getBytes(charset));
+            os.close();
+        }
+        assertEquals(EXPECTED, new String(Files.readAllBytes(fixturePath), charset));
+        // @formatter:on
+        try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) {
+        }
+    }
+
+    @Test
+    public void testFlush() throws IOException {
+        final Path fixturePath = temporaryFolder.resolve("testWriteInt.txt");
+        final Charset charset = StandardCharsets.US_ASCII;
+        // @formatter:off
+        try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder()
+                .setPath(fixturePath)
+                .setOpenOptions(StandardOpenOption.WRITE)
+                .get()) {
+            final byte[] bytes = EXPECTED.getBytes(charset);
+            for (final byte element : bytes) {
+                os.write(element);
+                os.flush();
+            }
+        }
+        assertEquals(EXPECTED, new String(Files.readAllBytes(fixturePath), charset));
+        // @formatter:on
+        try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) {
+        }
+    }
+
+    @Test
+    public void testWriteByteArray() throws IOException {
+        final Path fixturePath = temporaryFolder.resolve("testWriteInt.txt");
+        final Charset charset = StandardCharsets.US_ASCII;
+        // @formatter:off
+        try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder()
+                .setPath(fixturePath)
+                .setOpenOptions(StandardOpenOption.WRITE)
+                .get()) {
+            os.write(EXPECTED.getBytes(charset));
+        }
+        assertEquals(EXPECTED, new String(Files.readAllBytes(fixturePath), charset));
+        // @formatter:on
+        try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) {
+        }
+    }
+
+    @Test
+    public void testWriteByteArrayAt() throws IOException {
+        final Path fixturePath = temporaryFolder.resolve("testWriteInt.txt");
+        final Charset charset = StandardCharsets.US_ASCII;
+        // @formatter:off
+        try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder()
+                .setPath(fixturePath)
+                .setOpenOptions(StandardOpenOption.WRITE)
+                .get()) {
+            os.write(EXPECTED.getBytes(charset), 1, EXPECTED.length() - 2);
+        }
+        assertEquals(EXPECTED.subSequence(1, EXPECTED.length() - 1), new String(Files.readAllBytes(fixturePath), charset));
+        // @formatter:on
+        try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) {
+        }
+    }
+
+    @Test
+    public void testWriteGet() throws IOException {
+        final Path fixturePath = temporaryFolder.resolve("testWriteGet.txt");
+        // @formatter:off
+        try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder()
+                .setPath(fixturePath)
+                .setOpenOptions(StandardOpenOption.WRITE)
+                .get()) {
+            // doNothing
+        }
+        // @formatter:on
+    }
+
+    /**
+     * Tests that the default OpenOption is WRITE.
+     *
+     * @throws IOException Thrown when the test fails.
+     */
+    @Test
+    public void testWriteGetPathOnly() throws IOException {
+        final Path fixturePath = temporaryFolder.resolve("testWriteGet.txt");
+        // @formatter:off
+        try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder()
+                .setPath(fixturePath)
+                .get()) {
+            // doNothing
+        }
+        // @formatter:on
+    }
+
+    @Test
+    public void testWriteGetDefault() throws IOException {
+        assertThrows(IllegalStateException.class, () -> {
+            try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().get()) {
+                // doNothing
+            }
+        });
+    }
+
+    @Test
+    public void testWriteInt() throws IOException {
+        final Path fixturePath = temporaryFolder.resolve("testWriteInt.txt");
+        final Charset charset = StandardCharsets.US_ASCII;
+        // @formatter:off
+        try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder()
+                .setPath(fixturePath)
+                .setOpenOptions(StandardOpenOption.WRITE)
+                .get()) {
+            final byte[] bytes = EXPECTED.getBytes(charset);
+            for (final byte element : bytes) {
+                os.write(element);
+            }
+        }
+        assertEquals(EXPECTED, new String(Files.readAllBytes(fixturePath), charset));
+        // @formatter:on
+        try (RandomAccessFileOutputStream os = RandomAccessFileOutputStream.builder().setPath(fixturePath).get()) {
+        }
+    }
+
+}