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 28a1de480c8c0a45500f754fdddd07a2e88fa94c Author: Gary Gregory <[email protected]> AuthorDate: Sat Jul 18 17:13:14 2020 -0400 Add NullAppendable. --- src/changes/changes.xml | 3 ++ .../apache/commons/io/output/NullAppendable.java | 57 ++++++++++++++++++++++ .../commons/io/output/NullAppendableTest.java | 39 +++++++++++++++ 3 files changed, 99 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index dddd9c7..5921d15 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -56,6 +56,9 @@ The <action> type attribute can be add,update,fix,remove. <action dev="ggregory" type="add" due-to="Gary Gregory"> Add org.apache.commons.io.file.PathUtils.deleteDirectory(Path, FileVisitOption...). </action> + <action dev="ggregory" type="add" due-to="Gary Gregory"> + Add NullAppendable. + </action> <action dev="ggregory" type="fix" due-to="Rob Spoor, Jochen Wiedmann"> CharSequenceReader.skip should return 0 instead of EOF on stream end #123. </action> diff --git a/src/main/java/org/apache/commons/io/output/NullAppendable.java b/src/main/java/org/apache/commons/io/output/NullAppendable.java new file mode 100644 index 0000000..5706abf --- /dev/null +++ b/src/main/java/org/apache/commons/io/output/NullAppendable.java @@ -0,0 +1,57 @@ +/* + * 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; + +/** + * Appends all data to the famous <b>/dev/null</b>. + * <p> + * This Appendable has no destination (file/socket etc.) and all characters written to it are ignored and lost. + * </p> + * + * @since 2.8 + */ +public class NullAppendable implements Appendable { + + /** + * A singleton. + */ + public static final NullAppendable INSTANCE = new NullAppendable(); + + /** Use the singleton. */ + private NullAppendable() { + // no instances. + } + + @Override + public Appendable append(final char c) throws IOException { + return this; + } + + @Override + public Appendable append(final CharSequence csq) throws IOException { + return this; + } + + @Override + public Appendable append(final CharSequence csq, final int start, final int end) throws IOException { + return this; + } + +} diff --git a/src/test/java/org/apache/commons/io/output/NullAppendableTest.java b/src/test/java/org/apache/commons/io/output/NullAppendableTest.java new file mode 100644 index 0000000..b5339be --- /dev/null +++ b/src/test/java/org/apache/commons/io/output/NullAppendableTest.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.IOException; + +import org.junit.jupiter.api.Test; + +/** + * Really not a lot to do here, but checking that no Exceptions are thrown. + */ +public class NullAppendableTest { + + @Test + public void testNull() throws IOException { + final char[] chars = new char[] {'A', 'B', 'C'}; + final Appendable appendable = NullAppendable.INSTANCE; + appendable.append('a'); + appendable.append("A"); + appendable.append("A", 0, 1); + appendable.append(null, 0, 1); + appendable.append(null, -1, -1); + } + +}
