gausssoft.io
Class BandwidthOutputStream

java.lang.Object
  |
  +--java.io.OutputStream
        |
        +--java.io.FilterOutputStream
              |
              +--gausssoft.io.StatusOutputStream
                    |
                    +--gausssoft.io.ChunkingOutputStream
                          |
                          +--gausssoft.io.BandwidthOutputStream
Direct Known Subclasses:
BandwidthControlledOutputStream

public class BandwidthOutputStream
extends ChunkingOutputStream

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

This implementation uses the ChunkingOutputStream class to perform updates. It is recommended that you look at how the ChunkingOutputStream 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 the initiate or resetMeasuring was last called. Bandwidth is calculated in bytes per second.

When the stream is closed, 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:
BandwidthInputStream

Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
  BandwidthOutputStream(OutputStream out)
          Creates a BandwidthOutputStream that does not use cumulative totals, chunks data into 1024 bytes, and performs start-of-call and end-of-call interrupts.
  BandwidthOutputStream(OutputStream out, int chunksize)
          Creates a BandwidthOutputStream that does not use cumulative totals, and performs start-of-call and end-of-call interrupts.
protected BandwidthOutputStream(OutputStream out, int chunksize, boolean cumulative, boolean start)
          Creates a BandwidthOutputStream 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.ChunkingOutputStream
getChunkSize, setChunkSize, updateChunkSize, write, write, write
 
Methods inherited from class gausssoft.io.StatusOutputStream
flush, getCount, isClosed
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BandwidthOutputStream

public BandwidthOutputStream(OutputStream out)
Creates a BandwidthOutputStream 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:
out - The OutputStream to wrap around.
Throws:
NullPointerException - If in is null.

BandwidthOutputStream

public BandwidthOutputStream(OutputStream out,
                             int chunksize)
Creates a BandwidthOutputStream 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:
out - The OutputStream to wrap around.
chunksize - The chunk size to use.
Throws:
NullPointerException - If in is null.
IllegalArgumentException - If chunksize is less than 1.

BandwidthOutputStream

protected BandwidthOutputStream(OutputStream out,
                                int chunksize,
                                boolean cumulative,
                                boolean start)
Creates a BandwidthOutputStream that performs end-of-call interrupts. The initiate() method must be called to begin measuring.
Parameters:
out - The OutputStream 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 ChunkingOutputStream
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 StatusOutputStream