From: David ‘Bombe’ Roden Date: Tue, 28 Aug 2012 08:12:59 +0000 (+0200) Subject: Use StreamCopier from utils. X-Git-Tag: 0.11^2~19^2~1 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=fbfe4707a1e3e9f23c8e1904d1a0eb0c3fbf587f;p=jSite.git Use StreamCopier from utils. --- diff --git a/pom.xml b/pom.xml index 6fa03e2..574c52b 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ net.pterodactylus utils - 0.12 + 0.12.1-SNAPSHOT diff --git a/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java b/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java index b5f2e16..a9ea8d1 100644 --- a/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java +++ b/src/main/java/de/todesbaum/jsite/application/ProjectInserter.java @@ -34,6 +34,7 @@ import java.util.concurrent.CountDownLatch; import java.util.logging.Level; import java.util.logging.Logger; +import net.pterodactylus.util.io.StreamCopier.ProgressListener; import de.todesbaum.jsite.gui.FileScanner; import de.todesbaum.jsite.gui.FileScanner.ScannedFile; import de.todesbaum.jsite.gui.FileScannerListener; @@ -47,7 +48,6 @@ import de.todesbaum.util.freenet.fcp2.Message; import de.todesbaum.util.freenet.fcp2.PriorityClass; import de.todesbaum.util.freenet.fcp2.RedirectFileEntry; import de.todesbaum.util.freenet.fcp2.Verbosity; -import de.todesbaum.util.io.StreamCopier.ProgressListener; /** * Manages project inserts. diff --git a/src/main/java/de/todesbaum/jsite/gui/FileScanner.java b/src/main/java/de/todesbaum/jsite/gui/FileScanner.java index 75dafe0..aa07943 100644 --- a/src/main/java/de/todesbaum/jsite/gui/FileScanner.java +++ b/src/main/java/de/todesbaum/jsite/gui/FileScanner.java @@ -34,9 +34,9 @@ import java.util.logging.Level; import java.util.logging.Logger; import net.pterodactylus.util.io.Closer; +import net.pterodactylus.util.io.StreamCopier; import de.todesbaum.jsite.application.Project; import de.todesbaum.jsite.i18n.I18n; -import de.todesbaum.util.io.StreamCopier; /** * Scans the local path of a project anychronously and returns the list of found diff --git a/src/main/java/de/todesbaum/jsite/gui/ProjectInsertPage.java b/src/main/java/de/todesbaum/jsite/gui/ProjectInsertPage.java index bfd8b89..2a3e5e5 100644 --- a/src/main/java/de/todesbaum/jsite/gui/ProjectInsertPage.java +++ b/src/main/java/de/todesbaum/jsite/gui/ProjectInsertPage.java @@ -47,6 +47,7 @@ import javax.swing.JProgressBar; import javax.swing.JTextField; import javax.swing.SwingUtilities; +import net.pterodactylus.util.io.StreamCopier.ProgressListener; import de.todesbaum.jsite.application.AbortedException; import de.todesbaum.jsite.application.Freenet7Interface; import de.todesbaum.jsite.application.InsertListener; @@ -56,7 +57,6 @@ import de.todesbaum.jsite.i18n.I18n; import de.todesbaum.jsite.i18n.I18nContainer; import de.todesbaum.util.freenet.fcp2.ClientPutDir.ManifestPutter; import de.todesbaum.util.freenet.fcp2.PriorityClass; -import de.todesbaum.util.io.StreamCopier.ProgressListener; import de.todesbaum.util.swing.TWizard; import de.todesbaum.util.swing.TWizardPage; diff --git a/src/main/java/de/todesbaum/jsite/main/CLI.java b/src/main/java/de/todesbaum/jsite/main/CLI.java index 65c9253..0cf9cc4 100644 --- a/src/main/java/de/todesbaum/jsite/main/CLI.java +++ b/src/main/java/de/todesbaum/jsite/main/CLI.java @@ -20,12 +20,12 @@ package de.todesbaum.jsite.main; import java.io.PrintWriter; +import net.pterodactylus.util.io.StreamCopier.ProgressListener; import de.todesbaum.jsite.application.Freenet7Interface; import de.todesbaum.jsite.application.InsertListener; import de.todesbaum.jsite.application.Node; import de.todesbaum.jsite.application.Project; import de.todesbaum.jsite.application.ProjectInserter; -import de.todesbaum.util.io.StreamCopier.ProgressListener; /** * Command-line interface for jSite. diff --git a/src/main/java/de/todesbaum/jsite/main/Configuration.java b/src/main/java/de/todesbaum/jsite/main/Configuration.java index e3191de..28d4db0 100644 --- a/src/main/java/de/todesbaum/jsite/main/Configuration.java +++ b/src/main/java/de/todesbaum/jsite/main/Configuration.java @@ -36,6 +36,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import net.pterodactylus.util.io.Closer; +import net.pterodactylus.util.io.StreamCopier; import net.pterodactylus.util.xml.SimpleXML; import net.pterodactylus.util.xml.XML; import de.todesbaum.jsite.application.FileOption; @@ -44,7 +45,6 @@ import de.todesbaum.jsite.application.Project; import de.todesbaum.jsite.main.ConfigurationLocator.ConfigurationLocation; import de.todesbaum.util.freenet.fcp2.ClientPutDir.ManifestPutter; import de.todesbaum.util.freenet.fcp2.PriorityClass; -import de.todesbaum.util.io.StreamCopier; /** * The configuration. diff --git a/src/main/java/de/todesbaum/util/freenet/fcp2/Client.java b/src/main/java/de/todesbaum/util/freenet/fcp2/Client.java index ede92d1..8e819d3 100644 --- a/src/main/java/de/todesbaum/util/freenet/fcp2/Client.java +++ b/src/main/java/de/todesbaum/util/freenet/fcp2/Client.java @@ -22,7 +22,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -import de.todesbaum.util.io.StreamCopier.ProgressListener; +import net.pterodactylus.util.io.StreamCopier.ProgressListener; /** * A Client executes {@link Command}s over a {@link Connection} to a diff --git a/src/main/java/de/todesbaum/util/freenet/fcp2/Connection.java b/src/main/java/de/todesbaum/util/freenet/fcp2/Connection.java index e7e7ad9..ff11cdf 100644 --- a/src/main/java/de/todesbaum/util/freenet/fcp2/Connection.java +++ b/src/main/java/de/todesbaum/util/freenet/fcp2/Connection.java @@ -31,9 +31,9 @@ import java.util.ArrayList; import java.util.List; import net.pterodactylus.util.io.Closer; +import net.pterodactylus.util.io.StreamCopier; +import net.pterodactylus.util.io.StreamCopier.ProgressListener; import de.todesbaum.util.io.LineInputStream; -import de.todesbaum.util.io.StreamCopier; -import de.todesbaum.util.io.StreamCopier.ProgressListener; import de.todesbaum.util.io.TempFileInputStream; /** @@ -268,7 +268,7 @@ public class Connection { InputStream payloadInputStream = null; try { payloadInputStream = command.getPayload(); - StreamCopier.copy(payloadInputStream, nodeOutputStream, command.getPayloadLength(), progressListener); + StreamCopier.copy(payloadInputStream, nodeOutputStream, progressListener, command.getPayloadLength()); } finally { Closer.close(payloadInputStream); } diff --git a/src/main/java/de/todesbaum/util/io/StreamCopier.java b/src/main/java/de/todesbaum/util/io/StreamCopier.java deleted file mode 100644 index c7d99de..0000000 --- a/src/main/java/de/todesbaum/util/io/StreamCopier.java +++ /dev/null @@ -1,235 +0,0 @@ -/* - * jSite - StreamCopier.java - Copyright © 2006–2012 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 de.todesbaum.util.io; - -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$ - */ -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 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 - * 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. - * - * @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 - * 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 { - 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) { - 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; - 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); - - } - -}