< prev index next >
src/java.base/share/classes/java/io/Reader.java
Print this page
*** 1,7 ****
/*
! * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
--- 1,7 ----
/*
! * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
*** 24,33 ****
--- 24,35 ----
*/
package java.io;
+ import java.util.Objects;
+
/**
* Abstract class for reading character streams. The only methods that a
* subclass must implement are read(char[], int, int) and close(). Most
* subclasses, however, will override some of the methods defined here in order
* to provide higher efficiency, additional functionality, or both.
*** 48,57 ****
--- 50,61 ----
* @since 1.1
*/
public abstract class Reader implements Readable, Closeable {
+ private static final int TRANSFER_BUFFER_SIZE = 8192;
+
/**
* The object used to synchronize operations on this stream. For
* efficiency, a character-stream object may use an object other than
* itself to protect critical sections. A subclass should therefore use
* the object in this field rather than {@code this} or a synchronized
*** 260,265 ****
--- 264,306 ----
*
* @exception IOException If an I/O error occurs
*/
public abstract void close() throws IOException;
+ /**
+ * Reads all characters from this reader and writes the characters to the
+ * given writer in the order that they are read. On return, this reader
+ * will be at end of the stream. This method does not close either reader
+ * or writer.
+ * <p>
+ * This method may block indefinitely reading from the reader, or
+ * writing to the writer. The behavior for the case where the reader
+ * and/or writer is <i>asynchronously closed</i>, or the thread
+ * interrupted during the transfer, is highly reader and writer
+ * specific, and therefore not specified.
+ * <p>
+ * If an I/O error occurs reading from the reader or writing to the
+ * writer, then it may do so after some characters have been read or
+ * written. Consequently the reader may not be at end of the stream and
+ * one, or both, streams may be in an inconsistent state. It is strongly
+ * recommended that both streams be promptly closed if an I/O error occurs.
+ *
+ * @param out the writer, non-null
+ * @return the number of characters transferred
+ * @throws IOException if an I/O error occurs when reading or writing
+ * @throws NullPointerException if {@code out} is {@code null}
+ *
+ * @since 10
+ */
+ public long transferTo(Writer out) throws IOException {
+ Objects.requireNonNull(out, "out");
+ long transferred = 0;
+ char[] buffer = new char[TRANSFER_BUFFER_SIZE];
+ int nRead;
+ while ((nRead = read(buffer, 0, TRANSFER_BUFFER_SIZE)) >= 0) {
+ out.write(buffer, 0, nRead);
+ transferred += nRead;
+ }
+ return transferred;
+ }
+
}
< prev index next >