X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fnet%2Fpterodactylus%2Futil%2Fio%2FStreamCopier.java;h=d1d0b11e65e4564980b6076ad87ce40d22072106;hb=7d1f2c5c2b79e6291985c35e6468fb49a2559940;hp=1d898aabea6578279e5b1ca4330e1d109270d24d;hpb=9e1fe6764276597bb486cad1c23a58d41a7972fd;p=jSite2.git
diff --git a/src/net/pterodactylus/util/io/StreamCopier.java b/src/net/pterodactylus/util/io/StreamCopier.java
index 1d898aa..d1d0b11 100644
--- a/src/net/pterodactylus/util/io/StreamCopier.java
+++ b/src/net/pterodactylus/util/io/StreamCopier.java
@@ -1,17 +1,20 @@
/*
- * 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.
+ * jSite2 - StreamCopier.java -
+ * Copyright © 2008 David Roden
+ *
+ * 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;
@@ -22,131 +25,100 @@ import java.io.InputStream;
import java.io.OutputStream;
/**
- * Copies input from an {@link InputStream} to an {@link OutputStream}.
+ * Helper class that copies bytes from an {@link InputStream} to an
+ * {@link OutputStream}.
*
- * @author David Roden
+ * @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;
+ /** Default buffer size is 64k. */
+ private static final int DEFAULT_BUFFER_SIZE = 1 << 16;
- /**
- * The size of the buffer.
- */
- private int bufferSize;
+ /** The current buffer size. */
+ private static int bufferSize = DEFAULT_BUFFER_SIZE;
/**
- * Creates a new StreamCopier with the specified parameters and the default
- * buffer size.
+ * Sets the buffer size for following transfers.
*
- * @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 new buffer size
*/
- public StreamCopier(InputStream inputStream, OutputStream outputStream, long length) {
- this(inputStream, outputStream, length, BUFFER_SIZE);
+ public static void setBufferSize(int bufferSize) {
+ StreamCopier.bufferSize = bufferSize;
}
/**
- * Creates a new StreamCopier with the specified parameters and the default
- * buffer size.
+ * Copies length
bytes from the source input stream to the
+ * destination output stream. If length
is -1
+ * as much bytes as possible will be copied (i.e. until
+ * {@link InputStream#read()} returns -1
to signal the end of
+ * the stream).
*
- * @param inputStream
- * The {@link InputStream} to read from
- * @param outputStream
- * The {@link OutputStream} to write to
+ * @param source
+ * The input stream to read from
+ * @param destination
+ * The output stream 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);
+ public static void copy(InputStream source, OutputStream destination, long length) throws IOException {
+ long remaining = length;
+ byte[] buffer = new byte[bufferSize];
+ int read = 0;
+ while ((remaining == -1) || (remaining > 0)) {
+ read = source.read(buffer, 0, ((remaining > bufferSize) || (remaining == -1)) ? bufferSize : (int) remaining);
+ if (read == -1) {
+ if (length == -1) {
+ return;
+ }
+ throw new EOFException("stream reached eof");
+ }
+ destination.write(buffer, 0, read);
+ remaining -= read;
+ }
}
/**
- * Copies length
bytes from the inputStream
to
- * the outputStream
.
+ * Copies as much bytes as possible (i.e. until {@link InputStream#read()}
+ * returns -1
) from the source input stream to the
+ * destination output stream.
*
- * @param inputStream
+ * @param source
* The input stream to read from
- * @param outputStream
+ * @param destination
* The output stream to write to
- * @param length
- * The number of bytes to copy
* @throws IOException
- * if an I/O exception occurs
+ * if an I/O error occurs
*/
- public static void copy(InputStream inputStream, OutputStream outputStream, long length) throws IOException {
- copy(inputStream, outputStream, length, BUFFER_SIZE);
+ public static void copy(InputStream source, OutputStream destination) throws IOException {
+ copy(source, destination, -1);
}
/**
- * Copies length
bytes from the inputStream
to
- * the outputStream
using a buffer with the specified size
+ * Finds the length of the input stream by reading until
+ * {@link InputStream#read(byte[])} returns -1
.
*
- * @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
+ * @param source
+ * The input stream to measure
+ * @return The length of the input stream in bytes
* @throws IOException
- * if an I/O exception occurs
+ * if an I/O error occurs
*/
- public static void copy(InputStream inputStream, OutputStream outputStream, long length, int bufferSize) throws IOException {
- long remaining = length;
+ public static long findLength(InputStream source) throws IOException {
+ long length = 0;
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();
+ int read = 0;
+ while (read != -1) {
+ read = source.read(buffer);
+ if (read != -1) {
+ length += read;
}
- outputStream.write(buffer, 0, read);
- remaining -= read;
}
+ return length;
}
}