001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * https://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.io.output; 018 019import java.io.Writer; 020 021import org.apache.commons.io.IOUtils; 022 023/** 024 * Never writes data. Calls never go beyond this class. 025 * <p> 026 * This {@link Writer} has no destination (file/socket etc.) and all characters written to it are ignored and lost. 027 * </p> 028 */ 029public class NullWriter extends Writer { 030 031 /** 032 * The singleton instance. 033 * 034 * @since 2.12.0 035 */ 036 public static final NullWriter INSTANCE = new NullWriter(); 037 038 /** 039 * The singleton instance. 040 * 041 * @deprecated Use {@link #INSTANCE}. 042 */ 043 @Deprecated 044 public static final NullWriter NULL_WRITER = INSTANCE; 045 046 /** 047 * Constructs a new NullWriter. 048 * 049 * @deprecated Use {@link #INSTANCE}. 050 */ 051 @Deprecated 052 public NullWriter() { 053 } 054 055 /** 056 * Does nothing, like writing to {@code /dev/null}. 057 * 058 * @param c The character to write. 059 * @return this writer. 060 * @since 2.0 061 */ 062 @Override 063 public Writer append(final char c) { 064 //to /dev/null 065 return this; 066 } 067 068 /** 069 * Does nothing, like writing to {@code /dev/null}. 070 * 071 * @param csq The character sequence to write. 072 * @return this writer 073 * @since 2.0 074 */ 075 @Override 076 public Writer append(final CharSequence csq) { 077 //to /dev/null 078 return this; 079 } 080 081 /** 082 * Does nothing except argument validation, like writing to {@code /dev/null}. 083 * 084 * @param csq The character sequence from which a subsequence will be 085 * appended. 086 * If {@code csq} is {@code null}, it is treated as if it were 087 * {@code "null"}. 088 * @param start The index of the first character in the subsequence. 089 * @param end The index of the character following the last character in the 090 * subsequence. 091 * @return {@code this} instance. 092 * @throws IndexOutOfBoundsException If {@code start} or {@code end} are negative, {@code end} is 093 * greater than {@code csq.length()}, or {@code start} is greater 094 * than {@code end}. 095 * @since 2.0 096 */ 097 @Override 098 public Writer append(final CharSequence csq, final int start, final int end) { 099 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}