From 18df3b41f730c570d7154b5763b8bd2ffeed4300 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 10 Apr 2013 22:32:58 +0200 Subject: [PATCH] Measure and expose the bandwidth of the download. --- .../java/net/pterodactylus/irc/DccReceiver.java | 28 +++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/pterodactylus/irc/DccReceiver.java b/src/main/java/net/pterodactylus/irc/DccReceiver.java index 39f236c..88d3b64 100644 --- a/src/main/java/net/pterodactylus/irc/DccReceiver.java +++ b/src/main/java/net/pterodactylus/irc/DccReceiver.java @@ -22,11 +22,14 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; +import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; import net.pterodactylus.irc.event.DccSendReceived; +import net.pterodactylus.xdcc.util.io.BandwidthCountingInputStream; +import com.google.common.io.Closeables; import com.google.common.util.concurrent.AbstractExecutionThreadService; /** @@ -57,6 +60,9 @@ public class DccReceiver extends AbstractExecutionThreadService { /** The number of bytes already written. */ private long progress; + /** The bandwidth-measuring input stream. */ + private BandwidthCountingInputStream inputStream; + /** * Creates a new DCC receiver. * @@ -114,6 +120,24 @@ public class DccReceiver extends AbstractExecutionThreadService { return progress; } + /** + * Returns the current rate of the download. + * + * @return The current rate of the download, in bytes/second + */ + public long getCurrentRate() { + return inputStream.getCurrentRate(); + } + + /** + * Returns the overall rate of the download. + * + * @return The overall rate of the download, in bytes/second + */ + public long getOverallRate() { + return inputStream.getOverallRate(); + } + // // ABSTRACTEXECUTIONTHREADSERVICE METHODS // @@ -124,9 +148,10 @@ public class DccReceiver extends AbstractExecutionThreadService { try { socket = new Socket(inetAddress, port); InputStream socketInputStream = socket.getInputStream(); + inputStream = new BandwidthCountingInputStream(socketInputStream, 5, TimeUnit.SECONDS); byte[] buffer = new byte[65536]; while (true) { - int r = socketInputStream.read(buffer); + int r = inputStream.read(buffer); if (r == -1) { /* yay, eof! */ break; @@ -137,6 +162,7 @@ public class DccReceiver extends AbstractExecutionThreadService { } catch (IOException ioe1) { logger.log(Level.WARNING, "I/O error while receiving DCC!", ioe1); } finally { + Closeables.close(inputStream, true); socket.close(); } } -- 2.7.4