com.gc.iotools.stream.reader.inspection
Class StatsReader

java.lang.Object
  extended by java.io.Reader
      extended by com.gc.iotools.stream.reader.inspection.StatsReader
All Implemented Interfaces:
Closeable, Readable

public class StatsReader
extends Reader

Gather some statistics of the Reader passed in the constructor.

It can be used to read:

Full statistics are available after the stream has been fully processed (by other parts of the application), or after invoking the method close() while partial statistics are available on the fly.

Usage:

 StatsReader srReader = new StatsReader(originalStream);
 //performs all the application operation on stream
 performTasksOnStream(srReader);
 srReader.close();
 long size = srReader.getSize();
 

Since:
1.2.7
Version:
$Id: StatsReader.java 527 2014-02-24 19:29:50Z gabriele.contini@gmail.com $
Author:
dvd.smnt

Field Summary
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
StatsReader(Reader source)
           Constructs an SizeReaderReader.
StatsReader(Reader istream, boolean fullReadOnClose)
          Constructs an SizeReaderReader and allow to specify actions to do on close.
StatsReader(Reader istream, boolean fullReadOnClose, boolean automaticLog)
           Constructs an SizeReaderReader and allow to specify actions to do on close.
StatsReader(Reader istream, boolean fullReadOnClose, boolean automaticLog, StatsReader chainStream)
           Constructs an SizeReaderReader and allow to specify actions to do on close.
 
Method Summary
 void close()
           Closes the inner stream.
protected  void finalize()
          
 float getAverageBytePerRead()
           Returns the average bytes per read.
 float getBitRate()
          Returns the reading bit rate in KB per second of this single instance.
 String getBitRateString()
          Returns the reading bit rate formatted with a convenient unit.
 long getNumberRead()
          Number of calls to int read() , int read(byte[]) and int read(byte[],int,int) methods.
 long getSize()
           Returns the number of bytes read until now from the internal Reader or total length of the stream if the close() method has been called or EOF was reached.
 long getTime()
           Returns the time (in milliseconds) spent until now waiting for reading from the internal Reader.
 long getTime(TimeUnit tu)
           Returns the time spent until now waiting for the internal stream to respond.
 long getTotalNumberRead()
          Total count of calls to int read(), int read(byte[]) and int read(byte[],int,int) methods, made by this instance over the subsequent calls.
 long getTotalTime(TimeUnit tu)
           Returns the total time (in milliseconds) spent until now waiting for reading from the internal Reader by the instances (identified by their constructor position).
 boolean isFullReadOnClose()
          Returns the behavior of the close method.
 void logCurrentStatistics()
          Logs the current statistics.
 void mark(int readlimit)
          
 boolean markSupported()
          
 int read()
          
 int read(char[] b)
          
 int read(char[] chars, int off, int len)
          
 void reset()
          
 long skip(long n)
          
 
Methods inherited from class java.io.Reader
read, ready
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StatsReader

public StatsReader(Reader source)

Constructs an SizeReaderReader. When close() is called the underlying stream will be closed. No further read will be done.

Parameters:
source - Stream whose statistics must be calculated.

StatsReader

public StatsReader(Reader istream,
                   boolean fullReadOnClose)
Constructs an SizeReaderReader and allow to specify actions to do on close.

Parameters:
istream - Stream whose bytes must be counted.
fullReadOnClose - if true after the close the inner stream is read completely and the effective size of the inner stream is calculated.

StatsReader

public StatsReader(Reader istream,
                   boolean fullReadOnClose,
                   boolean automaticLog)

Constructs an SizeReaderReader and allow to specify actions to do on close.

If automaticLog is true the statistics will be written when the StatsReader is closed or finalized.

Parameters:
istream - Stream whose bytes must be counted.
fullReadOnClose - if true after the close the inner stream is read completely and the effective size of the inner stream is calculated.
automaticLog - if true statistics will be automatically written when the stream is closed or finalized.
Since:
1.2.7

StatsReader

public StatsReader(Reader istream,
                   boolean fullReadOnClose,
                   boolean automaticLog,
                   StatsReader chainStream)

Constructs an SizeReaderReader and allow to specify actions to do on close.

If automaticLog is true the statistics will be written when the StatsReader is closed or finalized.

