< prev index next >
src/java.base/share/classes/java/lang/Readable.java
Print this page
*** 1,7 ****
/*
! * Copyright (c) 2003, 2013, 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) 2003, 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.lang;
import java.io.IOException;
+ import java.nio.CharBuffer;
+ import java.util.Objects;
/**
* A {@code Readable} is a source of characters. Characters from
* a {@code Readable} are made available to callers of the read
* method via a {@link java.nio.CharBuffer CharBuffer}.
*** 48,53 ****
--- 50,94 ----
* @throws IOException if an I/O error occurs
* @throws NullPointerException if cb is null
* @throws java.nio.ReadOnlyBufferException if cb is a read only buffer
*/
public int read(java.nio.CharBuffer cb) throws IOException;
+
+ /**
+ * Reads all characters from this source and writes the characters to
+ * the given appendable in the order that they are read. On return, the
+ * source of characters will be at its end.
+ * <p>
+ * This method may block indefinitely reading from the readable, or
+ * writing to the appendable. Where this source or the appendable is
+ * {@link AutoCloseable closeable}, then the behavior when either are
+ * <i>asynchronously closed</i>, or the thread interrupted during the transfer,
+ * is highly readable and appendable specific, and therefore not specified.
+ * <p>
+ * If an I/O error occurs reading from the readable or writing to the
+ * appendable, then it may do so after some characters have been read or
+ * written. Consequently the readable may not be at end of its data and
+ * one, or both participants may be in an inconsistent state. That in mind
+ * all additional measures required by one or both participants in order to
+ * eventually free their internal resources have to be taken by the caller
+ * of this method.
+ *
+ * @param out the appendable, 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 default long transferTo(Appendable out) throws IOException {
+ Objects.requireNonNull(out, "out");
+ long transferred = 0;
+ CharBuffer buffer = CharBuffer.allocate(8192);
+ int read;
+ while ((read = this.read(buffer)) >= 0) {
+ buffer.rewind();
+ out.append(buffer, 0, read);
+ transferred += read;
+ }
+ return transferred;
+ }
}
< prev index next >