< prev index next >

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

Print this page
rev 48083 : [mq]: 4358774-nullISAndOS.01
rev 48082 : 4358774: Add null InputStream and OutputStream
Reviewed-by: XXX
rev 47920 : 8191516: OutputStream.write(byte[],int,int) could have fewer parameter bounds checks
Summary: Reduce parameter bounds checks from five to three as in InputStream::read
Reviewed-by: psandoz
rev 47216 : 8187443: Forest Consolidation: Move files to unified layout
Reviewed-by: darcy, ihse


  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             @Override
  71             public void write(int b) throws IOException {
  72                 if (closed) {
  73                     throw new IOException("Stream closed");
  74                 }
  75             }
  76 
  77             @Override
  78             // overridden for efficiency
  79             public void write(byte b[], int off, int len) throws IOException {
  80                 Objects.requireNonNull(b);
  81                 Objects.checkFromIndexSize(off, len, b.length);
  82                 if (closed) {
  83                     throw new IOException("Stream closed");
  84                 }
  85             }
  86 
  87             @Override
  88             public void close() {
  89                 closed = true;
  90             }
  91         };
  92     }
  93 
  94     /**
  95      * Writes the specified byte to this output stream. The general
  96      * contract for <code>write</code> is that one byte is written
  97      * to the output stream. The byte to be written is the eight
  98      * low-order bits of the argument <code>b</code>. The 24
  99      * high-order bits of <code>b</code> are ignored.
 100      * <p>
 101      * Subclasses of <code>OutputStream</code> must provide an
 102      * implementation for this method.
 103      *
 104      * @param      b   the <code>byte</code>.
 105      * @exception  IOException  if an I/O error occurs. In particular,
 106      *             an <code>IOException</code> may be thrown if the
 107      *             output stream has been closed.
 108      */
 109     public abstract void write(int b) throws IOException;
 110 
 111     /**
 112      * Writes <code>b.length</code> bytes from the specified byte array
 113      * to this output stream. The general contract for <code>write(b)</code>
 114      * is that it should have exactly the same effect as the call


< prev index next >