--- /dev/null 2014-11-27 11:53:40.000000000 +0000 +++ new/./src/java.base/share/classes/java/io/IOUtil.java 2014-11-27 11:53:40.000000000 +0000 @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2014, 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.io; + +import java.nio.CharBuffer; + +/** + * Utility methods for copying data between a {@link InputStream} and + * {@link OutputStream} or between a {@link Readable} and a {@link Appendable}. + */ +public final class IOUtil { + + // buffer size used for reading and writing + private static final int BUFFER_SIZE = 8192; + + private IOUtil() { + throw new Error("no instances"); + } + + /** + * Reads all bytes from an input stream and writes them to an output stream. + * + * @param source the input stream to read from + * @param target the output stream to write to + * + * @return the number of bytes successfully read and written + * + * @throws IOException if an I/O error occurs when reading or writing + */ + public static long copy(InputStream source, OutputStream target) + throws IOException { + long totalRead = 0L; + byte[] buffer = new byte[BUFFER_SIZE]; + int read; + while ((read = source.read(buffer)) > -1) { + target.write(buffer, 0, read); + totalRead += read; + } + return totalRead; + } + + /** + * Reads all characters from an readable and writes them to an appendable. + * + * @param source the readable to read from + * @param target the appendable to write to + * + * @return the number of characters successfully read and written + * + * @throws IOException if an I/O error occurs when reading or writing + */ + public static long copy(Readable source, Appendable target) throws IOException { + long totalRead = 0L; + CharBuffer buffer = CharBuffer.allocate(BUFFER_SIZE); + int read; + while ((read = source.read(buffer)) > -1) { + buffer.flip(); + target.append(buffer, 0, read); + totalRead += read; + } + return totalRead; + } +}