
com.gc.iotools.stream.os.StatsOutputStream Maven / Gradle / Ivy
package com.gc.iotools.stream.os;
/*
* Copyright (c) 2008,2009 Davide Simonetti.
* This source code is released under the BSD License.
*/
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.TimeUnit;
import com.gc.iotools.stream.utils.StreamUtils;
/**
*
* Gather some statistics on the OutputStream
passed in the
* constructor.
*
*
* It can be used to read:
*
* - The size of the data written to the underlying stream.
* - The time spent writing the bytes.
* - The bandwidth of the underlying stream.
*
*
*
* Full statistics are available after the stream has been fully processed (by
* other parts of the application), or after invoking the method
* {@linkplain #close()} while partial statistics are available on the fly.
*
*
* @author dvd.smnt
* @since 1.2.1
*/
public class StatsOutputStream extends OutputStream {
private boolean closeCalled;
private final OutputStream innerOs;
private long size = 0;
private long time = 0;
/**
* Creates a new SizeRecorderOutputStream
with the given
* destination stream.
*
* @param destination
* Destination stream where data are written.
*/
public StatsOutputStream(final OutputStream destination) {
this.innerOs = destination;
}
/**
* {@inheritDoc}
*/
@Override
public void close() throws IOException {
if (!this.closeCalled) {
this.closeCalled = true;
final long start = System.currentTimeMillis();
this.innerOs.close();
this.time += System.currentTimeMillis() - start;
}
}
/**
* {@inheritDoc}
*/
@Override
public void flush() throws IOException {
final long start = System.currentTimeMillis();
this.innerOs.flush();
this.time += System.currentTimeMillis() - start;
}
/**
* Returns the number of bytes written until now.
*
* @return return the number of bytes written until now.
*/
public long getSize() {
return this.size;
}
/**
*
* Returns a string representation of the writing bit rate formatted with a
* convenient unit. The unit will change trying to keep not more than 3
* digits.
*
*
* @return The bitRate of the stream.
* @since 1.2.2
*/
public String getBitRateString() {
return StreamUtils.getRateString(this.size, this.time);
}
/**
*
* Returns the time spent waiting for the internal stream to write the data.
*
*
* @param tu
* Unit to measure the time.
* @return time spent in waiting.
*/
public long getTime(final TimeUnit tu) {
return tu.convert(this.time, TimeUnit.MILLISECONDS);
}
/**
* {@inheritDoc}
*/
@Override
public void write(final byte[] b) throws IOException {
final long start = System.currentTimeMillis();
this.innerOs.write(b);
this.time += System.currentTimeMillis() - start;
this.size += b.length;
}
/**
* {@inheritDoc}
*/
@Override
public void write(final byte[] b, final int off, final int len)
throws IOException {
final long start = System.currentTimeMillis();
this.innerOs.write(b, off, len);
this.time += System.currentTimeMillis() - start;
this.size += len;
}
/**
* {@inheritDoc}
*/
@Override
public void write(final int b) throws IOException {
final long start = System.currentTimeMillis();
this.innerOs.write(b);
this.time += System.currentTimeMillis() - start;
this.size++;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy