gausssoft.io
Class BandwidthInputStream

java.lang.Object
  |
  +--java.io.InputStream
        |
        +--java.io.FilterInputStream
              |
              +--gausssoft.io.StatusInputStream
                    |
                    +--gausssoft.io.ChunkingInputStream
                          |
                          +--gausssoft.io.BandwidthInputStream
Direct Known Subclasses:
BandwidthControlledInputStream

public class BandwidthInputStream
extends ChunkingInputStream

This class measures the level of throughput that goes into an underlying InputStream.

This implementation uses the ChunkingInputStream class to perform updates. It is recommended that you look at how the ChunkingInputStream class works before using this class.

The bandwidth is calculated every time an interrupt is performed. It is necessary to call the initiate() method to begin measuring throughput. You can also call the resetMeasuring() to discard previous bandwidth calculations.

Bandwidth is calculated by taking the number of bytes passed since initiate or resetMeasuring was last called. Bandwidth is calculated in bytes per second.

When the stream is empty, the bandwidth at that point is calculated, and is subsequently returned every time getBandwidth() is invoked.

Since:
GSDK 1.1
Author:
Allan Crooks
See Also:
BandwidthOutputStream

Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
  BandwidthInputStream(InputStream in)
          Creates a BandwidthInputStream that does not use cumulative totals, chunks data into 1024 bytes, and performs start-of-call and end-of-call interrupts.
  BandwidthInputStream(InputStream in, int chunksize)
          Creates a BandwidthInputStream that does not use cumulative totals, and performs start-of-call and end-of-call interrupts.
protected BandwidthInputStream(InputStream in, int chunksize, boolean cumulative, boolean start)
          Creates a BandwidthInputStream that performs end-of-call interrupts.
 
Method Summary
 void close()
           
 int getBandwidth()
          Calculates the bandwidth in bytes per second.
 void initiate()
          Begins measuring bandwidth.
 boolean initiated()
          Indicates if this stream has been initiated yet.
protected  void interrupt()
          Used for bandwidth calculations.
 void resetMeasuring()
          Discards all previous calculation data.
 
Methods inherited from class gausssoft.io.ChunkingInputStream
getChunkSize, read, read, read, setChunkSize, skip, updateChunkSize
 
Methods inherited from class gausssoft.io.StatusInputStream
available, getCount, isClosed, isEmpty
 
Methods inherited from class java.io.FilterInputStream
mark, markSupported, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BandwidthInputStream

public BandwidthInputStream(InputStream in)
Creates a BandwidthInputStream that does not use cumulative totals, chunks data into 1024 bytes, and performs start-of-call and end-of-call interrupts.

The initiate() method must be called to begin measuring.

Parameters:
in - The InputStream to wrap around.
Throws:
NullPointerException - If in is null.

BandwidthInputStream

public BandwidthInputStream(InputStream in,
                            int chunksize)
Creates a BandwidthInputStream that does not use cumulative totals, and performs start-of-call and end-of-call interrupts.

The initiate() method must be called to begin measuring.

Parameters:
in - The InputStream to wrap around.
chunksize - The chunk size to use.
Throws:
NullPointerException - If in is null.
IllegalArgumentException - If chunksize is less than 1.

BandwidthInputStream

protected BandwidthInputStream(InputStream in,
                               int chunksize,
                               boolean cumulative,
                               boolean start)
Creates a BandwidthInputStream that performs end-of-call interrupts.

The initiate() method must be called to begin measuring.

Parameters:
in - The InputStream to wrap around.
chunksize - The chunk size to use.
cumulative - true if this object should use the getCount method for calculations, false to use an internal counter.
start - true if this object should perform start-of-call interrupts.
Throws:
NullPointerException - If in is null.
IllegalArgumentException - If chunksize is less than 1.
Method Detail

initiate

public void initiate()
Begins measuring bandwidth. This method is also overrideable by subclasses that need to be explicitly told to function properly instead of functioning immediately at construction time.
See Also:
resetMeasuring(), initiated()

interrupt

protected void interrupt()
Used for bandwidth calculations.
Overrides:
interrupt in class ChunkingInputStream
See Also:
ChunkingOutputStream.interrupt()

initiated

public final boolean initiated()
Indicates if this stream has been initiated yet.
Returns:
true if this stream has been initiated.
See Also:
initiate()

resetMeasuring

public final void resetMeasuring()
Discards all previous calculation data.

getBandwidth

public final int getBandwidth()
Calculates the bandwidth in bytes per second.
Returns:
The bandwidth in bytes per second.
Throws:
IllegalStateException - If this method is invoked before the object is initialised.

close

public void close()
           throws IOException
Overrides:
close in class StatusInputStream