Skip to content

Commit d313778

Browse files
author
Gary Gregory
committed
[IO-608] Add a convenience NullPrintStream.
1 parent a467b03 commit d313778

3 files changed

Lines changed: 97 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ The <action> type attribute can be add,update,fix,remove.
101101
<action issue="IO-578" dev="ggregory" type="add" due-to="Mark Chesney">
102102
Support java.nio.Path and non-default file systems for ReversedLinesFileReader (#62)
103103
</action>
104+
<action issue="IO-608" dev="ggregory" type="add" due-to="Gary Gregory">
105+
Add a convenience NullPrintStream.
106+
</action>
104107
</release>
105108

106109
<release version="2.6" date="2017-10-15" description="Java 7 required, Java 9 supported.">
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
18+
package org.apache.commons.io.output;
19+
20+
import java.io.PrintStream;
21+
22+
/**
23+
* This PrintStream writes all data to the famous <b>/dev/null</b>.
24+
* <p>
25+
* This print stream has no destination (file/socket etc.) and all bytes written to it are ignored and lost.
26+
* </p>
27+
*
28+
* @since 2.7
29+
*/
30+
public class NullPrintStream extends PrintStream {
31+
32+
public static final NullPrintStream NULL_PRINT_STREAM = new NullPrintStream();
33+
34+
@SuppressWarnings("resource")
35+
public NullPrintStream() {
36+
super(new NullOutputStream());
37+
}
38+
39+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
20+
import java.io.IOException;
21+
22+
import org.junit.Test;
23+
24+
25+
/**
26+
* Really not a lot to do here, but checking that no Exceptions are thrown.
27+
*/
28+
public class NullPrintStreamTest {
29+
30+
private void process(final NullPrintStream nos) throws IOException {
31+
nos.write("string".getBytes());
32+
nos.write("some string".getBytes(), 3, 5);
33+
nos.write(1);
34+
nos.write(0x0f);
35+
nos.flush();
36+
nos.close();
37+
nos.write("allowed".getBytes());
38+
nos.write(255);
39+
}
40+
41+
@Test
42+
public void testNullNewInstance() throws IOException {
43+
try (final NullPrintStream nos = new NullPrintStream()) {
44+
process(nos);
45+
}
46+
}
47+
48+
@Test
49+
public void testNullSingleton() throws IOException {
50+
try (final NullPrintStream nos = NullPrintStream.NULL_PRINT_STREAM) {
51+
process(nos);
52+
}
53+
}
54+
55+
}

0 commit comments

Comments
 (0)