2 * XdccDownloader - BandwidthCountingInputStream.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.xdcc.util.io;
20 import java.io.FilterInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.concurrent.TimeUnit;
26 * An {@link InputStream} that can calculate the bandwidth consumed by its
27 * wrapped input stream.
29 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31 public class BandwidthCountingInputStream extends FilterInputStream {
33 /** The accumulating data counter. */
34 private final AccumulatingDataCounter accumulatingDataCounter;
37 * Creates a new bandwidth counting input stream.
40 * The input stream to wrap
42 public BandwidthCountingInputStream(InputStream inputStream) {
44 accumulatingDataCounter = new AccumulatingDataCounter();
48 * Creates a new bandwidth counting input stream.
51 * The input stream to wrap
52 * @param maximumLifeTime
53 * The maximum lifetime of the bandwidth counter
55 * The time unit of the lifetime
57 public BandwidthCountingInputStream(InputStream inputStream, long maximumLifeTime, TimeUnit timeUnit) {
59 accumulatingDataCounter = new AccumulatingDataCounter(maximumLifeTime, timeUnit);
67 * Returns the current rate of this input stream, averaged over the maximum
68 * lifetime of the counter data.
70 * @return The current rate of this input stream, in bytes/second
72 public long getCurrentRate() {
73 return accumulatingDataCounter.getCurrentRate();
77 * Returns the current rate of this input stream, averaged over the given
78 * amount of milliseconds.
81 * The number of millis to average the bandwidth over
82 * @return The current rate of this input stream, in bytes/second
84 public long getCurrentRate(long millis) {
85 return accumulatingDataCounter.getCurrentRate(millis);
89 * Returns the overall rate of this input stream, averaged over the total
92 * @return The overall rate of this input stream, in bytes/second
94 public long getOverallRate() {
95 return accumulatingDataCounter.getOverallRate();
99 // INPUTSTREAM METHODS
103 public int read() throws IOException {
104 int r = super.read();
106 accumulatingDataCounter.count(1);
112 public int read(byte[] buffer) throws IOException {
113 return read(buffer, 0, buffer.length);
117 public int read(byte[] buffer, int offset, int length) throws IOException {
118 int r = super.read(buffer, offset, length);
120 accumulatingDataCounter.count(r);
128 * This method also {@link AccumulatingDataCounter#stop()}s the bandwidth
129 * counter; subsequent calls of {@link #getOverallRate()} will always return
133 public void close() throws IOException {
134 accumulatingDataCounter.stop();