Skip to content

Commit d15f480

Browse files
author
Gary Gregory
committed
[IO-617] Add class CloseShieldWriter. #83.
- Applied partial patch from GitHub PR #83 from Rob Spoor which contained duplication from another PR. - Reformatted some files. - Closes #83.
1 parent 556a455 commit d15f480

5 files changed

Lines changed: 116 additions & 2 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ The <action> type attribute can be add,update,fix,remove.
125125
<action issue="IO-616" dev="ggregory" type="add" due-to="Rob Spoor">
126126
Add class AppendableWriter. #87.
127127
</action>
128+
<action issue="IO-617" dev="ggregory" type="add" due-to="Rob Spoor, Gary Gregory">
129+
Add class CloseShieldWriter. #83.
130+
</action>
128131
</release>
129132

130133
<release version="2.6" date="2017-10-15" description="Java 7 required, Java 9 supported.">

src/main/java/org/apache/commons/io/output/CloseShieldOutputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public CloseShieldOutputStream(final OutputStream out) {
4747
*/
4848
@Override
4949
public void close() {
50-
out = new ClosedOutputStream();
50+
out = ClosedOutputStream.CLOSED_OUTPUT_STREAM;
5151
}
5252

5353
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.io.output;
18+
19+
import java.io.Writer;
20+
21+
/**
22+
* Proxy stream that prevents the underlying writer from being closed.
23+
* <p>
24+
* This class is typically used in cases where a writer needs to be passed to a component that wants to explicitly close
25+
* the writer even if other components would still use the writer for output.
26+
* </p>
27+
*
28+
* @since 2.7
29+
*/
30+
public class CloseShieldWriter extends ProxyWriter {
31+
32+
/**
33+
* Creates a proxy that shields the given writer from being closed.
34+
*
35+
* @param out underlying writer
36+
*/
37+
public CloseShieldWriter(final Writer out) {
38+
super(out);
39+
}
40+
41+
/**
42+
* Replaces the underlying writer with a {@link ClosedWriter} sentinel. The original writer will remain open, but
43+
* this proxy will appear closed.
44+
*/
45+
@Override
46+
public void close() {
47+
out = ClosedWriter.CLOSED_WRITER;
48+
}
49+
50+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.io.output;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.fail;
21+
import static org.mockito.Mockito.never;
22+
import static org.mockito.Mockito.spy;
23+
import static org.mockito.Mockito.verify;
24+
25+
import java.io.IOException;
26+
import java.io.Writer;
27+
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
31+
/**
32+
* JUnit Test Case for {@link CloseShieldWriter}.
33+
*/
34+
public class CloseShieldWriterTest {
35+
36+
private StringBuilderWriter original;
37+
38+
private Writer shielded;
39+
40+
@Before
41+
public void setUp() {
42+
original = spy(new StringBuilderWriter());
43+
shielded = new CloseShieldWriter(original);
44+
}
45+
46+
@Test
47+
public void testClose() throws IOException {
48+
shielded.close();
49+
verify(original, never()).close();
50+
try {
51+
shielded.write('x');
52+
fail("write(c)");
53+
} catch (final IOException ignore) {
54+
// expected
55+
}
56+
original.write('y');
57+
assertEquals(1, original.getBuilder().length());
58+
assertEquals('y', original.toString().charAt(0));
59+
}
60+
61+
}

src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class ClosedOutputStreamTest {
3232
* @throws Exception
3333
*/
3434
@Test
35-
public void testRead() throws Exception {
35+
public void testWrite() throws Exception {
3636
try (ClosedOutputStream cos = new ClosedOutputStream()) {
3737
cos.write('x');
3838
fail("write(b)");

0 commit comments

Comments
 (0)