You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by pk...@apache.org on 2025/09/28 08:12:44 UTC

(commons-io) 01/01: Add `Closeable` to `AbstractOrigin`

This is an automated email from the ASF dual-hosted git repository.

pkarwasz pushed a commit to branch feat/abstract-origin-closeable
in repository https://gitbox.apache.org/repos/asf/commons-io.git

commit 33c70b3ceace3a1e42f1e8bf581254097d1e1ac9
Author: Piotr P. Karwasz <pk...@apache.org>
AuthorDate: Sun Sep 28 10:08:13 2025 +0200

    Add `Closeable` to `AbstractOrigin`
    
    This change makes `AbstractOrigin` implement `Closeable` and updates the Javadoc of all “getter” methods that return `Closeable` resources. The Javadoc now explicitly warns that:
    
    * Closing the returned resource will also close the underlying `AbstractOrigin`.
    * Ownership of the underlying resource is transferred to the caller.
    
    ### Rationale
    
    * Callers must always close the returned resource. Some implementations create a new resource on each call, which must be released explicitly.
    * Other implementations return the underlying origin directly, making it impossible to guarantee a close-shielded wrapper.
    * The primary usage of `AbstractOrigin` is within `AbstractStreamBuilder`, whose implementations typically wrap closeable resources.
---
 .../apache/commons/io/build/AbstractOrigin.java    |  76 ++++++++--
 .../commons/io/build/AbstractStreamBuilder.java    |  70 ++++++++-
 .../commons/io/build/AbstractOriginTest.java       | 161 ++++++++++-----------
 .../build/AbstractRandomAccessFileOriginTest.java  |  14 +-
 .../apache/commons/io/build/ChannelOriginTest.java |  20 +--
 .../commons/io/build/CharSequenceOriginTest.java   |   4 +-
 .../apache/commons/io/build/FileOriginTest.java    |  15 +-
 .../io/build/IORandomAccessFileOriginTest.java     |  41 ++++++
 .../commons/io/build/InputStreamOriginTest.java    |  26 ++++
 .../commons/io/build/OutputStreamOriginTest.java   |  20 +++
 .../apache/commons/io/build/PathOriginTest.java    |  14 +-
 .../io/build/RandomAccessFileOriginTest.java       |  39 +++++
 .../apache/commons/io/build/ReaderOriginTest.java  |  26 ++++
 .../org/apache/commons/io/build/URIOriginTest.java |  20 +--
 .../commons/io/build/WriterStreamOriginTest.java   |  20 +++
 15 files changed, 404 insertions(+), 162 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 0668c3c5a..94a63078f 100644
--- a/src/main/java/org/apache/commons/io/build/AbstractOrigin.java
+++ b/src/main/java/org/apache/commons/io/build/AbstractOrigin.java
@@ -18,6 +18,7 @@
 package org.apache.commons.io.build;
 
 import java.io.ByteArrayInputStream;
+import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -324,7 +325,7 @@
  * @param <B> the concrete builder subclass type.
  * @since 2.12.0
  */
