62 * @return an {@code OutputStream} which discards all bytes 63 * 64 * @since 11 65 */ 66 public static OutputStream nullOutputStream() { 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 public void write(byte b[], int off, int len) throws IOException { 83 Objects.checkFromIndexSize(off, len, b.length); 84 ensureOpen(); 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 115 * <code>write(b, 0, b.length)</code>. 116 * 117 * @param b the data. 118 * @exception IOException if an I/O error occurs. 119 * @see java.io.OutputStream#write(byte[], int, int) 120 */ 121 public void write(byte b[]) throws IOException { 122 write(b, 0, b.length); 123 } 124 125 /** 126 * Writes <code>len</code> bytes from the specified byte array 127 * starting at offset <code>off</code> to this output stream. 128 * The general contract for <code>write(b, off, len)</code> is that 129 * some of the bytes in the array <code>b</code> are written to the 130 * output stream in order; element <code>b[off]</code> is the first 131 * byte written and <code>b[off+len-1]</code> is the last byte written 132 * by this operation. 133 * <p> 134 * The <code>write</code> method of <code>OutputStream</code> calls 135 * the write method of one argument on each of the bytes to be 136 * written out. Subclasses are encouraged to override this method and 137 * provide a more efficient implementation. 138 * <p> 139 * If <code>b</code> is <code>null</code>, a 140 * <code>NullPointerException</code> is thrown. 141 * <p> 142 * If <code>off</code> is negative, or <code>len</code> is negative, or 143 * <code>off+len</code> is greater than the length of the array 144 * {@code b}, then an {@code IndexOutOfBoundsException} is thrown. 145 * 146 * @param b the data. 147 * @param off the start offset in the data. 148 * @param len the number of bytes to write. 149 * @exception IOException if an I/O error occurs. In particular, 150 * an <code>IOException</code> is thrown if the output 151 * stream is closed. 152 */ 153 public void write(byte b[], int off, int len) throws IOException { 154 Objects.checkFromIndexSize(off, len, b.length); 155 // len == 0 condition implicitly handled by loop bounds 156 for (int i = 0 ; i < len ; i++) { 157 write(b[off + i]); 158 } 159 } 160 161 /** 162 * Flushes this output stream and forces any buffered output bytes 163 * to be written out. The general contract of <code>flush</code> is 164 * that calling it is an indication that, if any bytes previously 165 * written have been buffered by the implementation of the output 166 * stream, such bytes should immediately be written to their 167 * intended destination. 168 * <p> 169 * If the intended destination of this stream is an abstraction provided by 170 * the underlying operating system, for example a file, then flushing the 171 * stream guarantees only that bytes previously written to the stream are 172 * passed to the operating system for writing; it does not guarantee that 173 * they are actually written to a physical device such as a disk drive. | 62 * @return an {@code OutputStream} which discards all bytes 63 * 64 * @since 11 65 */ 66 public static OutputStream nullOutputStream() { 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 public void write(byte[] b, int off, int len) throws IOException { 83 Objects.checkFromIndexSize(off, len, b.length); 84 ensureOpen(); 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 115 * <code>write(b, 0, b.length)</code>. 116 * 117 * @param b the data. 118 * @exception IOException if an I/O error occurs. 119 * @see java.io.OutputStream#write(byte[], int, int) 120 */ 121 public void write(byte[] b) throws IOException { 122 write(b, 0, b.length); 123 } 124 125 /** 126 * Writes <code>len</code> bytes from the specified byte array 127 * starting at offset <code>off</code> to this output stream. 128 * The general contract for <code>write(b, off, len)</code> is that 129 * some of the bytes in the array <code>b</code> are written to the 130 * output stream in order; element <code>b[off]</code> is the first 131 * byte written and <code>b[off+len-1]</code> is the last byte written 132 * by this operation. 133 * <p> 134 * The <code>write</code> method of <code>OutputStream</code> calls 135 * the write method of one argument on each of the bytes to be 136 * written out. Subclasses are encouraged to override this method and 137 * provide a more efficient implementation. 138 * <p> 139 * If <code>b</code> is <code>null</code>, a 140 * <code>NullPointerException</code> is thrown. 141 * <p> 142 * If <code>off</code> is negative, or <code>len</code> is negative, or 143 * <code>off+len</code> is greater than the length of the array 144 * {@code b}, then an {@code IndexOutOfBoundsException} is thrown. 145 * 146 * @param b the data. 147 * @param off the start offset in the data. 148 * @param len the number of bytes to write. 149 * @exception IOException if an I/O error occurs. In particular, 150 * an <code>IOException</code> is thrown if the output 151 * stream is closed. 152 */ 153 public void write(byte[] b, int off, int len) throws IOException { 154 Objects.checkFromIndexSize(off, len, b.length); 155 // len == 0 condition implicitly handled by loop bounds 156 for (int i = 0 ; i < len ; i++) { 157 write(b[off + i]); 158 } 159 } 160 161 /** 162 * Flushes this output stream and forces any buffered output bytes 163 * to be written out. The general contract of <code>flush</code> is 164 * that calling it is an indication that, if any bytes previously 165 * written have been buffered by the implementation of the output 166 * stream, such bytes should immediately be written to their 167 * intended destination. 168 * <p> 169 * If the intended destination of this stream is an abstraction provided by 170 * the underlying operating system, for example a file, then flushing the 171 * stream guarantees only that bytes previously written to the stream are 172 * passed to the operating system for writing; it does not guarantee that 173 * they are actually written to a physical device such as a disk drive. |