1 /* 2 * Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.io; 27 28 import java.util.Objects; 29 30 /** 31 * This abstract class is the superclass of all classes representing 32 * an output stream of bytes. An output stream accepts output bytes 33 * and sends them to some sink. 34 * <p> 35 * Applications that need to define a subclass of 36 * {@code OutputStream} must always provide at least a method 37 * that writes one byte of output. 38 * 39 * @author Arthur van Hoff 40 * @see java.io.BufferedOutputStream 41 * @see java.io.ByteArrayOutputStream 42 * @see java.io.DataOutputStream 43 * @see java.io.FilterOutputStream 44 * @see java.io.InputStream 45 * @see java.io.OutputStream#write(int) 46 * @since 1.0 47 */ 48 public abstract class OutputStream implements Closeable, Flushable { 49 /** 50 * Constructor for subclasses to call. 51 */ OutputStream()52 public OutputStream() {} 53 54 /** 55 * Returns a new {@code OutputStream} which discards all bytes. The 56 * returned stream is initially open. The stream is closed by calling 57 * the {@code close()} method. Subsequent calls to {@code close()} have 58 * no effect. 59 * 60 * <p> While the stream is open, the {@code write(int)}, {@code 61 * write(byte[])}, and {@code write(byte[], int, int)} methods do nothing. 62 * After the stream has been closed, these methods all throw {@code 63 * IOException}. 64 * 65 * <p> The {@code flush()} method does nothing. 66 * 67 * @return an {@code OutputStream} which discards all bytes 68 * 69 * @since 11 70 */ nullOutputStream()71 public static OutputStream nullOutputStream() { 72 return new OutputStream() { 73 private volatile boolean closed; 74 75 private void ensureOpen() throws IOException { 76 if (closed) { 77 throw new IOException("Stream closed"); 78 } 79 } 80 81 @Override 82 public void write(int b) throws IOException { 83 ensureOpen(); 84 } 85 86 @Override 87 public void write(byte b[], int off, int len) throws IOException { 88 Objects.checkFromIndexSize(off, len, b.length); 89 ensureOpen(); 90 } 91 92 @Override 93 public void close() { 94 closed = true; 95 } 96 }; 97 } 98 99 /** 100 * Writes the specified byte to this output stream. The general 101 * contract for {@code write} is that one byte is written 102 * to the output stream. The byte to be written is the eight 103 * low-order bits of the argument {@code b}. The 24 104 * high-order bits of {@code b} are ignored. 105 * <p> 106 * Subclasses of {@code OutputStream} must provide an 107 * implementation for this method. 108 * 109 * @param b the {@code byte}. 110 * @throws IOException if an I/O error occurs. In particular, 111 * an {@code IOException} may be thrown if the 112 * output stream has been closed. 113 */ write(int b)114 public abstract void write(int b) throws IOException; 115 116 /** 117 * Writes {@code b.length} bytes from the specified byte array 118 * to this output stream. The general contract for {@code write(b)} 119 * is that it should have exactly the same effect as the call 120 * {@code write(b, 0, b.length)}. 121 * 122 * @param b the data. 123 * @throws IOException if an I/O error occurs. 124 * @see java.io.OutputStream#write(byte[], int, int) 125 */ write(byte b[])126 public void write(byte b[]) throws IOException { 127 write(b, 0, b.length); 128 } 129 130 /** 131 * Writes {@code len} bytes from the specified byte array 132 * starting at offset {@code off} to this output stream. 133 * The general contract for {@code write(b, off, len)} is that 134 * some of the bytes in the array {@code b} are written to the 135 * output stream in order; element {@code b[off]} is the first 136 * byte written and {@code b[off+len-1]} is the last byte written 137 * by this operation. 138 * <p> 139 * The {@code write} method of {@code OutputStream} calls 140 * the write method of one argument on each of the bytes to be 141 * written out. Subclasses are encouraged to override this method and 142 * provide a more efficient implementation. 143 * <p> 144 * If {@code b} is {@code null}, a 145 * {@code NullPointerException} is thrown. 146 * <p> 147 * If {@code off} is negative, or {@code len} is negative, or 148 * {@code off+len} is greater than the length of the array 149 * {@code b}, then an {@code IndexOutOfBoundsException} is thrown. 150 * 151 * @param b the data. 152 * @param off the start offset in the data. 153 * @param len the number of bytes to write. 154 * @throws IOException if an I/O error occurs. In particular, 155 * an {@code IOException} is thrown if the output 156 * stream is closed. 157 */ write(byte b[], int off, int len)158 public void write(byte b[], int off, int len) throws IOException { 159 Objects.checkFromIndexSize(off, len, b.length); 160 // len == 0 condition implicitly handled by loop bounds 161 for (int i = 0 ; i < len ; i++) { 162 write(b[off + i]); 163 } 164 } 165 166 /** 167 * Flushes this output stream and forces any buffered output bytes 168 * to be written out. The general contract of {@code flush} is 169 * that calling it is an indication that, if any bytes previously 170 * written have been buffered by the implementation of the output 171 * stream, such bytes should immediately be written to their 172 * intended destination. 173 * <p> 174 * If the intended destination of this stream is an abstraction provided by 175 * the underlying operating system, for example a file, then flushing the 176 * stream guarantees only that bytes previously written to the stream are 177 * passed to the operating system for writing; it does not guarantee that 178 * they are actually written to a physical device such as a disk drive. 179 * <p> 180 * The {@code flush} method of {@code OutputStream} does nothing. 181 * 182 * @throws IOException if an I/O error occurs. 183 */ flush()184 public void flush() throws IOException { 185 } 186 187 /** 188 * Closes this output stream and releases any system resources 189 * associated with this stream. The general contract of {@code close} 190 * is that it closes the output stream. A closed stream cannot perform 191 * output operations and cannot be reopened. 192 * <p> 193 * The {@code close} method of {@code OutputStream} does nothing. 194 * 195 * @throws IOException if an I/O error occurs. 196 */ close()197 public void close() throws IOException { 198 } 199 200 } 201