Add classes ClosedReader and CloseShieldReader. Closes
Applied modified patch from Rob Spoor.
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -113,6 +113,9 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="IO-612" dev="ggregory" type="add" due-to="Rob Spoor, Gary Gregory">
Add class TeeReader.
</action>
+ <action issue="IO-613" dev="ggregory" type="add" due-to="Rob Spoor, Gary Gregory">
+ Add classes ClosedReader and CloseShieldReader. #84.
+ </action>
</release>
<release version="2.6" date="2017-10-15" description="Java 7 required, Java 9 supported.">

MODIFY
src/main/java/org/apache/commons/io/input/CloseShieldInputStream.java
src/main/java/org/apache/commons/io/input/CloseShieldInputStream.java
* This class is typically used in cases where an input stream needs to be
* passed to a component that wants to explicitly close the stream even if
* more input would still be available to other components.
+ * </p>
*
* @since 1.4
*/
@@ -46,7 +47,7 @@ public CloseShieldInputStream(final InputStream in) {
*/
@Override
public void close() {
- in = new ClosedInputStream();
+ in = ClosedInputStream.CLOSED_INPUT_STREAM;
}
}

ADD
src/main/java/org/apache/commons/io/input/CloseShieldReader.java
+/*
+ * 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.Reader;
+
+/**
+ * Proxy stream that prevents the underlying reader from being closed.
+ * <p>
+ * This class is typically used in cases where a reader needs to be
+ * passed to a component that wants to explicitly close the reader even if
+ * more input would still be available to other components.
+ * <p>
+ *
+ * @since 2.7
+ */
+public class CloseShieldReader extends ProxyReader {
+
+ /**
+ * Creates a proxy that shields the given reader from being
+ * closed.
+ *
+ * @param in underlying reader
+ */
+ public CloseShieldReader(final Reader in) {
+ super(in);
+ }
+
+ /**
+ * Replaces the underlying reader with a {@link ClosedReader}
+ * sentinel. The original reader will remain open, but this proxy
+ * will appear closed.
+ */
+ @Override
+ public void close() {
+ in = ClosedReader.CLOSED_READER;
+ }
+
+}

ADD
src/main/java/org/apache/commons/io/input/ClosedReader.java
+/*
+ * 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.apache.commons.io.IOUtils.EOF;
+
+import java.io.IOException;
+import java.io.Reader;
+
+/**
+ * Closed reader. This reader returns EOF to all attempts to read
+ * something from it.
+ * <p>
+ * Typically uses of this class include testing for corner cases in methods
+ * that accept readers and acting as a sentinel value instead of a
+ * {@code null} reader.
+ * </p>
+ *
+ * @since 2.7
+ */
+public class ClosedReader extends Reader {
+
+ /**
+ * A singleton.
+ */
+ public static final ClosedReader CLOSED_READER = new ClosedReader();
+
+ /**
+ * Returns -1 to indicate that the stream is closed.
+ *
+ * @param cbuf ignored
+ * @param off ignored
+ * @param len ignored
+ * @return always -1
+ */
+ @Override
+ public int read(final char[] cbuf, final int off, final int len) {
+ return EOF;
+ }
+
+ @Override
+ public void close() throws IOException {
+ // noop
+ }
+
+}

ADD
src/test/java/org/apache/commons/io/input/CloseShieldReaderTest.java
+/*
+ * 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.Assert.assertEquals;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+import java.io.IOException;
+import java.io.Reader;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * JUnit Test Case for {@link CloseShieldReader}.
+ */
+public class CloseShieldReaderTest {
+
+ private String data;
+
+ private Reader original;
+
+ private Reader shielded;
+
+ @Before
+ public void setUp() {
+ data = "xyz";
+ original = spy(new CharSequenceReader(data));
+ shielded = new CloseShieldReader(original);
+ }
+
+ @Test
+ public void testClose() throws IOException {
+ shielded.close();
+ verify(original, never()).close();
+ char[] cbuf = new char[10];
+ assertEquals("read(cbuf, off, len)", -1, shielded.read(cbuf, 0, 10));
+ assertEquals("read(cbuf, off, len)", data.length(), original.read(cbuf, 0, 10));
+ assertEquals(data, new String(cbuf, 0, data.length()));
+ }
+
+}

ADD
src/test/java/org/apache/commons/io/input/ClosedReaderTest.java
+/*
+ * 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.Assert.assertEquals;
+
+import org.junit.Test;
+
+/**
+ * JUnit Test Case for {@link ClosedReader}.
+ */
+public class ClosedReaderTest {
+
+ @Test
+ public void testRead() throws Exception {
+ try (final ClosedReader cr = new ClosedReader()) {
+ assertEquals("read(cbuf, off, len)", -1, cr.read(new char[10], 0, 10));
+ }
+ }
+
+}

[IO-613] Add classes ClosedReader and CloseShieldReader. Closes #84.
Applied modified patch from Rob Spoor.