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    *      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.logging.log4j.core.util;
19  
20  import java.io.IOException;
21  import java.io.OutputStream;
22  
23  /**
24   * Writes all data to the famous <b>/dev/null</b>.
25   * <p>
26   * This output stream has no destination (file/socket etc.) and all bytes written to it are ignored and lost.
27   * </p>
28   * Originally from Apache Commons IO.
29   *
30   * @since 2.3
31   */
32  public class NullOutputStream extends OutputStream {
33  
34      private static final NullOutputStream INSTANCE = new NullOutputStream();
35  
36      /**
37       * @deprecated Deprecated in 2.7: use {@link #getInstance()}.
38       */
39      @Deprecated
40      public static final NullOutputStream NULL_OUTPUT_STREAM = INSTANCE;
41  
42      /**
43       * Gets the singleton instance.
44       *
45       * @return the singleton instance.
46       */
47      public static NullOutputStream getInstance() {
48          return INSTANCE;
49      }
50  
51      private NullOutputStream() {
52          // do nothing
53      }
54  
55      /**
56       * Does nothing - output to <code>/dev/null</code>.
57       *
58       * @param b
59       *        The bytes to write
60       * @param off
61       *        The start offset
62       * @param len
63       *        The number of bytes to write
64       */
65      @Override
66      public void write(final byte[] b, final int off, final int len) {
67          // to /dev/null
68      }
69  
70      /**
71       * Does nothing - output to <code>/dev/null</code>.
72       *
73       * @param b
74       *        The byte to write
75       */
76      @Override
77      public void write(final int b) {
78          // to /dev/null
79      }
80  
81      /**
82       * Does nothing - output to <code>/dev/null</code>.
83       *
84       * @param b
85       *        The bytes to write
86       * @throws IOException
87       *         never
88       */
89      @Override
90      public void write(final byte[] b) throws IOException {
91          // to /dev/null
92      }
93  }