Add bandwidth counting output stream.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 2 May 2013 19:11:59 +0000 (21:11 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 2 May 2013 19:11:59 +0000 (21:11 +0200)
src/main/java/net/pterodactylus/xdcc/util/io/BandwidthCountingOutputStream.java [new file with mode: 0644]

diff --git a/src/main/java/net/pterodactylus/xdcc/util/io/BandwidthCountingOutputStream.java b/src/main/java/net/pterodactylus/xdcc/util/io/BandwidthCountingOutputStream.java
new file mode 100644 (file)
index 0000000..1ad3c4b
--- /dev/null
@@ -0,0 +1,132 @@
+/*
+ * XdccDownloader - BandwidthCountingInputStream.java - Copyright © 2013 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.xdcc.util.io;
+
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * An {@link OutputStream} that can calculate the bandwidth consumed by its
+ * wrapped output stream.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class BandwidthCountingOutputStream extends FilterOutputStream {
+
+       /** The accumulating data counter. */
+       private final AccumulatingDataCounter accumulatingDataCounter;
+
+       /**
+        * Creates a new bandwidth counting output stream.
+        *
+        * @param outputStream
+        *              The output stream to wrap
+        */
+       public BandwidthCountingOutputStream(OutputStream outputStream) {
+               super(outputStream);
+               accumulatingDataCounter = new AccumulatingDataCounter();
+       }
+
+       /**
+        * Creates a new bandwidth counting output stream.
+        *
+        * @param outputStream
+        *              The output stream to wrap
+        * @param maximumLifeTime
+        *              The maximum lifetime of the bandwidth counter
+        * @param timeUnit
+        *              The time unit of the lifetime
+        */
+       public BandwidthCountingOutputStream(OutputStream outputStream, long maximumLifeTime, TimeUnit timeUnit) {
+               super(outputStream);
+               accumulatingDataCounter = new AccumulatingDataCounter(maximumLifeTime, timeUnit);
+       }
+
+       //
+       // ACTIONS
+       //
+
+       /**
+        * Returns the current rate of this input stream, averaged over the maximum
+        * lifetime of the counter data.
+        *
+        * @return The current rate of this input stream, in bytes/second
+        */
+       public long getCurrentRate() {
+               return accumulatingDataCounter.getCurrentRate();
+       }
+
+       /**
+        * Returns the current rate of this input stream, averaged over the given
+        * amount of milliseconds.
+        *
+        * @param millis
+        *              The number of millis to average the bandwidth over
+        * @return The current rate of this input stream, in bytes/second
+        */
+       public long getCurrentRate(long millis) {
+               return accumulatingDataCounter.getCurrentRate(millis);
+       }
+
+       /**
+        * Returns the overall rate of this input stream, averaged over the total
+        * lifetime.
+        *
+        * @return The overall rate of this input stream, in bytes/second
+        */
+       public long getOverallRate() {
+               return accumulatingDataCounter.getOverallRate();
+       }
+
+       //
+       // OUTPUTSTREAM METHODS
+       //
+
+       @Override
+       public void write(int b) throws IOException {
+               super.write(b);
+               accumulatingDataCounter.count(1);
+       }
+
+       @Override
+       public void write(byte[] buffer) throws IOException {
+               write(buffer, 0, buffer.length);
+       }
+
+       @Override
+       public void write(byte[] buffer, int offset, int length) throws IOException {
+               super.write(buffer, offset, length);
+               accumulatingDataCounter.count(length);
+       }
+
+       /**
+        * {@inheritDoc}
+        * <p/>
+        * This method also {@link AccumulatingDataCounter#stop()}s the bandwidth
+        * counter; subsequent calls of {@link #getOverallRate()} will always return
+        * the same value.
+        */
+       @Override
+       public void close() throws IOException {
+               accumulatingDataCounter.stop();
+               super.close();
+       }
+
+}