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 if (b == null) {
85 throw new NullPointerException();
86 } else if (off < 0 || len < 0 || len > b.length - off) {
87 throw new IndexOutOfBoundsException();
88 }
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</code> 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</code>. The 24
104 * high-order bits of <code>b</code> are ignored.
105 * <p>
106 * Subclasses of <code>OutputStream</code> must provide an
107 * implementation for this method.
108 *
109 * @param b the <code>byte</code>.
110 * @exception IOException if an I/O error occurs. In particular,
111 * an <code>IOException</code> may be thrown if the
112 * output stream has been closed.
113 */
114 public abstract void write(int b) throws IOException;
115
116 /**
117 * Writes <code>b.length</code> bytes from the specified byte array
118 * to this output stream. The general contract for <code>write(b)</code>
119 * is that it should have exactly the same effect as the call
|