1 /*
2 * Copyright (c) 1994, 2004, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.io;
27
28 /**
29 * This abstract class is the superclass of all classes representing
30 * an output stream of bytes. An output stream accepts output bytes
31 * and sends them to some sink.
32 * <p>
33 * Applications that need to define a subclass of
34 * <code>OutputStream</code> must always provide at least a method
35 * that writes one byte of output.
36 *
37 * @author Arthur van Hoff
38 * @see java.io.BufferedOutputStream
39 * @see java.io.ByteArrayOutputStream
40 * @see java.io.DataOutputStream
41 * @see java.io.FilterOutputStream
42 * @see java.io.InputStream
43 * @see java.io.OutputStream#write(int)
44 * @since 1.0
45 */
46 public abstract class OutputStream implements Closeable, Flushable {
47 /**
87 * The <code>write</code> method of <code>OutputStream</code> calls
88 * the write method of one argument on each of the bytes to be
89 * written out. Subclasses are encouraged to override this method and
90 * provide a more efficient implementation.
91 * <p>
92 * If <code>b</code> is <code>null</code>, a
93 * <code>NullPointerException</code> is thrown.
94 * <p>
95 * If <code>off</code> is negative, or <code>len</code> is negative, or
96 * <code>off+len</code> is greater than the length of the array
97 * {@code b}, then an {@code IndexOutOfBoundsException} is thrown.
98 *
99 * @param b the data.
100 * @param off the start offset in the data.
101 * @param len the number of bytes to write.
102 * @exception IOException if an I/O error occurs. In particular,
103 * an <code>IOException</code> is thrown if the output
104 * stream is closed.
105 */
106 public void write(byte b[], int off, int len) throws IOException {
107 if (b == null) {
108 throw new NullPointerException();
109 } else if ((off < 0) || (off > b.length) || (len < 0) ||
110 ((off + len) > b.length) || ((off + len) < 0)) {
111 throw new IndexOutOfBoundsException();
112 } else if (len == 0) {
113 return;
114 }
115 for (int i = 0 ; i < len ; i++) {
116 write(b[off + i]);
117 }
118 }
119
120 /**
121 * Flushes this output stream and forces any buffered output bytes
122 * to be written out. The general contract of <code>flush</code> is
123 * that calling it is an indication that, if any bytes previously
124 * written have been buffered by the implementation of the output
125 * stream, such bytes should immediately be written to their
126 * intended destination.
127 * <p>
128 * If the intended destination of this stream is an abstraction provided by
129 * the underlying operating system, for example a file, then flushing the
130 * stream guarantees only that bytes previously written to the stream are
|
1 /*
2 * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.io;
27
28 import java.util.Objects;
29
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 /**
89 * The <code>write</code> method of <code>OutputStream</code> calls
90 * the write method of one argument on each of the bytes to be
91 * written out. Subclasses are encouraged to override this method and
92 * provide a more efficient implementation.
93 * <p>
94 * If <code>b</code> is <code>null</code>, a
95 * <code>NullPointerException</code> is thrown.
96 * <p>
97 * If <code>off</code> is negative, or <code>len</code> is negative, or
98 * <code>off+len</code> is greater than the length of the array
99 * {@code b}, then an {@code IndexOutOfBoundsException} is thrown.
100 *
101 * @param b the data.
102 * @param off the start offset in the data.
103 * @param len the number of bytes to write.
104 * @exception IOException if an I/O error occurs. In particular,
105 * an <code>IOException</code> is thrown if the output
106 * stream is closed.
107 */
108 public void write(byte b[], int off, int len) throws IOException {
109 Objects.requireNonNull(b);
110 if (off < 0 || len < 0 || len > b.length - off) {
111 throw new IndexOutOfBoundsException();
112 } else if (len == 0) {
113 return;
114 }
115 for (int i = 0 ; i < len ; i++) {
116 write(b[off + i]);
117 }
118 }
119
120 /**
121 * Flushes this output stream and forces any buffered output bytes
122 * to be written out. The general contract of <code>flush</code> is
123 * that calling it is an indication that, if any bytes previously
124 * written have been buffered by the implementation of the output
125 * stream, such bytes should immediately be written to their
126 * intended destination.
127 * <p>
128 * If the intended destination of this stream is an abstraction provided by
129 * the underlying operating system, for example a file, then flushing the
130 * stream guarantees only that bytes previously written to the stream are
|