Indicates another StatsReader to chain with. The aim is to test performances of a single Reader in a chain of multiple Readers. You should put the Reader to be tested between two StatsReader and chain the two together.

Reader source = //source of data StatsReader sourceStats = new StatsReader(source); Reader toBeTested = new ReaderToBeTested(sourceStats); StatsReader wrapperStis=new StatsReader(toBeTested, false, false, sourceStats);

This will allow to produce statistics of the single Reader to be tested, in a way independent from the source. Times spent will be the difference between the times from the source and times on the final wrapper.

Parameters:
istream - Stream whose bytes must be counted.
fullReadOnClose - if true after the close the inner stream is read completely and the effective size of the inner stream is calculated.
automaticLog - if true statistics will be automatically written when the stream is closed or finalized.
chainStream - The Reader to chain.
Since:
1.3.0
Method Detail

close

public void close()
           throws IOException
Closes the inner stream. If fullReadOnClose was set in the constructor it also count all the bytes of the underlying stream.

Specified by:
close in interface Closeable
Specified by:
close in class Reader
Throws:
IOException - if an I/O error occurs reading the whole content of the stream.
See Also:
Reader.close()

finalize

protected void finalize()
                 throws Throwable

Overrides:
finalize in class Object
Throws:
Throwable

getAverageBytePerRead

public float getAverageBytePerRead()

Returns the average bytes per read.

If this parameter is near 1 means that too many calls are made to read the Reader bringing a loss of performances for large amount of data. Access to this Reader should be made trough a BufferedReader with a reasonable buffer size.

WARN: This measure is not accurate in case of mark and reset.

Returns:
The average bytes per read().

getBitRate

public float getBitRate()
Returns the reading bit rate in KB per second of this single instance.

Returns:
The KB/Sec bitRate of the stream.

getBitRateString

public String getBitRateString()
Returns the reading bit rate formatted with a convenient unit.

Returns:
The bitRate of the stream.

getNumberRead

public long getNumberRead()
Number of calls to int read() , int read(byte[]) and int read(byte[],int,int) methods.

Returns:
Long representing the number of calls to read() methods.

getSize

public long getSize()

Returns the number of bytes read until now from the internal Reader or total length of the stream if the close() method has been called or EOF was reached.

Calculation refers to the original size of the internal Reader. If mark(int) and reset() are called, the extra data read after the reset() is not taken in account, until the mark position is reached again.

Returns:
bytes read until now or the total length of the stream if close() was called.

getTime

public long getTime()

Returns the time (in milliseconds) spent until now waiting for reading from the internal Reader.

Returns:
time spent in waiting in milliseconds.

getTime

public long getTime(TimeUnit tu)

Returns the time spent until now waiting for the internal stream to respond.

Parameters:
tu - Unit to measure the time.
Returns:
time spent in waiting.

getTotalNumberRead

public long getTotalNumberRead()
Total count of calls to int read(), int read(byte[]) and int read(byte[],int,int) methods, made by this instance over the subsequent calls.

Returns:
Long representing the number of calls to read() methods.

getTotalTime

public long getTotalTime(TimeUnit tu)

Returns the total time (in milliseconds) spent until now waiting for reading from the internal Reader by the instances (identified by their constructor position).

Parameters:
tu - Unit to measure the time.
Returns:
time spent in waiting.

isFullReadOnClose

public boolean isFullReadOnClose()
Returns the behavior of the close method. If true when close is invoked a full read of the stream will be performed.

Returns:
Whether a full read will be performed on the invocation of close() method.

logCurrentStatistics

public void logCurrentStatistics()
Logs the current statistics.


mark

public void mark(int readlimit)
          throws IOException

Overrides:
mark in class Reader
Throws:
IOException

markSupported

public boolean markSupported()

Overrides:
markSupported in class Reader

read

public int read()
         throws IOException

Overrides:
read in class Reader
Throws:
IOException

read

public int read(char[] b)
         throws IOException

Overrides:
read in class Reader
Throws:
IOException

read

public int read(char[] chars,
                int off,
                int len)
         throws IOException

Specified by:
read in class Reader
Throws:
IOException

reset

public void reset()
           throws IOException

Overrides:
reset in class Reader
Throws:
IOException

skip

public long skip(long n)
          throws IOException

Overrides:
skip in class Reader
Throws:
IOException


Copyright © 2008–2014. All rights reserved.