NullWriter.java

  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. import java.io.Writer;

  19. /**
  20.  * Never writes data. Calls never go beyond this class.
  21.  * <p>
  22.  * This {@link Writer} has no destination (file/socket etc.) and all characters written to it are ignored and lost.
  23.  * </p>
  24.  */
  25. public class NullWriter extends Writer {

  26.     /**
  27.      * The singleton instance.
  28.      *
  29.      * @since 2.12.0
  30.      */
  31.     public static final NullWriter INSTANCE = new NullWriter();

  32.     /**
  33.      * The singleton instance.
  34.      *
  35.      * @deprecated Use {@link #INSTANCE}.
  36.      */
  37.     @Deprecated
  38.     public static final NullWriter NULL_WRITER = INSTANCE;

  39.     /**
  40.      * Constructs a new NullWriter.
  41.      *
  42.      * @deprecated Use {@link #INSTANCE}.
  43.      */
  44.     @Deprecated
  45.     public NullWriter() {
  46.     }

  47.     /**
  48.      * Does nothing - output to {@code /dev/null}.
  49.      * @param c The character to write
  50.      * @return this writer
  51.      * @since 2.0
  52.      */
  53.     @Override
  54.     public Writer append(final char c) {
  55.         //to /dev/null
  56.         return this;
  57.     }

  58.     /**
  59.      * Does nothing - output to {@code /dev/null}.
  60.      * @param csq The character sequence to write
  61.      * @return this writer
  62.      * @since 2.0
  63.      */
  64.     @Override
  65.     public Writer append(final CharSequence csq) {
  66.         //to /dev/null
  67.         return this;
  68.     }

  69.     /**
  70.      * Does nothing - output to {@code /dev/null}.
  71.      * @param csq The character sequence to write
  72.      * @param start The index of the first character to write
  73.      * @param end  The index of the first character to write (exclusive)
  74.      * @return this writer
  75.      * @since 2.0
  76.      */
  77.     @Override
  78.     public Writer append(final CharSequence csq, final int start, final int end) {
  79.         //to /dev/null
  80.         return this;
  81.     }

  82.     /** @see java.io.Writer#close() */
  83.     @Override
  84.     public void close() {
  85.         //to /dev/null
  86.     }

  87.     /** @see java.io.Writer#flush() */
  88.     @Override
  89.     public void flush() {
  90.         //to /dev/null
  91.     }

  92.     /**
  93.      * Does nothing - output to {@code /dev/null}.
  94.      * @param chr The characters to write
  95.      */
  96.     @Override
  97.     public void write(final char[] chr) {
  98.         //to /dev/null
  99.     }

  100.     /**
  101.      * Does nothing - output to {@code /dev/null}.
  102.      * @param chr The characters to write
  103.      * @param st The start offset
  104.      * @param end The number of characters to write
  105.      */
  106.     @Override
  107.     public void write(final char[] chr, final int st, final int end) {
  108.         //to /dev/null
  109.     }

  110.     /**
  111.      * Does nothing - output to {@code /dev/null}.
  112.      * @param idx The character to write
  113.      */
  114.     @Override
  115.     public void write(final int idx) {
  116.         //to /dev/null
  117.     }

  118.     /**
  119.      * Does nothing - output to {@code /dev/null}.
  120.      * @param str The string to write
  121.      */
  122.     @Override
  123.     public void write(final String str) {
  124.         //to /dev/null
  125.     }

  126.     /**
  127.      * Does nothing - output to {@code /dev/null}.
  128.      * @param str The string to write
  129.      * @param st The start offset
  130.      * @param end The number of characters to write
  131.      */
  132.     @Override
  133.     public void write(final String str, final int st, final int end) {
  134.         //to /dev/null
  135.     }

  136. }