--- /dev/null
+/*
+ * 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.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * An {@link InputStream} that can calculate the bandwidth consumed by its
+ * wrapped input stream.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class BandwidthCountingInputStream extends FilterInputStream {
+
+ /** The accumulating data counter. */
+ private final AccumulatingDataCounter accumulatingDataCounter;
+
+ /**
+ * Creates a new bandwidth counting input stream.
+ *
+ * @param inputStream
+ * The input stream to wrap
+ */
+ public BandwidthCountingInputStream(InputStream inputStream) {
+ super(inputStream);
+ accumulatingDataCounter = new AccumulatingDataCounter();
+ }
+
+ /**
+ * Creates a new bandwidth counting input stream.
+ *
+ * @param inputStream
+ * The input stream to wrap
+ * @param maximumLifeTime
+ * The maximum lifetime of the bandwidth counter
+ * @param timeUnit
+ * The time unit of the lifetime
+ */
+ public BandwidthCountingInputStream(InputStream inputStream, long maximumLifeTime, TimeUnit timeUnit) {
+ super(inputStream);
+ accumulatingDataCounter = new AccumulatingDataCounter(maximumLifeTime, timeUnit);
+ }
+
+ //
+ // ACTIONS
+ //
+
+ /**
+ * 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();
+ }
+
+ //
+ // INPUTSTREAM METHODS
+ //
+
+ @Override
+ public int read() throws IOException {
+ int r = super.read();
+ if (r != -1) {
+ accumulatingDataCounter.count(1);
+ }
+ return r;
+ }
+
+ @Override
+ public int read(byte[] buffer) throws IOException {
+ return read(buffer, 0, buffer.length);
+ }
+
+ @Override
+ public int read(byte[] buffer, int offset, int length) throws IOException {
+ int r = super.read(buffer, offset, length);
+ if (r != -1) {
+ accumulatingDataCounter.count(r);
+ }
+ return r;
+ }
+
+ /**
+ * {@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();
+ }
+
+}