X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fde%2Ftodesbaum%2Futil%2Fio%2FStreamCopier.java;h=ee87c19c3d6100c6ec7d8856f14da97b4d5fe9f1;hb=7dee6ec51d770004481ef6f4eab96e8f03fe0af9;hp=982d556ce5aef525f2d0c33a537f663d01aa4bbd;hpb=6f1a8216cfba28add0ef365b46a08d16d4eb87fe;p=jSite.git
diff --git a/src/de/todesbaum/util/io/StreamCopier.java b/src/de/todesbaum/util/io/StreamCopier.java
index 982d556..ee87c19 100644
--- a/src/de/todesbaum/util/io/StreamCopier.java
+++ b/src/de/todesbaum/util/io/StreamCopier.java
@@ -3,12 +3,12 @@
* 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.
@@ -20,12 +20,13 @@ import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.util.EventListener;
/**
* Copies input from an {@link InputStream} to an {@link OutputStream}.
- *
+ *
* @author David Roden
- * @version $Id: StreamCopier.java 428 2006-03-29 18:03:36Z bombe $
+ * @version $Id$
*/
public class StreamCopier {
@@ -57,7 +58,7 @@ public class StreamCopier {
/**
* Creates a new StreamCopier with the specified parameters and the default
* buffer size.
- *
+ *
* @param inputStream
* The {@link InputStream} to read from
* @param outputStream
@@ -72,7 +73,7 @@ public class StreamCopier {
/**
* Creates a new StreamCopier with the specified parameters and the default
* buffer size.
- *
+ *
* @param inputStream
* The {@link InputStream} to read from
* @param outputStream
@@ -93,7 +94,7 @@ public class StreamCopier {
* 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
@@ -105,9 +106,26 @@ public class StreamCopier {
}
/**
+ * Copies the stream data. If the input stream is depleted before the
+ * requested number of bytes have been read an {@link EOFException} is
+ * thrown.
+ *
+ * @param progressListener
+ * The progress listener (may be {@code null})
+ * @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(ProgressListener progressListener) throws EOFException, IOException {
+ copy(inputStream, outputStream, length, bufferSize, progressListener);
+ }
+
+ /**
* Copies length
bytes from the inputStream
to
* the outputStream
.
- *
+ *
* @param inputStream
* The input stream to read from
* @param outputStream
@@ -123,8 +141,27 @@ public class StreamCopier {
/**
* 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
+ * @param progressListener
+ * The progress listener (may be {@code null})
+ * @throws IOException
+ * if an I/O exception occurs
+ */
+ public static void copy(InputStream inputStream, OutputStream outputStream, long length, ProgressListener progressListener) throws IOException {
+ copy(inputStream, outputStream, length, BUFFER_SIZE, progressListener);
+ }
+
+ /**
+ * 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
@@ -137,6 +174,27 @@ public class StreamCopier {
* if an I/O exception occurs
*/
public static void copy(InputStream inputStream, OutputStream outputStream, long length, int bufferSize) throws IOException {
+ copy(inputStream, outputStream, length, bufferSize, null);
+ }
+
+ /**
+ * 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
+ * @param progressListener
+ * The progress listener (may be {@code null})
+ * @throws IOException
+ * if an I/O exception occurs
+ */
+ public static void copy(InputStream inputStream, OutputStream outputStream, long length, int bufferSize, ProgressListener progressListener) throws IOException {
long remaining = length;
byte[] buffer = new byte[bufferSize];
while (remaining > 0) {
@@ -146,7 +204,30 @@ public class StreamCopier {
}
outputStream.write(buffer, 0, read);
remaining -= read;
+ if (progressListener != null) {
+ progressListener.onProgress(length - remaining, length);
+ }
}
}
+ /**
+ * Interface for objects that want to be notified about the progress of a
+ * {@link StreamCopier#copy()} operation.
+ *
+ * @author David âBombeâ Roden <bombe@freenetproject.org>
+ */
+ public static interface ProgressListener extends EventListener {
+
+ /**
+ * Notifiies a listener that a copy process made some progress.
+ *
+ * @param copied
+ * The number of bytes that have already been copied
+ * @param length
+ * The total number of bytes that will be copied
+ */
+ public void onProgress(long copied, long length);
+
+ }
+
}