1 /* |
1 /* |
2 * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
44 * @see java.io.InputStream |
44 * @see java.io.InputStream |
45 * @see java.io.OutputStream#write(int) |
45 * @see java.io.OutputStream#write(int) |
46 * @since 1.0 |
46 * @since 1.0 |
47 */ |
47 */ |
48 public abstract class OutputStream implements Closeable, Flushable { |
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 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 |
49 /** |
94 /** |
50 * Writes the specified byte to this output stream. The general |
95 * Writes the specified byte to this output stream. The general |
51 * contract for <code>write</code> is that one byte is written |
96 * contract for <code>write</code> is that one byte is written |
52 * to the output stream. The byte to be written is the eight |
97 * to the output stream. The byte to be written is the eight |
53 * low-order bits of the argument <code>b</code>. The 24 |
98 * low-order bits of the argument <code>b</code>. The 24 |
104 * @exception IOException if an I/O error occurs. In particular, |
149 * @exception IOException if an I/O error occurs. In particular, |
105 * an <code>IOException</code> is thrown if the output |
150 * an <code>IOException</code> is thrown if the output |
106 * stream is closed. |
151 * stream is closed. |
107 */ |
152 */ |
108 public void write(byte b[], int off, int len) throws IOException { |
153 public void write(byte b[], int off, int len) throws IOException { |
109 Objects.requireNonNull(b); |
|
110 Objects.checkFromIndexSize(off, len, b.length); |
154 Objects.checkFromIndexSize(off, len, b.length); |
111 // len == 0 condition implicitly handled by loop bounds |
155 // len == 0 condition implicitly handled by loop bounds |
112 for (int i = 0 ; i < len ; i++) { |
156 for (int i = 0 ; i < len ; i++) { |
113 write(b[off + i]); |
157 write(b[off + i]); |
114 } |
158 } |