From 9e1fe6764276597bb486cad1c23a58d41a7972fd Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 5 Apr 2008 21:01:26 +0000 Subject: [PATCH] add stream copier git-svn-id: http://trooper/svn/projects/jSite/trunk@614 c3eda9e8-030b-0410-8277-bc7414b0a119 --- src/net/pterodactylus/util/io/StreamCopier.java | 152 ++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 src/net/pterodactylus/util/io/StreamCopier.java diff --git a/src/net/pterodactylus/util/io/StreamCopier.java b/src/net/pterodactylus/util/io/StreamCopier.java new file mode 100644 index 0000000..1d898aa --- /dev/null +++ b/src/net/pterodactylus/util/io/StreamCopier.java @@ -0,0 +1,152 @@ +/* + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * This program 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 for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package net.pterodactylus.util.io; + +import java.io.EOFException; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * Copies input from an {@link InputStream} to an {@link OutputStream}. + * + * @author David Roden + * @version $Id$ + */ +public class StreamCopier { + + /** + * The default size of the buffer. + */ + private static final int BUFFER_SIZE = 64 * 1024; + + /** + * The {@link InputStream} to read from. + */ + private InputStream inputStream; + + /** + * The {@link OutputStream} to write to. + */ + private OutputStream outputStream; + + /** + * The number of bytes to copy. + */ + private long length; + + /** + * The size of the buffer. + */ + private int bufferSize; + + /** + * Creates a new StreamCopier with the specified parameters and the default + * buffer size. + * + * @param inputStream + * The {@link InputStream} to read from + * @param outputStream + * The {@link OutputStream} to write to + * @param length + * The number of bytes to copy + */ + public StreamCopier(InputStream inputStream, OutputStream outputStream, long length) { + this(inputStream, outputStream, length, BUFFER_SIZE); + } + + /** + * Creates a new StreamCopier with the specified parameters and the default + * buffer size. + * + * @param inputStream + * The {@link InputStream} to read from + * @param outputStream + * The {@link OutputStream} to write to + * @param length + * The number of bytes to copy + * @param bufferSize + * The number of bytes to copy at a time + */ + public StreamCopier(InputStream inputStream, OutputStream outputStream, long length, int bufferSize) { + this.inputStream = inputStream; + this.outputStream = outputStream; + this.length = length; + this.bufferSize = bufferSize; + } + + /** + * Copies the stream data. If the input stream is depleted before the + * requested number of bytes have been read an {@link EOFException} is + * thrown. + * + * @throws EOFException + * if the input stream is depleted before the requested number + * of bytes has been read + * @throws IOException + * if an I/O error occurs + */ + public void copy() throws EOFException, IOException { + copy(inputStream, outputStream, length, bufferSize); + } + + /** + * Copies length bytes from the inputStream to + * the outputStream. + * + * @param inputStream + * The input stream to read from + * @param outputStream + * The output stream to write to + * @param length + * The number of bytes to copy + * @throws IOException + * if an I/O exception occurs + */ + public static void copy(InputStream inputStream, OutputStream outputStream, long length) throws IOException { + copy(inputStream, outputStream, length, BUFFER_SIZE); + } + + /** + * Copies length bytes from the inputStream to + * the outputStream using a buffer with the specified size + * + * @param inputStream + * The input stream to read from + * @param outputStream + * The output stream to write to + * @param length + * The number of bytes to copy + * @param bufferSize + * The size of the copy buffer + * @throws IOException + * if an I/O exception occurs + */ + public static void copy(InputStream inputStream, OutputStream outputStream, long length, int bufferSize) throws IOException { + long remaining = length; + byte[] buffer = new byte[bufferSize]; + while (remaining > 0) { + int read = inputStream.read(buffer, 0, (int) Math.min(Integer.MAX_VALUE, Math.min(bufferSize, remaining))); + if (read == -1) { + throw new EOFException(); + } + outputStream.write(buffer, 0, read); + remaining -= read; + } + } + +} -- 2.7.4