From: David ‘Bombe’ Roden Date: Wed, 10 Apr 2013 20:15:34 +0000 (+0200) Subject: Add input stream that counts its consumed bandwidth. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=9b7afa0998062127271320893951dfded32cc22a;p=xudocci.git Add input stream that counts its consumed bandwidth. --- diff --git a/src/main/java/net/pterodactylus/xdcc/util/io/BandwidthCountingInputStream.java b/src/main/java/net/pterodactylus/xdcc/util/io/BandwidthCountingInputStream.java new file mode 100644 index 0000000..5009cda --- /dev/null +++ b/src/main/java/net/pterodactylus/xdcc/util/io/BandwidthCountingInputStream.java @@ -0,0 +1,128 @@ +/* + * 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 . + */ + +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 David ‘Bombe’ Roden + */ +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} + *

+ * 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(); + } + +}