View Javadoc
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    *      https://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  import org.apache.commons.io.IOUtils;
22  
23  /**
24   * Never writes data. Calls never go beyond this class.
25   * <p>
26   * This {@link Writer} has no destination (file/socket etc.) and all characters written to it are ignored and lost.
27   * </p>
28   */
29  public class NullWriter extends Writer {
30  
31      /**
32       * The singleton instance.
33       *
34       * @since 2.12.0
35       */
36      public static final NullWriter INSTANCE = new NullWriter();
37  
38      /**
39       * The singleton instance.
40       *
41       * @deprecated Use {@link #INSTANCE}.
42       */
43      @Deprecated
44      public static final NullWriter NULL_WRITER = INSTANCE;
45  
46      /**
47       * Constructs a new NullWriter.
48       *
49       * @deprecated Use {@link #INSTANCE}.
50       */
51      @Deprecated
52      public NullWriter() {
53      }
54  
55      /**
56       * Does nothing, like writing to {@code /dev/null}.
57       *
58       * @param c The character to write.
59       * @return this writer.
60       * @since 2.0
61       */
62      @Override
63      public Writer append(final char c) {
64          //to /dev/null
65          return this;
66      }
67  
68      /**
69       * Does nothing, like writing to {@code /dev/null}.
70       *
71       * @param csq The character sequence to write.
72       * @return this writer
73       * @since 2.0
74       */
75      @Override
76      public Writer append(final CharSequence csq) {
77          //to /dev/null
78          return this;
79      }
80  
81      /**
82       * Does nothing except argument validation, like writing to {@code /dev/null}.
83       *
84       * @param csq   The character sequence from which a subsequence will be
85       *              appended.
86       *              If {@code csq} is {@code null}, it is treated as if it were
87       *              {@code "null"}.
88       * @param start The index of the first character in the subsequence.
89       * @param end   The index of the character following the last character in the
90       *              subsequence.
91       * @return {@code this} instance.
92       * @throws IndexOutOfBoundsException If {@code start} or {@code end} are negative, {@code end} is
93       *                                   greater than {@code csq.length()}, or {@code start} is greater
94       *                                   than {@code end}.
95       * @since 2.0
96       */
97      @Override
98      public Writer append(final CharSequence csq, final int start, final int end) {
99          IOUtils.checkFromToIndex(csq, start, end);
100         //to /dev/null
101         return this;
102     }
103 
104     /** @see Writer#close() */
105     @Override
106     public void close() {
107         //to /dev/null
108     }
109 
110     /** @see Writer#flush() */
111     @Override
112     public void flush() {
113         //to /dev/null
114     }
115 
116     /**
117      * Does nothing except argument validation, like writing to {@code /dev/null}.
118      *
119      * @param chr The characters to write, not {@code null}.
120      * @throws NullPointerException if {@code chr} is {@code null}.
121      */
122     @Override
123     public void write(final char[] chr) {
124         write(chr, 0, chr.length);
125         //to /dev/null
126     }
127 
128     /**
129      * Does nothing except argument validation, like writing to {@code /dev/null}.
130      *
131      * @param cbuf The characters to write, not {@code null}.
132      * @param off  The start offset.
133      * @param len  The number of characters to write.
134      * @throws NullPointerException      if {@code chr} is {@code null}.
135      * @throws IndexOutOfBoundsException If ({@code off} or {@code len} are negative, or {@code off + len} is greater than {@code cbuf.length}.
136      */
137     @Override
138     public void write(final char[] cbuf, final int off, final int len) {
139         IOUtils.checkFromIndexSize(cbuf, off, len);
140         //to /dev/null
141     }
142 
143     /**
144      * Does nothing, like writing to {@code /dev/null}.
145      *
146      * @param b The character to write.
147      */
148     @Override
149     public void write(final int b) {
150         //to /dev/null
151     }
152 
153     /**
154      * Does nothing except argument validation, like writing to {@code /dev/null}.
155      *
156      * @param str The string to write, not {@code null}.
157      * @throws NullPointerException if {@code str} is {@code null}.
158      */
159     @Override
160     public void write(final String str) {
161         write(str, 0, str.length());
162         //to /dev/null
163     }
164 
165     /**
166      * Does nothing except argument validation, like writing to {@code /dev/null}.
167      *
168      * @param str The string to write, not {@code null}.
169      * @param off The start offset.
170      * @param len The number of characters to write.
171      * @throws NullPointerException      If {@code str} is {@code null}.
172      * @throws IndexOutOfBoundsException If ({@code off} or {@code len} are negative, or {@code off + len} is greater than {@code str.length()}.
173      */
174     @Override
175     public void write(final String str, final int off, final int len) {
176         IOUtils.checkFromIndexSize(str, off, len);
177         //to /dev/null
178     }
179 
180 }