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.FilterOutputStream;
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.util.concurrent.TimeUnit;
26 * An {@link OutputStream} that can calculate the bandwidth consumed by its
27 * wrapped output stream.
29 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31 public class BandwidthCountingOutputStream extends FilterOutputStream {
33 /** The accumulating data counter. */
34 private final AccumulatingDataCounter accumulatingDataCounter;
37 * Creates a new bandwidth counting output stream.
40 * The output stream to wrap
42 public BandwidthCountingOutputStream(OutputStream outputStream) {
44 accumulatingDataCounter = new AccumulatingDataCounter();
48 * Creates a new bandwidth counting output stream.
51 * The output stream to wrap
52 * @param maximumLifeTime
53 * The maximum lifetime of the bandwidth counter
55 * The time unit of the lifetime
57 public BandwidthCountingOutputStream(OutputStream outputStream, 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 // OUTPUTSTREAM METHODS
103 public void write(int b) throws IOException {
105 accumulatingDataCounter.count(1);
109 public void write(byte[] buffer) throws IOException {
110 write(buffer, 0, buffer.length);
114 public void write(byte[] buffer, int offset, int length) throws IOException {
115 super.write(buffer, offset, length);
116 accumulatingDataCounter.count(length);
122 * This method also {@link AccumulatingDataCounter#stop()}s the bandwidth
123 * counter; subsequent calls of {@link #getOverallRate()} will always return
127 public void close() throws IOException {
128 accumulatingDataCounter.stop();