-public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends AbstractSupplier<T, B> {
+public abstract class AbstractOrigin<T, B extends AbstractOrigin<T, B>> extends AbstractSupplier<T, B> implements Closeable {
 
     /**
      * A {@link RandomAccessFile} origin.
@@ -1048,6 +1049,18 @@ protected AbstractOrigin(final T origin) {
         this.origin = Objects.requireNonNull(origin, "origin");
     }
 
+    /**
+     * @inheritDoc
+     *
+     * @since 2.21.0
+     */
+    @Override
+    public void close() throws IOException {
+        if (origin instanceof Closeable) {
+            ((Closeable) origin).close();
+        }
+    }
+
     /**
      * Gets the origin.
      *
@@ -1091,7 +1104,12 @@ public byte[] getByteArray(final long position, final int length) throws IOExcep
     }
 
     /**
-     * Gets this origin as a Channel of the given type, if possible.
+     * Gets this origin as a {@link Channel} of the given type, if supported.
+     *
+     * <p>Calling this method transfers ownership of the underlying resource to the caller.
+     * The caller is responsible for closing the returned stream once it is no longer needed.
+     * If the origin itself implements {@link Closeable}, closing the returned stream will
+     * also close the underlying origin resource.</p>
      *
      * @param channelType The type of channel to return.
      * @param options Options specifying how a file-based origin is opened, ignored otherwise.
@@ -1107,11 +1125,26 @@ public final <C extends Channel> C getChannel(final Class<C> channelType, final
         if (channelType.isInstance(channel)) {
             return channelType.cast(channel);
         }
-        throw unsupportedChannelType(channelType);
+        final UnsupportedOperationException e = unsupportedChannelType(channelType);
+        // Ensure the underlying resource is closed to prevent leaks if getChannel() created a new resource.
+        try {
+            channel.close();
+        } catch (IOException ex) {
+            e.addSuppressed(ex);
+        }
+        throw e;
     }
 
     /**
-     * Gets this origin as a Channel, if possible.
+     * Gets this origin as a {@link Channel} of the given type, if supported.
+     *
+     * <p>Subclasses overriding this method must adhere to the following requirements:</p>
+     * <ul>
+     *   <li>If the origin is file-based, the {@code options} parameter determines how the file is opened.
+     *       Otherwise, the parameter should be ignored.</li>
+     *   <li>If the origin itself implements {@link Closeable}, closing the returned channel must also
+     *       close the underlying origin resource.</li>
+     * </ul>
      *
      * @param options Options specifying how a file-based origin is opened, ignored otherwise.
      * @return A new Channel on the origin.
@@ -1146,7 +1179,12 @@ public File getFile() {
     }
 
     /**
-     * Gets this origin as an InputStream, if possible.
+     * Gets this origin as an {@link InputStream}, if supported.
+     *
+     * <p>Calling this method transfers ownership of the underlying resource to the caller.
+     * The caller is responsible for closing the returned stream once it is no longer needed.
+     * If the origin itself implements {@link Closeable}, closing the returned stream will
+     * also close the underlying origin resource.</p>
      *
      * @param options options specifying how the file is opened
      * @return this origin as an InputStream, if possible.
@@ -1158,7 +1196,12 @@ public InputStream getInputStream(final OpenOption... options) throws IOExceptio
     }
 
     /**
-     * Gets this origin as an OutputStream, if possible.
+     * Gets this origin as an {@link OutputStream}, if supported.
+     *
+     * <p>Calling this method transfers ownership of the underlying resource to the caller.
+     * The caller is responsible for closing the returned stream once it is no longer needed.
+     * If the origin itself implements {@link Closeable}, closing the returned stream will
+     * also close the underlying origin resource.</p>
      *
      * @param options options specifying how the file is opened
      * @return this origin as an OutputStream, if possible.
@@ -1180,7 +1223,12 @@ public Path getPath() {
     }
 
     /**
-     * Gets this origin as a RandomAccessFile, if possible.
+     * Gets this origin as an {@link RandomAccessFile}, if supported.
+     *
+     * <p>Calling this method transfers ownership of the underlying resource to the caller.
+     * The caller is responsible for closing the returned stream once it is no longer needed.
+     * If the origin itself implements {@link Closeable}, closing the returned stream will
+     * also close the underlying origin resource.</p>
      *
      * @param openOption options like {@link StandardOpenOption}.
      * @return this origin as a RandomAccessFile, if possible.
@@ -1193,7 +1241,12 @@ public RandomAccessFile getRandomAccessFile(final OpenOption... openOption) thro
     }
 
     /**
-     * Gets a new Reader on the origin, buffered by default.
+     * Gets this origin as a {@link Reader}, if supported.
+     *
+     * <p>Calling this method transfers ownership of the underlying resource to the caller.
+     * The caller is responsible for closing the returned stream once it is no longer needed.
+     * If the origin itself implements {@link Closeable}, closing the returned stream will
+     * also close the underlying origin resource.</p>
      *
      * @param charset the charset to use for decoding, null maps to the default Charset.
      * @return a new Reader on the origin.
@@ -1208,7 +1261,12 @@ private String getSimpleClassName() {
     }
 
     /**
-     * Gets a new Writer on the origin, buffered by default.
+     * Gets this origin as a {@link Writer}, if supported.
+     *
+     * <p>Calling this method transfers ownership of the underlying resource to the caller.
+     * The caller is responsible for closing the returned stream once it is no longer needed.
+     * If the origin itself implements {@link Closeable}, closing the returned stream will
+     * also close the underlying origin resource.</p>
      *
      * @param charset the charset to use for encoding
      * @param options options specifying how the file is opened
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 5b366627e..2a277631a 100644
--- a/src/main/java/org/apache/commons/io/build/AbstractStreamBuilder.java
+++ b/src/main/java/org/apache/commons/io/build/AbstractStreamBuilder.java
@@ -17,6 +17,7 @@
 
 package org.apache.commons.io.build;
 
+import java.io.Closeable;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -121,7 +122,15 @@ public int getBufferSizeDefault() {
     }
 
     /**
-     * Gets a Channel from the origin with OpenOption[].
+     * Gets the origin as a {@link Channel} of the given type, if supported.
+     *
+     * <p>If the origin is not already open, it will be opened using the options provided by
+     * {@link #getOpenOptions()}.</p>
+     *
+     * <p>Calling this method transfers ownership of the underlying resource to the caller.
+     * The caller is responsible for closing the returned stream once it is no longer needed.
+     * If the origin itself implements {@link Closeable}, closing the returned stream will
+     * also close the underlying origin resource.</p>
      *
      * @param channelType The channel type, not null.
      * @return A channel of the specified type.
@@ -133,6 +142,7 @@ public int getBufferSizeDefault() {
      * @see #getOpenOptions()
      * @since 2.21.0
      */
+    @SuppressWarnings("resource")
     public <C extends Channel> C getChannel(final Class<C> channelType) throws IOException {
         return checkOrigin().getChannel(channelType, getOpenOptions());
     }
@@ -147,6 +157,7 @@ public <C extends Channel> C getChannel(final Class<C> channelType) throws IOExc
      * @see AbstractOrigin#getCharSequence(Charset)
      * @since 2.13.0
      */
+    @SuppressWarnings("resource")
     public CharSequence getCharSequence() throws IOException {
         return checkOrigin().getCharSequence(getCharset());
     }
@@ -178,12 +189,21 @@ public Charset getCharsetDefault() {
      * @see AbstractOrigin#getPath()
      * @since 2.18.0
      */
+    @SuppressWarnings("resource")
     public File getFile() {
         return checkOrigin().getFile();
     }
 
     /**
-     * Gets an InputStream from the origin with OpenOption[].
+     * Gets the origin as an {@link InputStream}, if supported.
+     *
+     * <p>If the origin is not already open, it will be opened using the options provided by
+     * {@link #getOpenOptions()}.</p>
+     *
+     * <p>Calling this method transfers ownership of the underlying resource to the caller.
+     * The caller is responsible for closing the returned stream once it is no longer needed.
+     * If the origin itself implements {@link Closeable}, closing the returned stream will
+     * also close the underlying origin resource.</p>
      *
      * @return An input stream
      * @throws IllegalStateException         if the {@code origin} is {@code null}.
@@ -193,6 +213,7 @@ public File getFile() {
      * @see #getOpenOptions()
      * @since 2.13.0
      */
+    @SuppressWarnings("resource")
     public InputStream getInputStream() throws IOException {
         return checkOrigin().getInputStream(getOpenOptions());
     }
@@ -207,7 +228,15 @@ public OpenOption[] getOpenOptions() {
     }
 
     /**
-     * Gets an OutputStream from the origin with OpenOption[].
+     * Gets the origin as an {@link OutputStream}, if supported.
+     *
+     * <p>If the origin is not already open, it will be opened using the options provided by
+     * {@link #getOpenOptions()}.</p>
+     *
+     * <p>Calling this method transfers ownership of the underlying resource to the caller.
+     * The caller is responsible for closing the returned stream once it is no longer needed.
+     * If the origin itself implements {@link Closeable}, closing the returned stream will
+     * also close the underlying origin resource.</p>
      *
      * @return An OutputStream
      * @throws IllegalStateException         if the {@code origin} is {@code null}.
@@ -217,6 +246,7 @@ public OpenOption[] getOpenOptions() {
      * @see #getOpenOptions()
      * @since 2.13.0
      */
+    @SuppressWarnings("resource")
     public OutputStream getOutputStream() throws IOException {
         return checkOrigin().getOutputStream(getOpenOptions());
     }
@@ -230,12 +260,21 @@ public OutputStream getOutputStream() throws IOException {
      * @see AbstractOrigin#getPath()
      * @since 2.13.0
      */
+    @SuppressWarnings("resource")
     public Path getPath() {
         return checkOrigin().getPath();
     }
 
     /**
-     * Gets a RandomAccessFile from the origin.
+     * Gets the origin as an {@link OutputStream}, if supported.
+     *
+     * <p>If the origin is not already open, it will be opened using the options provided by
+     * {@link #getOpenOptions()}.</p>
+     *
+     * <p>Calling this method transfers ownership of the underlying resource to the caller.
+     * The caller is responsible for closing the returned stream once it is no longer needed.
+     * If the origin itself implements {@link Closeable}, closing the returned stream will
+     * also close the underlying origin resource.</p>
      *
      * @return A RandomAccessFile
      * @throws IllegalStateException         if the {@code origin} is {@code null}.
@@ -243,12 +282,21 @@ public Path getPath() {
      * @throws IOException                   if an I/O error occurs.
      * @since 2.18.0
      */
+    @SuppressWarnings("resource")
     public RandomAccessFile getRandomAccessFile() throws IOException {
         return checkOrigin().getRandomAccessFile(getOpenOptions());
     }
 
     /**
-     * Gets a Reader from the origin with a Charset.
+     * Gets the origin as an {@link OutputStream}, if supported.
+     *
+     * <p>If the origin is not already open, it will be opened using the options provided by
+     * {@link #getOpenOptions()}.</p>
+     *
+     * <p>Calling this method transfers ownership of the underlying resource to the caller.
+     * The caller is responsible for closing the returned stream once it is no longer needed.
+     * If the origin itself implements {@link Closeable}, closing the returned stream will
+     * also close the underlying origin resource.</p>
      *
      * @return A Reader
      * @throws IllegalStateException         if the {@code origin} is {@code null}.
@@ -258,12 +306,21 @@ public RandomAccessFile getRandomAccessFile() throws IOException {
      * @see #getCharset()
      * @since 2.16.0
      */
+    @SuppressWarnings("resource")
     public Reader getReader() throws IOException {
         return checkOrigin().getReader(getCharset());
     }
 
     /**
-     * Gets a Writer from the origin with an OpenOption[].
+     * Gets the origin as an {@link OutputStream}, if supported.
+     *
+     * <p>If the origin is not already open, it will be opened using the options provided by
+     * {@link #getOpenOptions()}.</p>
+     *
+     * <p>Calling this method transfers ownership of the underlying resource to the caller.
+     * The caller is responsible for closing the returned stream once it is no longer needed.
+     * If the origin itself implements {@link Closeable}, closing the returned stream will
+     * also close the underlying origin resource.</p>
      *
      * @return An writer.
      * @throws IllegalStateException         if the {@code origin} is {@code null}.
@@ -273,6 +330,7 @@ public Reader getReader() throws IOException {
      * @see #getOpenOptions()
      * @since 2.13.0
      */
+    @SuppressWarnings("resource")
     public Writer getWriter() throws IOException {
         return checkOrigin().getWriter(getCharset(), getOpenOptions());
     }
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 d40cbf05d..33480d14f 100644
--- a/src/test/java/org/apache/commons/io/build/AbstractOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/AbstractOriginTest.java
@@ -23,7 +23,6 @@
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
-import java.io.Closeable;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -46,8 +45,6 @@
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.build.AbstractOrigin.RandomAccessFileOrigin;
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.io.TempDir;
 import org.junit.jupiter.params.ParameterizedTest;
@@ -66,17 +63,16 @@ public abstract class AbstractOriginTest<T, B extends AbstractOrigin<T, B>> {
     protected static final String FILE_NAME_RW = AbstractOriginTest.class.getSimpleName() + ".txt";
     private static final int RO_LENGTH = 20;
 
-    protected AbstractOrigin<T, B> originRo;
-    protected AbstractOrigin<T, B> originRw;
-
     @TempDir
     protected Path tempPath;
 
-    @BeforeEach
-    void beforeEach() throws IOException {
-        setOriginRo(newOriginRo());
-        resetOriginRw();
-        setOriginRw(newOriginRw());
+    /**
+     * Asserts that the origin is open.
+     *
+     * @param origin The origin to test.
+     */
+    protected void assertOpen(final AbstractOrigin<T, B> origin) {
+        // No-op
     }
 
     private void checkRead(final ReadableByteChannel channel) throws IOException {
@@ -96,18 +92,6 @@ private void checkWrite(final WritableByteChannel channel) throws IOException {
         assertEquals(RO_LENGTH, written);
     }
 
-    @AfterEach
-    void cleanup() {
-        final T originRo = getOriginRo().get();
-        if (originRo instanceof Closeable) {
-            IOUtils.closeQuietly((Closeable) originRo);
-        }
-        final T originRw = getOriginRw().get();
-        if (originRw instanceof Closeable) {
-            IOUtils.closeQuietly((Closeable) originRw);
-        }
-    }
-
     byte[] getFixtureByteArray() throws IOException {
         return IOUtils.resourceToByteArray(FILE_RES_RO);
     }
@@ -116,14 +100,6 @@ String getFixtureString() throws IOException {
         return IOUtils.resourceToString(FILE_RES_RO, StandardCharsets.UTF_8);
     }
 
-    protected AbstractOrigin<T, B> getOriginRo() {
-        return Objects.requireNonNull(originRo, "originRo");
-    }
-
-    protected AbstractOrigin<T, B> getOriginRw() {
-        return Objects.requireNonNull(originRw, "originRw");
-    }
-
     @SuppressWarnings("resource")
     private boolean isValid(final RandomAccessFile raf) throws IOException {
         return Objects.requireNonNull(raf).getFD().valid();
@@ -133,50 +109,56 @@ private boolean isValid(final RandomAccessFile raf) throws IOException {
 
     protected abstract B newOriginRw() throws IOException;
 
-    protected void resetOriginRw() throws IOException {
-        // No-op
-    }
-
-    protected void setOriginRo(final AbstractOrigin<T, B> origin) {
-        this.originRo = origin;
-    }
-
-    protected void setOriginRw(final AbstractOrigin<T, B> origin) {
-        this.originRw = origin;
-    }
-
     @Test
     void testGetByteArray() throws IOException {
-        assertArrayEquals(getFixtureByteArray(), getOriginRo().getByteArray());
+        try (AbstractOrigin<T, B> originRo = newOriginRo()) {
+            assertArrayEquals(getFixtureByteArray(), originRo.getByteArray());
+            assertOpen(originRo);
+        }
     }
 
     @Test
     void testGetByteArrayAt_0_0() throws IOException {
-        assertArrayEquals(new byte[] {}, getOriginRo().getByteArray(0, 0));
+        try (AbstractOrigin<T, B> originRo = newOriginRo()) {
+            assertArrayEquals(new byte[] {}, originRo.getByteArray(0, 0));
+            assertOpen(originRo);
+        }
     }
 
     @Test
     void testGetByteArrayAt_0_1() throws IOException {
-        assertArrayEquals(new byte[] { '1' }, getOriginRo().getByteArray(0, 1));
+        try (AbstractOrigin<T, B> originRo = newOriginRo()) {
+            assertArrayEquals(new byte[] { '1' }, originRo.getByteArray(0, 1));
+            assertOpen(originRo);
+        }
     }
 
     @Test
     void testGetByteArrayAt_1_1() throws IOException {
-        assertArrayEquals(new byte[] { '2' }, getOriginRo().getByteArray(1, 1));
+        try (AbstractOrigin<T, B> originRo = newOriginRo()) {
+            assertArrayEquals(new byte[] { '2' }, originRo.getByteArray(1, 1));
+            assertOpen(originRo);
+        }
     }
 
     @Test
     void testGetCharSequence() throws IOException {
-        final CharSequence charSequence = getOriginRo().getCharSequence(StandardCharsets.UTF_8);
-        assertNotNull(charSequence);
-        assertEquals(getFixtureString(), charSequence.toString());
+        try (AbstractOrigin<T, B> originRo = newOriginRo()) {
+            final CharSequence charSequence = originRo.getCharSequence(StandardCharsets.UTF_8);
+            assertNotNull(charSequence);
+            assertEquals(getFixtureString(), charSequence.toString());
+            assertOpen(originRo);
+        }
     }
 
     @Test
     void testGetFile() throws IOException {
-        testGetFile(getOriginRo().getFile(), RO_LENGTH);
-        FileUtils.touch(getOriginRw().getFile());
-        testGetFile(getOriginRw().getFile(), 0);
+        try (AbstractOrigin<T, B> originRo = newOriginRo();
+             AbstractOrigin<T, B> originRw = newOriginRw()) {
+            testGetFile(originRo.getFile(), RO_LENGTH);
+            FileUtils.touch(originRw.getFile());
+            testGetFile(originRw.getFile(), 0);
+        }
     }
 
     private void testGetFile(final File file, final long expectedLen) throws IOException {
@@ -188,7 +170,7 @@ private void testGetFile(final File file, final long expectedLen) throws IOExcep
 
     @Test
     void testGetInputStream() throws IOException {
-        try (InputStream inputStream = getOriginRo().getInputStream()) {
+        try (InputStream inputStream = newOriginRo().getInputStream()) {
             assertNotNull(inputStream);
             assertArrayEquals(getFixtureByteArray(), IOUtils.toByteArray(inputStream));
         }
@@ -196,16 +178,19 @@ void testGetInputStream() throws IOException {
 
     @Test
     void testGetOutputStream() throws IOException {
-        try (OutputStream output = getOriginRw().getOutputStream()) {
+        try (OutputStream output = newOriginRw().getOutputStream()) {
             assertNotNull(output);
         }
     }
 
     @Test
     void testGetPath() throws IOException {
-        testGetPath(getOriginRo().getPath(), RO_LENGTH);
-        FileUtils.touch(getOriginRw().getPath().toFile());
-        testGetPath(getOriginRw().getPath(), 0);
+        try (AbstractOrigin<T, B> originRo = newOriginRo();
+             AbstractOrigin<T, B> originRw = newOriginRw()) {
+            testGetPath(originRo.getPath(), RO_LENGTH);
+            FileUtils.touch(originRw.getPath().toFile());
+            testGetPath(originRw.getPath(), 0);
+        }
     }
 
     private void testGetPath(final Path path, final long expectedLen) throws IOException {
@@ -217,35 +202,37 @@ private void testGetPath(final Path path, final long expectedLen) throws IOExcep
 
     @Test
     void testGetRandomAccessFile() throws IOException {
+        final AbstractOrigin<T, B> originRo = newOriginRo();
+        final AbstractOrigin<T, B> originRw = newOriginRw();
         // Default
-        try (RandomAccessFile raf = getOriginRo().getRandomAccessFile()) {
+        try (RandomAccessFile raf = originRo.getRandomAccessFile()) {
             assertNotNull(raf);
             assertTrue(isValid(raf));
         }
-        final boolean isRafOriginRo = getOriginRo() instanceof RandomAccessFileOrigin;
-        final boolean isRafOriginRw = getOriginRw() instanceof RandomAccessFileOrigin;
+        final boolean isRafOriginRo = originRo instanceof RandomAccessFileOrigin;
+        final boolean isRafOriginRw = originRw instanceof RandomAccessFileOrigin;
         // Same as above, but underlying resource is now closed.
-        try (RandomAccessFile raf = getOriginRo().getRandomAccessFile()) {
+        try (RandomAccessFile raf = originRo.getRandomAccessFile()) {
             assertNotNull(raf);
             assertFalse(isRafOriginRo && isValid(raf));
         }
         // Read
-        try (RandomAccessFile raf = getOriginRo().getRandomAccessFile(StandardOpenOption.READ)) {
+        try (RandomAccessFile raf = originRo.getRandomAccessFile(StandardOpenOption.READ)) {
             assertNotNull(raf);
             assertFalse(isRafOriginRo && isValid(raf));
         }
         // Write, first access
-        try (RandomAccessFile raf = getOriginRw().getRandomAccessFile(StandardOpenOption.WRITE)) {
+        try (RandomAccessFile raf = originRw.getRandomAccessFile(StandardOpenOption.WRITE)) {
             assertNotNull(raf);
-            if (isRafOriginRw || getOriginRw().getFile() != null) {
-                assertTrue(isValid(raf), () -> getOriginRw().toString());
+            if (isRafOriginRw || originRw.getFile() != null) {
+                assertTrue(isValid(raf), () -> originRw.toString());
             } else {
                 // Can't get there from here.
-                assertFalse(isValid(raf), () -> getOriginRw().toString());
+                assertFalse(isValid(raf), () -> originRw.toString());
             }
         }
         // Read, Write, underlying resource is now closed.
-        try (RandomAccessFile raf = getOriginRw().getRandomAccessFile(StandardOpenOption.READ, StandardOpenOption.WRITE)) {
+        try (RandomAccessFile raf = originRw.getRandomAccessFile(StandardOpenOption.READ, StandardOpenOption.WRITE)) {
             assertNotNull(raf);
             assertFalse(isRafOriginRw && isValid(raf));
         }
@@ -254,22 +241,23 @@ void testGetRandomAccessFile() throws IOException {
     @ParameterizedTest
     @EnumSource(StandardOpenOption.class)
     void testGetRandomAccessFile(final OpenOption openOption) throws IOException {
+        final AbstractOrigin<T, B> originRw = newOriginRw();
         // Default
-        try (RandomAccessFile raf = getOriginRw().getRandomAccessFile()) {
+        try (RandomAccessFile raf = originRw.getRandomAccessFile()) {
             assertNotNull(raf);
             assertTrue(isValid(raf));
         }
         // Same as above, but underlying resource is now closed.
-        final boolean isRafOrigin = getOriginRw() instanceof RandomAccessFileOrigin;
-        try (RandomAccessFile raf = getOriginRw().getRandomAccessFile()) {
+        final boolean isRafOrigin = originRw instanceof RandomAccessFileOrigin;
+        try (RandomAccessFile raf = originRw.getRandomAccessFile()) {
             assertNotNull(raf);
             assertFalse(isRafOrigin && isValid(raf));
         }
-        try (RandomAccessFile raf = getOriginRw().getRandomAccessFile(openOption)) {
+        try (RandomAccessFile raf = originRw.getRandomAccessFile(openOption)) {
             assertNotNull(raf);
             assertFalse(isRafOrigin && isValid(raf));
         }
-        try (RandomAccessFile raf = getOriginRw().getRandomAccessFile(openOption)) {
+        try (RandomAccessFile raf = originRw.getRandomAccessFile(openOption)) {
             assertNotNull(raf);
             assertFalse(isRafOrigin && isValid(raf));
         }
@@ -277,7 +265,7 @@ void testGetRandomAccessFile(final OpenOption openOption) throws IOException {
 
     @Test
     void testGetReadableByteChannel() throws IOException {
-        try (ReadableByteChannel channel = getOriginRo().getChannel(ReadableByteChannel.class, StandardOpenOption.READ)) {
+        try (ReadableByteChannel channel = newOriginRo().getChannel(ReadableByteChannel.class, StandardOpenOption.READ)) {
             final SeekableByteChannel seekable = channel instanceof SeekableByteChannel ? (SeekableByteChannel) channel : null;
             assertNotNull(channel);
             assertTrue(channel.isOpen());
@@ -294,15 +282,13 @@ void testGetReadableByteChannel() throws IOException {
 
     @Test
     void testGetReader() throws IOException {
-        try (Reader reader = getOriginRo().getReader(Charset.defaultCharset())) {
+        try (Reader reader = newOriginRo().getReader(Charset.defaultCharset())) {
             assertNotNull(reader);
         }
-        setOriginRo(newOriginRo());
-        try (Reader reader = getOriginRo().getReader(null)) {
+        try (Reader reader = newOriginRo().getReader(null)) {
             assertNotNull(reader);
         }
-        setOriginRo(newOriginRo());
-        try (Reader reader = getOriginRo().getReader(StandardCharsets.UTF_8)) {
+        try (Reader reader = newOriginRo().getReader(StandardCharsets.UTF_8)) {
             assertNotNull(reader);
             assertEquals(getFixtureString(), IOUtils.toString(reader));
         }
@@ -311,7 +297,7 @@ void testGetReader() throws IOException {
     @Test
     void testGetWritableByteChannel() throws IOException {
         final boolean supportsRead;
-        try (WritableByteChannel channel = getOriginRw().getChannel(WritableByteChannel.class, StandardOpenOption.WRITE)) {
+        try (WritableByteChannel channel = newOriginRw().getChannel(WritableByteChannel.class, StandardOpenOption.WRITE)) {
             supportsRead = channel instanceof ReadableByteChannel;
             final SeekableByteChannel seekable = channel instanceof SeekableByteChannel ? (SeekableByteChannel) channel : null;
             assertNotNull(channel);
@@ -327,15 +313,13 @@ void testGetWritableByteChannel() throws IOException {
             }
         }
         if (supportsRead) {
-            setOriginRw(newOriginRw());
-            try (ReadableByteChannel channel = getOriginRw().getChannel(ReadableByteChannel.class, StandardOpenOption.READ)) {
+            try (ReadableByteChannel channel = newOriginRw().getChannel(ReadableByteChannel.class, StandardOpenOption.READ)) {
                 assertNotNull(channel);
                 assertTrue(channel.isOpen());
                 checkRead(channel);
             }
         }
-        setOriginRw(newOriginRw());
-        try (WritableByteChannel channel = getOriginRw().getChannel(WritableByteChannel.class, StandardOpenOption.WRITE)) {
+        try (WritableByteChannel channel = newOriginRw().getChannel(WritableByteChannel.class, StandardOpenOption.WRITE)) {
             final SeekableByteChannel seekable = channel instanceof SeekableByteChannel ? (SeekableByteChannel) channel : null;
             assertNotNull(channel);
             assertTrue(channel.isOpen());
@@ -357,17 +341,18 @@ void testGetWritableByteChannel() throws IOException {
 
     @Test
     void testGetWriter() throws IOException {
-        try (Writer writer = getOriginRw().getWriter(Charset.defaultCharset())) {
+        try (Writer writer = newOriginRw().getWriter(Charset.defaultCharset())) {
             assertNotNull(writer);
         }
-        setOriginRw(newOriginRw());
-        try (Writer writer = getOriginRw().getWriter(null)) {
+        try (Writer writer = newOriginRw().getWriter(null)) {
             assertNotNull(writer);
         }
     }
 
     @Test
     void testSize() throws IOException {
-        assertEquals(RO_LENGTH, getOriginRo().getByteArray().length);
+        try (AbstractOrigin<T, B> originRo = newOriginRo()) {
+            assertEquals(RO_LENGTH, originRo.getByteArray().length);
+        }
     }
 }
diff --git a/src/test/java/org/apache/commons/io/build/AbstractRandomAccessFileOriginTest.java b/src/test/java/org/apache/commons/io/build/AbstractRandomAccessFileOriginTest.java
index 28644f3c8..670a65020 100644
--- a/src/test/java/org/apache/commons/io/build/AbstractRandomAccessFileOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/AbstractRandomAccessFileOriginTest.java
@@ -17,17 +17,14 @@
 
 package org.apache.commons.io.build;
 
-import java.io.IOException;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
 import java.io.RandomAccessFile;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.StandardOpenOption;
 
 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;
-import org.apache.commons.lang3.ArrayUtils;
 
 /**
  * Tests {@link RandomAccessFileOrigin} and {@link IORandomAccessFileOrigin}.
@@ -39,11 +36,8 @@
  */
 public abstract class AbstractRandomAccessFileOriginTest<T extends RandomAccessFile, B extends AbstractRandomAccessFileOrigin<T, B>>
         extends AbstractOriginTest<T, B> {
-
     @Override
-    protected void resetOriginRw() throws IOException {
-        // Reset the file
-        final Path rwPath = tempPath.resolve(FILE_NAME_RW);
-        Files.write(rwPath, ArrayUtils.EMPTY_BYTE_ARRAY, StandardOpenOption.CREATE);
+    protected void assertOpen(AbstractOrigin<T, B> origin) {
+        assertTrue(origin.get().getChannel().isOpen(), "RandomAccessFile not open");
     }
 }
diff --git a/src/test/java/org/apache/commons/io/build/ChannelOriginTest.java b/src/test/java/org/apache/commons/io/build/ChannelOriginTest.java
index a41992868..7d41fc588 100644
--- a/src/test/java/org/apache/commons/io/build/ChannelOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/ChannelOriginTest.java
@@ -18,6 +18,7 @@
 
 import static java.nio.file.StandardOpenOption.READ;
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.Mockito.mock;
 
 import java.io.IOException;
@@ -26,7 +27,6 @@
 import java.nio.channels.WritableByteChannel;
 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.Arrays;
@@ -34,29 +34,25 @@
 import java.util.HashSet;
 
 import org.apache.commons.io.build.AbstractOrigin.ChannelOrigin;
-import org.apache.commons.lang3.ArrayUtils;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.EnumSource;
 
 class ChannelOriginTest extends AbstractOriginTest<Channel, ChannelOrigin> {
     @Override
-    protected ChannelOrigin newOriginRo() throws IOException {
-        return new ChannelOrigin(Files.newByteChannel(Paths.get(FILE_NAME_RO), Collections.singleton(READ)));
+    protected void assertOpen(AbstractOrigin<Channel, ChannelOrigin> origin) {
+        assertTrue(origin.get().isOpen(), "Channel not open");
     }
 
     @Override
-    protected ChannelOrigin newOriginRw() throws IOException {
-        return new ChannelOrigin(Files.newByteChannel(
-                tempPath.resolve(FILE_NAME_RW),
-                new HashSet<>(Arrays.asList(StandardOpenOption.READ, StandardOpenOption.WRITE))));
+    protected ChannelOrigin newOriginRo() throws IOException {
+        return new ChannelOrigin(Files.newByteChannel(Paths.get(FILE_NAME_RO), Collections.singleton(READ)));
     }
 
     @Override
-    protected void resetOriginRw() throws IOException {
-        // Reset the file
-        final Path rwPath = tempPath.resolve(FILE_NAME_RW);
-        Files.write(rwPath, ArrayUtils.EMPTY_BYTE_ARRAY, StandardOpenOption.CREATE);
+    protected ChannelOrigin newOriginRw() throws IOException {
+        return new ChannelOrigin(Files.newByteChannel(tempPath.resolve(FILE_NAME_RW), new HashSet<>(Arrays.asList(StandardOpenOption.READ,
+                StandardOpenOption.WRITE, StandardOpenOption.CREATE))));
     }
 
     @Override
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 0fc5b9c5c..5ba0165df 100644
--- a/src/test/java/org/apache/commons/io/build/CharSequenceOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/CharSequenceOriginTest.java
@@ -94,7 +94,7 @@ void testGetRandomAccessFile(final OpenOption openOption) {
     @Test
     void testGetReaderIgnoreCharset() throws IOException {
         // The CharSequenceOrigin ignores the given Charset.
-        try (Reader reader = getOriginRo().getReader(StandardCharsets.UTF_16LE)) {
+        try (Reader reader = newOriginRo().getReader(StandardCharsets.UTF_16LE)) {
             assertNotNull(reader);
             assertEquals(getFixtureStringFromFile(), IOUtils.toString(reader));
         }
@@ -103,7 +103,7 @@ void testGetReaderIgnoreCharset() throws IOException {
     @Test
     void testGetReaderIgnoreCharsetNull() throws IOException {
         // The CharSequenceOrigin ignores the given Charset.
-        try (Reader reader = getOriginRo().getReader(null)) {
+        try (Reader reader = newOriginRo().getReader(null)) {
             assertNotNull(reader);
             assertEquals(getFixtureStringFromFile(), IOUtils.toString(reader));
         }
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 548d78885..e09509acc 100644
--- a/src/test/java/org/apache/commons/io/build/FileOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/FileOriginTest.java
@@ -18,12 +18,9 @@
 
 import java.io.File;
 import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.StandardOpenOption;
 
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.build.AbstractOrigin.FileOrigin;
-import org.apache.commons.lang3.ArrayUtils;
 
 /**
  * Tests {@link FileOrigin}.
@@ -41,13 +38,9 @@ protected FileOrigin newOriginRo() {
 
     @Override
     protected FileOrigin newOriginRw() throws IOException {
-        return new FileOrigin(tempPath.resolve(FILE_NAME_RW).toFile());
+        final File file = tempPath.resolve(FILE_NAME_RW).toFile();
+        FileUtils.touch(file);
+        return new FileOrigin(file);
     }
 
-    @Override
-    protected void resetOriginRw() throws IOException {
-        // Reset the file
-        final Path rwPath = tempPath.resolve(FILE_NAME_RW);
-        Files.write(rwPath, ArrayUtils.EMPTY_BYTE_ARRAY, StandardOpenOption.CREATE);
-    }
 }
diff --git a/src/test/java/org/apache/commons/io/build/IORandomAccessFileOriginTest.java b/src/test/java/org/apache/commons/io/build/IORandomAccessFileOriginTest.java
index b51118c65..5621ed20c 100644
--- a/src/test/java/org/apache/commons/io/build/IORandomAccessFileOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/IORandomAccessFileOriginTest.java
@@ -16,13 +16,23 @@
  */
 package org.apache.commons.io.build;
 
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.RandomAccessFile;
+import java.nio.channels.FileChannel;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.WritableByteChannel;
+import java.nio.charset.StandardCharsets;
 
 import org.apache.commons.io.IORandomAccessFile;
 import org.apache.commons.io.RandomAccessFileMode;
 import org.apache.commons.io.build.AbstractOrigin.IORandomAccessFileOrigin;
+import org.junit.jupiter.api.Test;
 
 /**
  * Tests {@link IORandomAccessFileOrigin}.
@@ -42,4 +52,35 @@ protected IORandomAccessFileOrigin newOriginRo() throws FileNotFoundException {
     protected IORandomAccessFileOrigin newOriginRw() throws IOException {
         return new IORandomAccessFileOrigin(RandomAccessFileMode.READ_WRITE.io(tempPath.resolve(FILE_NAME_RW).toFile().getPath()));
     }
+
+    @Test
+    void testClosesOrigin() throws IOException {
+        final FileChannel channel = mock(FileChannel.class);
+        final IORandomAccessFile resource = mock(IORandomAccessFile.class);
+        when(resource.getChannel()).thenReturn(channel);
+        final AbstractOrigin.IORandomAccessFileOrigin origin = new AbstractOrigin.IORandomAccessFileOrigin(resource);
+
+        // These wrappers close the underlying Channel.
+        origin.getInputStream().close();
+        verify(channel, times(1)).close();
+
+        origin.getReader(StandardCharsets.UTF_8).close();
+        verify(channel, times(2)).close();
+
+        origin.getChannel(ReadableByteChannel.class).close();
+        verify(channel, times(3)).close();
+
+        origin.getChannel(WritableByteChannel.class).close();
+        verify(channel, times(4)).close();
+
+        // These wrappers close the underlying RandomAccessFile.
+        origin.getOutputStream().close();
+        verify(resource, times(1)).close();
+
+        origin.getWriter(StandardCharsets.UTF_8).close();
+        verify(resource, times(2)).close();
+
+        origin.getRandomAccessFile().close();
+        verify(resource, times(3)).close();
+    }
 }
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 7329fdb57..44577f069 100644
--- a/src/test/java/org/apache/commons/io/build/InputStreamOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/InputStreamOriginTest.java
@@ -16,12 +16,18 @@
  */
 package org.apache.commons.io.build;
 
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.OpenOption;
 import java.nio.file.StandardOpenOption;
 
@@ -39,6 +45,11 @@
  * @see InputStream
  */
 class InputStreamOriginTest extends AbstractOriginTest<InputStream, InputStreamOrigin> {
+    @Override
+    protected void assertOpen(AbstractOrigin<InputStream, InputStreamOrigin> origin) {
+        // If the FileInputStream is closed, available() throws IOException.
+        assertDoesNotThrow(() -> origin.get().available(), "InputStream not open");
+    }
 
     @SuppressWarnings("resource")
     @Override
@@ -52,6 +63,21 @@ protected InputStreamOrigin newOriginRw() {
         return new InputStreamOrigin(CharSequenceInputStream.builder().setCharSequence("World").get());
     }
 
+    @Test
+    void testClosesOrigin() throws IOException {
+        final InputStream resource = mock(InputStream.class);
+        final InputStreamOrigin origin = new InputStreamOrigin(resource);
+
+        origin.getInputStream().close();
+        verify(resource, times(1)).close();
+
+        origin.getReader(StandardCharsets.UTF_8).close();
+        verify(resource, times(2)).close();
+
+        origin.getChannel(ReadableByteChannel.class).close();
+        verify(resource, times(3)).close();
+    }
+
     @Override
     @Test
     void testGetFile() {
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 463ad83fb..d31e6c749 100644
--- a/src/test/java/org/apache/commons/io/build/OutputStreamOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/OutputStreamOriginTest.java
@@ -17,9 +17,14 @@
 package org.apache.commons.io.build;
 
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.nio.channels.WritableByteChannel;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.OpenOption;
 import java.nio.file.StandardOpenOption;
 
@@ -48,6 +53,21 @@ protected OutputStreamOrigin newOriginRw() {
         return new OutputStreamOrigin(new ByteArrayOutputStream());
     }
 
+    @Test
+    void testClosesOrigin() throws IOException {
+        final OutputStream resource = mock(OutputStream.class);
+        final OutputStreamOrigin origin = new OutputStreamOrigin(resource);
+
+        origin.getOutputStream().close();
+        verify(resource, times(1)).close();
+
+        origin.getWriter(StandardCharsets.UTF_8).close();
+        verify(resource, times(2)).close();
+
+        origin.getChannel(WritableByteChannel.class).close();
+        verify(resource, times(3)).close();
+    }
+
     @Override
     @Test
     void testGetByteArray() {
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 bc29df52b..115c79af2 100644
--- a/src/test/java/org/apache/commons/io/build/PathOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/PathOriginTest.java
@@ -17,13 +17,11 @@
 package org.apache.commons.io.build;
 
 import java.io.IOException;
-import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.nio.file.StandardOpenOption;
 
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.build.AbstractOrigin.PathOrigin;
-import org.apache.commons.lang3.ArrayUtils;
 
 /**
  * Tests {@link PathOrigin}.
@@ -41,13 +39,9 @@ protected PathOrigin newOriginRo() {
 
     @Override
     protected PathOrigin newOriginRw() throws IOException {
-        return new PathOrigin(tempPath.resolve(FILE_NAME_RW));
+        final Path path = tempPath.resolve(FILE_NAME_RW);
+        FileUtils.touch(path.toFile());
+        return new PathOrigin(path);
     }
 
-    @Override
-    protected void resetOriginRw() throws IOException {
-        // Reset the file
-        final Path rwPath = tempPath.resolve(FILE_NAME_RW);
-        Files.write(rwPath, ArrayUtils.EMPTY_BYTE_ARRAY, StandardOpenOption.CREATE);
-    }
 }
diff --git a/src/test/java/org/apache/commons/io/build/RandomAccessFileOriginTest.java b/src/test/java/org/apache/commons/io/build/RandomAccessFileOriginTest.java
index d7306c12c..df9761087 100644
--- a/src/test/java/org/apache/commons/io/build/RandomAccessFileOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/RandomAccessFileOriginTest.java
@@ -18,10 +18,18 @@
 package org.apache.commons.io.build;
 
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.RandomAccessFile;
+import java.nio.channels.FileChannel;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.WritableByteChannel;
+import java.nio.charset.StandardCharsets;
 
 import org.apache.commons.io.RandomAccessFileMode;
 import org.apache.commons.io.build.AbstractOrigin.RandomAccessFileOrigin;
@@ -46,6 +54,37 @@ protected RandomAccessFileOrigin newOriginRw() throws IOException {
         return new RandomAccessFileOrigin(RandomAccessFileMode.READ_WRITE.create(tempPath.resolve(FILE_NAME_RW)));
     }
 
+    @Test
+    void testClosesOrigin() throws IOException {
+        final FileChannel channel = mock(FileChannel.class);
+        final RandomAccessFile resource = mock(RandomAccessFile.class);
+        when(resource.getChannel()).thenReturn(channel);
+        final RandomAccessFileOrigin origin = new RandomAccessFileOrigin(resource);
+
+        // These wrappers close the underlying Channel.
+        origin.getInputStream().close();
+        verify(channel, times(1)).close();
+
+        origin.getReader(StandardCharsets.UTF_8).close();
+        verify(channel, times(2)).close();
+
+        origin.getChannel(ReadableByteChannel.class).close();
+        verify(channel, times(3)).close();
+
+        origin.getChannel(WritableByteChannel.class).close();
+        verify(channel, times(4)).close();
+
+        // These wrappers close the underlying RandomAccessFile.
+        origin.getOutputStream().close();
+        verify(resource, times(1)).close();
+
+        origin.getWriter(StandardCharsets.UTF_8).close();
+        verify(resource, times(2)).close();
+
+        origin.getRandomAccessFile().close();
+        verify(resource, times(3)).close();
+    }
+
     @Override
     @Test
     void testGetFile() {
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 3c1e3032f..6fdfd64b6 100644
--- a/src/test/java/org/apache/commons/io/build/ReaderOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/ReaderOriginTest.java
@@ -16,12 +16,18 @@
  */
 package org.apache.commons.io.build;
 
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
 import java.io.Reader;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.OpenOption;
 import java.nio.file.StandardOpenOption;
 
@@ -39,6 +45,11 @@
  * @see Reader
  */
 class ReaderOriginTest extends AbstractOriginTest<Reader, ReaderOrigin> {
+    @Override
+    protected void assertOpen(AbstractOrigin<Reader, ReaderOrigin> origin) {
+        // If the underlying FileInputStream is closed, ready() calls available() which throws IOException.
+        assertDoesNotThrow(() -> origin.get().ready(), "Reader not open");
+    }
 
     @Override
     protected ReaderOrigin newOriginRo() throws FileNotFoundException {
@@ -50,6 +61,21 @@ protected ReaderOrigin newOriginRw() {
         return new ReaderOrigin(new CharSequenceReader("World"));
     }
 
+    @Test
+    void testClosesOrigin() throws IOException {
+        final Reader resource = mock(Reader.class);
+        final ReaderOrigin origin = new ReaderOrigin(resource);
+
+        origin.getInputStream().close();
+        verify(resource, times(1)).close();
+
+        origin.getReader(StandardCharsets.UTF_8).close();
+        verify(resource, times(2)).close();
+
+        origin.getChannel(ReadableByteChannel.class).close();
+        verify(resource, times(3)).close();
+    }
+
     @Override
     @Test
     void testGetFile() {
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 6fd2043eb..8e406ab2c 100644
--- a/src/test/java/org/apache/commons/io/build/URIOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/URIOriginTest.java
@@ -21,13 +21,11 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
-import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.nio.file.StandardOpenOption;
 
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.build.AbstractOrigin.URIOrigin;
-import org.apache.commons.lang3.ArrayUtils;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.ValueSource;
@@ -47,15 +45,10 @@ protected URIOrigin newOriginRo() {
     }
 
     @Override
-    protected URIOrigin newOriginRw() {
-        return new URIOrigin(tempPath.resolve(FILE_NAME_RW).toUri());
-    }
-
-    @Override
-    protected void resetOriginRw() throws IOException {
-        // Reset the file
-        final Path rwPath = tempPath.resolve(FILE_NAME_RW);
-        Files.write(rwPath, ArrayUtils.EMPTY_BYTE_ARRAY, StandardOpenOption.CREATE);
+    protected URIOrigin newOriginRw() throws IOException {
+        final Path path = tempPath.resolve(FILE_NAME_RW);
+        FileUtils.touch(path.toFile());
+        return new URIOrigin(path.toUri());
     }
 
     @ParameterizedTest
@@ -72,8 +65,7 @@ void testGetInputStream(final String uri) throws Exception {
 
     @Test
     void testGetInputStreamFileURI() throws Exception {
-        final AbstractOrigin.URIOrigin origin = getOriginRo().asThis();
-        try (InputStream in = origin.getInputStream()) {
+        try (InputStream in = newOriginRo().getInputStream()) {
             assertNotEquals(-1, in.read());
         }
     }
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 9d1f2339b..f6e5fa134 100644
--- a/src/test/java/org/apache/commons/io/build/WriterStreamOriginTest.java
+++ b/src/test/java/org/apache/commons/io/build/WriterStreamOriginTest.java
@@ -17,10 +17,15 @@
 package org.apache.commons.io.build;
 
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 
 import java.io.IOException;
 import java.io.StringWriter;
 import java.io.Writer;
+import java.nio.channels.WritableByteChannel;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.OpenOption;
 import java.nio.file.StandardOpenOption;
 
@@ -48,6 +53,21 @@ protected WriterOrigin newOriginRw() {
         return new WriterOrigin(new StringWriter());
     }
 
+    @Test
+    void testClosesOrigin() throws IOException {
+        final Writer resource = mock(Writer.class);
+        final WriterOrigin origin = new WriterOrigin(resource);
+
+        origin.getOutputStream().close();
+        verify(resource, times(1)).close();
+
+        origin.getWriter(StandardCharsets.UTF_8).close();
+        verify(resource, times(2)).close();
+
+        origin.getChannel(WritableByteChannel.class).close();
+        verify(resource, times(3)).close();
+    }
+
     @Override
     @Test
     void testGetByteArray() {