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

com.gc.iotools.stream.writer.inspection.StatsWriter 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.writer.inspection;

/*
 * Copyright (c) 2008, 2015 Gabriele Contini. This source code is released
 * under the BSD License.
 */
import java.io.IOException;
import java.io.Writer;
import java.util.concurrent.TimeUnit;

import com.gc.iotools.stream.utils.StreamUtils;

/**
 * 

* A delegating Writer that gather statistics on the * Writer passed in the constructor. *

*

* It can be used to read: *

    *
  • The number of the characters written to the underlying character stream.
  • *
  • The time spent writing the character.
  • *
  • 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 Gabriele Contini * @since 1.2.14 * @version $Id: StatsWriter.java 527 2014-02-24 19:29:50Z * $ */ public class StatsWriter extends Writer { private boolean closeCalled; private final Writer innerOs; private long size = 0; private long time = 0; /** * Creates a new StatsWriter with the given destination * character stream. * * @param destination * Destination stream where data are written. */ public StatsWriter(final Writer 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 characterRate of the stream. */ public String getBitRateString() { return StreamUtils.getRateString(this.size, this.time); } /** * Returns the number of characters written until now. * * @return return the number of characters 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 char[] 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 char[] 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