You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/05/27 12:18:58 UTC

[commons-io] 02/02: [IO-608] Add a convenience NullPrintStream.

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 d3137782060046872e1ca15f244cbe6d4142b7cf
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon May 27 08:18:53 2019 -0400

    [IO-608] Add a convenience NullPrintStream.
---
 src/changes/changes.xml                            |  3 ++
 .../apache/commons/io/output/NullPrintStream.java  | 39 +++++++++++++++
 .../commons/io/output/NullPrintStreamTest.java     | 55 ++++++++++++++++++++++
 3 files changed, 97 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 9a7e901..76ce9b0 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -101,6 +101,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action issue="IO-578" dev="ggregory" type="add" due-to="Mark Chesney">
         Support java.nio.Path and non-default file systems for ReversedLinesFileReader (#62)
       </action>
+      <action issue="IO-608" dev="ggregory" type="add" due-to="Gary Gregory">
+        Add a convenience NullPrintStream.
+      </action>
     </release>
 
     <release version="2.6" date="2017-10-15" description="Java 7 required, Java 9 supported.">
diff --git a/src/main/java/org/apache/commons/io/output/NullPrintStream.java b/src/main/java/org/apache/commons/io/output/NullPrintStream.java
new file mode 100644
index 0000000..c81d29f
--- /dev/null
+++ b/src/main/java/org/apache/commons/io/output/NullPrintStream.java
@@ -0,0 +1,39 @@
+/*
+ * 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.output;
+
+import java.io.PrintStream;
+
+/**
+ * This PrintStream writes all data to the famous <b>/dev/null</b>.
+ * <p>
+ * This print stream has no destination (file/socket etc.) and all bytes written to it are ignored and lost.
+ * </p>
+ *
+ * @since 2.7
+ */
+public class NullPrintStream extends PrintStream {
+
+    public static final NullPrintStream NULL_PRINT_STREAM = new NullPrintStream();
+
+    @SuppressWarnings("resource")
+    public NullPrintStream() {
+        super(new NullOutputStream());
+    }
+
+}
diff --git a/src/test/java/org/apache/commons/io/output/NullPrintStreamTest.java b/src/test/java/org/apache/commons/io/output/NullPrintStreamTest.java
new file mode 100644
index 0000000..3dc2ac3
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/output/NullPrintStreamTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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.output;
+
+
+import java.io.IOException;
+
+import org.junit.Test;
+
+
+/**
+ * Really not a lot to do here, but checking that no Exceptions are thrown.
+ */
+public class NullPrintStreamTest {
+
+    private void process(final NullPrintStream nos) throws IOException {
+        nos.write("string".getBytes());
+        nos.write("some string".getBytes(), 3, 5);
+        nos.write(1);
+        nos.write(0x0f);
+        nos.flush();
+        nos.close();
+        nos.write("allowed".getBytes());
+        nos.write(255);
+    }
+
+    @Test
+    public void testNullNewInstance() throws IOException {
+        try (final NullPrintStream nos = new NullPrintStream()) {
+            process(nos);
+        }
+    }
+
+    @Test
+    public void testNullSingleton() throws IOException {
+        try (final NullPrintStream nos = NullPrintStream.NULL_PRINT_STREAM) {
+            process(nos);
+        }
+    }
+
+}