/* * 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 appends them to a destination * in the order in which they are read. On return, the source of characters * will be at its end. *

* This method may block indefinitely while reading from the source or * writing to the destination. If the source or destination is * {@link AutoCloseable closeable}, then the behavior when either is * asynchronously closed, or the thread is interrupted during the * transfer, is highly implementation-dependent and hence unspecified. *

* If an I/O error occurs during the operation, then not all characters * might have been transferred and the source or destination could be * left in an inconsistent state. The caller of this method should therefore * ensure in such a case that measures are taken to release any resources * held by the source and destination. * * @implSpec * The default implementation invokes the read method to read all characters * from this source and invokes the {@link Appendable#append(CharSequence, int, int)} * method to write all characters to the appendable. * The total amount will be added up by after the write method has been * completed. * * The default implementation behaves as if: *

{@code
     *     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;
     * }
* * @implNote * The default implementation should usually be overridden in cases where * the implementer is already a {@link CharSequence} or its data is already * available in the internal representation in order not having the extra * overhead creating a buffer in order to transfer its data to the * destination. * * @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; } }