All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gc.iotools.stream.os.inspection.StatsOutputStream Maven / Gradle / Ivy

Go to download

EasyStream is a small set of utilities for dealing with streams (InputStreams and OutputStreams). The aim is to ease the use of pipes when they're required. Main features are: * "Convert" an OutputStream to an InputStream. * Count the number of bytes read or wrote to a given stream. * While reading the data from an InputStream copy it to a supplied OutputStream. * Read the content of an InputStream multiple times or seek to a definite position

The newest version!
package com.gc.iotools.stream.os.inspection;

/*
 * Copyright (c) 2008, 2015 Gabriele Contini. 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.6 * @version $Id: StatsOutputStream.java 576 2015-03-28 00:03:33Z gcontini $ */ 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 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 number of bytes written until now. * * @return return the number of bytes written until now. */ public long getSize() { return this.size; } /** *

* 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