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

commit 864fb60e4128345b81a443a5bb6c3f972f0e1e9e
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Sun Jul 11 10:28:24 2021 -0400

    Add UncheckedFilterReader.
---
 src/changes/changes.xml                            |   3 +
 .../commons/io/input/UncheckedFilterReader.java    | 165 ++++++++++++++++++
 .../io/input/UncheckedFilterReaderTest.java        | 189 +++++++++++++++++++++
 3 files changed, 357 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index aa80316..383adef 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -57,6 +57,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add UncheckedBufferedReader.
       </action>
+      <action dev="ggregory" type="add" due-to="Gary Gregory">
+        Add UncheckedFilterReader.
+      </action>
       <!-- UPDATE -->
       <action dev="ggregory" type="update" due-to="Dependabot">
         Bump Maven Javadoc plugin from 3.2.0 to 3.3.0.
diff --git 
a/src/main/java/org/apache/commons/io/input/UncheckedFilterReader.java 
b/src/main/java/org/apache/commons/io/input/UncheckedFilterReader.java
new file mode 100644
index 0000000..a3a7acd
--- /dev/null
+++ b/src/main/java/org/apache/commons/io/input/UncheckedFilterReader.java
@@ -0,0 +1,165 @@
+/*
+ * 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.input;
+
+import java.io.FilterReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.UncheckedIOException;
+import java.nio.CharBuffer;
+
+/**
+ * A {@link FilterReader} that throws {@link UncheckedIOException} instead of 
{@link IOException}.
+ *
+ * @see FilterReader
+ * @see IOException
+ * @see UncheckedIOException
+ * @since 2.12.0
+ */
+public class UncheckedFilterReader extends FilterReader {
+
+    /**
+     * Creates a new filtered reader.
+     *
+     * @param reader a Reader object providing the underlying stream.
+     * @return a new UncheckedFilterReader.
+     * @throws NullPointerException if {@code reader} is {@code null}.
+     */
+    public static UncheckedFilterReader on(final Reader reader) {
+        return new UncheckedFilterReader(reader);
+    }
+
+    /**
+     * Creates a new filtered reader.
+     *
+     * @param reader a Reader object providing the underlying stream.
+     * @throws NullPointerException if {@code reader} is {@code null}.
+     */
+    public UncheckedFilterReader(final Reader reader) {
+        super(reader);
+    }
+
+    /**
+     * Calls this method's super and rethrow {@link IOException} as {@link 
UncheckedIOException}.
+     */
+    @Override
+    public void close() throws UncheckedIOException {
+        try {
+            super.close();
+        } catch (final IOException e) {
+            throw new UncheckedIOException(e);
+        }
+    }
+
+    /**
+     * Calls this method's super and rethrow {@link IOException} as {@link 
UncheckedIOException}.
+     */
+    @Override
+    public void mark(final int readAheadLimit) throws UncheckedIOException {
+        try {
+            super.mark(readAheadLimit);
+        } catch (final IOException e) {
+            throw new UncheckedIOException(e);
+        }
+    }
+
+    /**
+     * Calls this method's super and rethrow {@link IOException} as {@link 
UncheckedIOException}.
+     */
+    @Override
+    public int read() throws UncheckedIOException {
+        try {
+            return super.read();
+        } catch (final IOException e) {
+            throw new UncheckedIOException(e);
+        }
+    }
+
+    /**
+     * Calls this method's super and rethrow {@link IOException} as {@link 
UncheckedIOException}.
+     */
+    @Override
+    public int read(final char[] cbuf) throws UncheckedIOException {
+        try {
+            return super.read(cbuf);
+        } catch (final IOException e) {
+            throw new UncheckedIOException(e);
+        }
+    }
+
+    /**
+     * Calls this method's super and rethrow {@link IOException} as {@link 
UncheckedIOException}.
+     */
+    @Override
+    public int read(final char[] cbuf, final int off, final int len) throws 
UncheckedIOException {
+        try {
+            return super.read(cbuf, off, len);
+        } catch (final IOException e) {
+            throw new UncheckedIOException(e);
+        }
+    }
+
+    /**
+     * Calls this method's super and rethrow {@link IOException} as {@link 
UncheckedIOException}.
+     */
+    @Override
+    public int read(final CharBuffer target) throws UncheckedIOException {
+        try {
+            return super.read(target);
+        } catch (final IOException e) {
+            throw new UncheckedIOException(e);
+        }
+    }
+
+    /**
+     * Calls this method's super and rethrow {@link IOException} as {@link 
UncheckedIOException}.
+     */
+    @Override
+    public boolean ready() throws UncheckedIOException {
+        try {
+            return super.ready();
+        } catch (final IOException e) {
+            throw new UncheckedIOException(e);
+        }
+    }
+
+    /**
+     * Calls this method's super and rethrow {@link IOException} as {@link 
UncheckedIOException}.
+     */
+    @Override
+    public void reset() throws UncheckedIOException {
+        try {
+            super.reset();
+        } catch (final IOException e) {
+            throw new UncheckedIOException(e);
+        }
+    }
+
+    /**
+     * Calls this method's super and rethrow {@link IOException} as {@link 
UncheckedIOException}.
+     */
+    @Override
+    public long skip(final long n) throws UncheckedIOException {
+        try {
+            return super.skip(n);
+        } catch (final IOException e) {
+            throw new UncheckedIOException(e);
+        }
+    }
+
+}
diff --git 
a/src/test/java/org/apache/commons/io/input/UncheckedFilterReaderTest.java 
b/src/test/java/org/apache/commons/io/input/UncheckedFilterReaderTest.java
new file mode 100644
index 0000000..55f2b00
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/input/UncheckedFilterReaderTest.java
@@ -0,0 +1,189 @@
+/*
+ * 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.input;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.io.UncheckedIOException;
+import java.nio.CharBuffer;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link UncheckedFilterReader}.
+ */
+public class UncheckedFilterReaderTest {
+
+    private UncheckedFilterReader ucStringReader;
+    private UncheckedFilterReader ucBrokenReader;
+    private IOException exception = new IOException("test exception");
+
+    @SuppressWarnings("resource")
+    @BeforeEach
+    public void beforeEach() {
+        ucStringReader = UncheckedFilterReader.on(new StringReader("01"));
+        exception = new IOException("test exception");
+        ucBrokenReader = UncheckedFilterReader.on(new BrokenReader(exception));
+    }
+
+    @Test
+    public void testClose() {
+        ucStringReader.close();
+        assertThrows(UncheckedIOException.class, () -> ucBrokenReader.read());
+    }
+
+    @Test
+    public void testCloseThrows() {
+        assertEquals(exception, assertThrows(UncheckedIOException.class, () -> 
ucBrokenReader.close()).getCause());
+    }
+
+    @Test
+    public void testMarkReset() {
+        ucStringReader.mark(10);
+        final int c = ucStringReader.read();
+        ucStringReader.reset();
+        assertEquals(c, ucStringReader.read());
+    }
+
+    @Test
+    public void testMarkThrows() {
+        try (UncheckedFilterReader closedReader = 
UncheckedFilterReader.on(ClosedReader.CLOSED_READER)) {
+            closedReader.close();
+            assertThrows(UncheckedIOException.class, () -> 
closedReader.mark(1));
+        }
+    }
+
+    @Test
+    public void testRead() {
+        try (final UncheckedFilterReader uncheckedReader = 
UncheckedFilterReader.on(ucStringReader)) {
+            assertEquals('0', uncheckedReader.read());
+            assertEquals('1', uncheckedReader.read());
+            assertEquals(IOUtils.EOF, uncheckedReader.read());
+            assertEquals(IOUtils.EOF, uncheckedReader.read());
+        }
+    }
+
+    @Test
+    public void testReadCharArray() {
+        try (final UncheckedFilterReader uncheckedReader = 
UncheckedFilterReader.on(ucStringReader)) {
+            final char[] array = new char[1];
+            assertEquals(1, uncheckedReader.read(array));
+            assertEquals('0', array[0]);
+            array[0] = 0;
+            assertEquals(1, uncheckedReader.read(array));
+            assertEquals('1', array[0]);
+            array[0] = 0;
+            assertEquals(IOUtils.EOF, uncheckedReader.read(array));
+            assertEquals(0, array[0]);
+            assertEquals(IOUtils.EOF, uncheckedReader.read(array));
+            assertEquals(0, array[0]);
+        }
+    }
+
+    @Test
+    public void testReadCharArrayIndexed() {
+        try (final UncheckedFilterReader uncheckedReader = 
UncheckedFilterReader.on(ucStringReader)) {
+            final char[] array = new char[1];
+            assertEquals(1, uncheckedReader.read(array, 0, 1));
+            assertEquals('0', array[0]);
+            array[0] = 0;
+            assertEquals(1, uncheckedReader.read(array, 0, 1));
+            assertEquals('1', array[0]);
+            array[0] = 0;
+            assertEquals(IOUtils.EOF, uncheckedReader.read(array, 0, 1));
+            assertEquals(0, array[0]);
+            assertEquals(IOUtils.EOF, uncheckedReader.read(array, 0, 1));
+            assertEquals(0, array[0]);
+        }
+    }
+
+    @Test
+    public void testReadCharArrayIndexedThrows() {
+        assertEquals(exception, assertThrows(UncheckedIOException.class, () -> 
ucBrokenReader.read(new char[1], 0, 1)).getCause());
+    }
+
+    @Test
+    public void testReadCharArrayThrows() {
+        assertEquals(exception, assertThrows(UncheckedIOException.class, () -> 
ucBrokenReader.read(new char[1])).getCause());
+    }
+
+    @Test
+    public void testReadCharBuffer() {
+        try (final UncheckedFilterReader uncheckedReader = 
UncheckedFilterReader.on(ucStringReader)) {
+            final CharBuffer buffer = CharBuffer.wrap(new char[1]);
+            assertEquals(1, uncheckedReader.read(buffer));
+            buffer.flip();
+            assertEquals('0', buffer.charAt(0));
+            buffer.put(0, (char) 0);
+            assertEquals(1, uncheckedReader.read(buffer));
+            buffer.flip();
+            assertEquals('1', buffer.charAt(0));
+            buffer.put(0, (char) 0);
+            assertEquals(IOUtils.EOF, uncheckedReader.read(buffer));
+            buffer.flip();
+            assertEquals(0, buffer.length());
+            assertEquals(0, uncheckedReader.read(buffer));
+            buffer.flip();
+            assertEquals(0, buffer.length());
+        }
+    }
+
+    @Test
+    public void testReadCharBufferThrows() {
+        assertEquals(exception, assertThrows(UncheckedIOException.class, () -> 
ucBrokenReader.read(CharBuffer.wrap(new char[1]))).getCause());
+    }
+
+    @Test
+    public void testReadThrows() {
+        assertEquals(exception, assertThrows(UncheckedIOException.class, () -> 
ucBrokenReader.read()).getCause());
+    }
+
+    @Test
+    public void testReady() {
+        assertTrue(ucStringReader.ready());
+    }
+
+    @Test
+    public void testReadyThrows() {
+        assertEquals(exception, assertThrows(UncheckedIOException.class, () -> 
ucBrokenReader.ready()).getCause());
+    }
+
+    @Test
+    public void testResetThrows() {
+        try (UncheckedFilterReader closedReader = 
UncheckedFilterReader.on(ClosedReader.CLOSED_READER)) {
+            closedReader.close();
+            assertThrows(UncheckedIOException.class, () -> 
ucBrokenReader.reset());
+        }
+    }
+
+    @Test
+    public void testSkip() {
+        assertEquals(1, ucStringReader.skip(1));
+    }
+
+    @Test
+    public void testSkipThrows() {
+        assertEquals(exception, assertThrows(UncheckedIOException.class, () -> 
ucBrokenReader.skip(1)).getCause());
+    }
+
+}

Reply via email to