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.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.Objects;
32
33 /**
34 * This abstract class is the superclass of all classes representing
35 * an input stream of bytes.
36 *
37 * <p> Applications that need to define a subclass of <code>InputStream</code>
38 * must always provide a method that returns the next byte of input.
39 *
40 * @author Arthur van Hoff
41 * @see java.io.BufferedInputStream
42 * @see java.io.ByteArrayInputStream
43 * @see java.io.DataInputStream
44 * @see java.io.FilterInputStream
45 * @see java.io.InputStream#read()
46 * @see java.io.OutputStream
47 * @see java.io.PushbackInputStream
48 * @since 1.0
49 */
50 public abstract class InputStream implements Closeable {
51
525 * writing to the output stream. The behavior for the case where the input
526 * and/or output stream is <i>asynchronously closed</i>, or the thread
527 * interrupted during the transfer, is highly input and output stream
528 * specific, and therefore not specified.
529 * <p>
530 * If an I/O error occurs reading from the input stream or writing to the
531 * output stream, then it may do so after some bytes have been read or
532 * written. Consequently the input stream may not be at end of stream and
533 * one, or both, streams may be in an inconsistent state. It is strongly
534 * recommended that both streams be promptly closed if an I/O error occurs.
535 *
536 * @param out the output stream, non-null
537 * @return the number of bytes transferred
538 * @throws IOException if an I/O error occurs when reading or writing
539 * @throws NullPointerException if {@code out} is {@code null}
540 *
541 * @since 9
542 */
543 public long transferTo(OutputStream out) throws IOException {
544 Objects.requireNonNull(out, "out");
545 long transferred = 0;
546 byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
547 int read;
548 while ((read = this.read(buffer, 0, DEFAULT_BUFFER_SIZE)) >= 0) {
549 out.write(buffer, 0, read);
550 transferred += read;
551 }
552 return transferred;
553 }
554 }
|
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.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.Objects;
32 import jdk.internal.io.IOSupport;
33
34 /**
35 * This abstract class is the superclass of all classes representing
36 * an input stream of bytes.
37 *
38 * <p> Applications that need to define a subclass of <code>InputStream</code>
39 * must always provide a method that returns the next byte of input.
40 *
41 * @author Arthur van Hoff
42 * @see java.io.BufferedInputStream
43 * @see java.io.ByteArrayInputStream
44 * @see java.io.DataInputStream
45 * @see java.io.FilterInputStream
46 * @see java.io.InputStream#read()
47 * @see java.io.OutputStream
48 * @see java.io.PushbackInputStream
49 * @since 1.0
50 */
51 public abstract class InputStream implements Closeable {
52
526 * writing to the output stream. The behavior for the case where the input
527 * and/or output stream is <i>asynchronously closed</i>, or the thread
528 * interrupted during the transfer, is highly input and output stream
529 * specific, and therefore not specified.
530 * <p>
531 * If an I/O error occurs reading from the input stream or writing to the
532 * output stream, then it may do so after some bytes have been read or
533 * written. Consequently the input stream may not be at end of stream and
534 * one, or both, streams may be in an inconsistent state. It is strongly
535 * recommended that both streams be promptly closed if an I/O error occurs.
536 *
537 * @param out the output stream, non-null
538 * @return the number of bytes transferred
539 * @throws IOException if an I/O error occurs when reading or writing
540 * @throws NullPointerException if {@code out} is {@code null}
541 *
542 * @since 9
543 */
544 public long transferTo(OutputStream out) throws IOException {
545 Objects.requireNonNull(out, "out");
546 return IOSupport.copy(this, out);
547 }
548 }
|