< prev index next >

src/java.base/share/classes/java/io/OutputStream.java

Print this page




  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</code> 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      * Writes the specified byte to this output stream. The general
  51      * contract for <code>write</code> is that one byte is written
  52      * to the output stream. The byte to be written is the eight
  53      * low-order bits of the argument <code>b</code>. The 24
  54      * high-order bits of <code>b</code> are ignored.
  55      * <p>
  56      * Subclasses of <code>OutputStream</code> must provide an
  57      * implementation for this method.
  58      *
  59      * @param      b   the <code>byte</code>.
  60      * @exception  IOException  if an I/O error occurs. In particular,
  61      *             an <code>IOException</code> may be thrown if the
  62      *             output stream has been closed.
  63      */
  64     public abstract void write(int b) throws IOException;
  65 
  66     /**
  67      * Writes <code>b.length</code> bytes from the specified byte array
  68      * to this output stream. The general contract for <code>write(b)</code>
  69      * is that it should have exactly the same effect as the call




  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</code> 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      * Returns a new {@code OutputStream} which discards all bytes.  The
  51      * returned stream is initially open.  The stream is closed by calling
  52      * the {@code close()} method.  Subsequent calls to {@code close()} have
  53      * no effect.
  54      *
  55      * <p> While the stream is open, the {@code write(int)}, {@code
  56      * write(byte[])}, and {@code write(byte[], int, int)} methods do nothing.
  57      * After the stream has been closed, these methods all throw {@code
  58      * IOException}.
  59      *
  60      * <p> The {@code flush()} method does nothing.
  61      *
  62      * @return an {@code OutputStream} which discards all bytes
  63      *
  64      * @since 10
  65      */
  66     public static OutputStream nullStream() {
  67         return new OutputStream() {
  68             private volatile boolean closed;
  69 
  70             private void ensureOpen() throws IOException {
  71                 if (closed) {
  72                     throw new IOException("Stream closed");
  73                 }
  74             }
  75 
  76             @Override
  77             public void write(int b) throws IOException {
  78                 ensureOpen();
  79             }
  80 
  81             @Override
  82             // overridden for efficiency
  83             public void write(byte b[], int off, int len) throws IOException {
  84                 Objects.requireNonNull(b);
  85                 Objects.checkFromIndexSize(off, len, b.length);
  86                 ensureOpen();
  87             }
  88 
  89             @Override
  90             public void close() {
  91                 closed = true;
  92             }
  93         };
  94     }
  95 
  96     /**
  97      * Writes the specified byte to this output stream. The general
  98      * contract for <code>write</code> is that one byte is written
  99      * to the output stream. The byte to be written is the eight
 100      * low-order bits of the argument <code>b</code>. The 24
 101      * high-order bits of <code>b</code> are ignored.
 102      * <p>
 103      * Subclasses of <code>OutputStream</code> must provide an
 104      * implementation for this method.
 105      *
 106      * @param      b   the <code>byte</code>.
 107      * @exception  IOException  if an I/O error occurs. In particular,
 108      *             an <code>IOException</code> may be thrown if the
 109      *             output stream has been closed.
 110      */
 111     public abstract void write(int b) throws IOException;
 112 
 113     /**
 114      * Writes <code>b.length</code> bytes from the specified byte array
 115      * to this output stream. The general contract for <code>write(b)</code>
 116      * is that it should have exactly the same effect as the call


< prev index next >