Skip to content

Commit 166fc85

Browse files
author
Niall Pemberton
committed
IO-95 Add NullWriter implementation
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@454247 13f79535-47bb-0310-9956-ffa450edef68
1 parent 95d3fa1 commit 166fc85

2 files changed

Lines changed: 123 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.IOException;
20+
import java.io.Writer;
21+
22+
/**
23+
* This {@link Writer} writes all data to the famous <b>/dev/null</b>.
24+
* <p>
25+
* This {@link Writer} has no destination (file/socket etc.) and all
26+
* characters written to it are ignored and lost.
27+
*
28+
* @version $Id$
29+
*/
30+
public class NullWriter extends Writer {
31+
32+
/**
33+
* Constructs a new NullWriter.
34+
*/
35+
public NullWriter() {
36+
}
37+
38+
/** @see java.io.Writer#write(int) */
39+
public void write(int idx) throws IOException {
40+
//to /dev/null
41+
}
42+
43+
/** @see java.io.Writer#write(char[]) */
44+
public void write(char[] chr) throws IOException {
45+
//to /dev/null
46+
}
47+
48+
/** @see java.io.Writer#write(char[], int, int) */
49+
public void write(char[] chr, int st, int end) throws IOException {
50+
//to /dev/null
51+
}
52+
53+
/** @see java.io.Writer#write(String) */
54+
public void write(String str) throws IOException {
55+
//to /dev/null
56+
}
57+
58+
/** @see java.io.Writer#write(String, int, int) */
59+
public void write(String str, int st, int end) throws IOException {
60+
//to /dev/null
61+
}
62+
63+
/** @see java.io.Writer#flush() */
64+
public void flush() throws IOException {
65+
//to /dev/null
66+
}
67+
68+
/** @see java.io.Writer#close() */
69+
public void close() throws IOException {
70+
//to /dev/null
71+
}
72+
73+
}
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+
20+
import java.io.IOException;
21+
22+
import junit.framework.TestCase;
23+
24+
25+
/**
26+
* Really not a lot to do here, but checking that no
27+
* Exceptions are thrown.
28+
*
29+
* @version $Revision$
30+
*/
31+
32+
public class NullWriterTest extends TestCase {
33+
34+
public NullWriterTest(String name) {
35+
super(name);
36+
}
37+
38+
public void testNull() throws IOException {
39+
char[] chars = new char[] {'A', 'B', 'C'};
40+
NullWriter writer = new NullWriter();
41+
writer.write(1);
42+
writer.write(chars);
43+
writer.write(chars, 1, 1);
44+
writer.write("some string");
45+
writer.write("some string", 2, 2);
46+
writer.flush();
47+
writer.close();
48+
}
49+
50+
}

0 commit comments

Comments
 (0)