/* * 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 * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ 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}. * * @since 1.5 */ public interface Readable { /** * Attempts to read characters into the specified character buffer. * The buffer is used as a repository of characters as-is: the only * changes made are the results of a put operation. No flipping or * rewinding of the buffer is performed. * * @param cb the buffer to read characters into * @return The number of {@code char} values added to the buffer, * or -1 if this source of characters is at its end * @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. *

* 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 * asynchronously closed, or the thread interrupted during the transfer, * is highly readable and appendable specific, and therefore not specified. *

* 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; } }