com.anthonyeden.lib.io
Class ByteCountInputStream

java.lang.Object
  extended byjava.io.InputStream
      extended byjava.io.FilterInputStream
          extended bycom.anthonyeden.lib.io.ByteCountInputStream

public class ByteCountInputStream
extends java.io.FilterInputStream

A FilterInputStream which will keep track of the number of bytes read from the underlying stream. Callers who want to know how many bytes have been read should attach a ChangeListener and call getCount() whenever a change event is fired.

If no ChangeListeners are attached then this stream will not fire events. This improves performance by only firing events when at least one object is expecting them.

Author:
Anthony Eden

Field Summary
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
ByteCountInputStream(java.io.InputStream in)
          Construct a new ByteCountInputStream.
 
Method Summary
 void addChangeListener(javax.swing.event.ChangeListener l)
          Add a ChangeListener.
protected  void fireStateChanged()
          Fire a ChangeEvent.
protected  java.util.ArrayList getChangeListeners()
          Get a List of attached ChangeListeners.
 long getCount()
          Get the number of bytes read so far.
 int read()
          Read one byte.
 int read(byte[] buffer)
          Fill the buffer with bytes until the buffer is full, the stream blocks, or until the end of the stream.
 int read(byte[] buffer, int offset, int length)
          Fill the buffer with bytes starting at the given offset of the buffer and reading for length number of bytes.
 void removeChangeListener(javax.swing.event.ChangeListener l)
          Remove the ChangeListener.
 
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteCountInputStream

public ByteCountInputStream(java.io.InputStream in)
Construct a new ByteCountInputStream.

Parameters:
in - The input stream to count
Method Detail

addChangeListener

public void addChangeListener(javax.swing.event.ChangeListener l)
Add a ChangeListener.

Parameters:
l - The ChangeListener

removeChangeListener

public void removeChangeListener(javax.swing.event.ChangeListener l)
Remove the ChangeListener.

Parameters:
l - The ChangeListener

read

public int read(byte[] buffer)
         throws java.io.IOException
Fill the buffer with bytes until the buffer is full, the stream blocks, or until the end of the stream.

Parameters:
buffer - The buffer to fill
Returns:
The number of bytes read
Throws:
java.io.IOException

read

public int read(byte[] buffer,
                int offset,
                int length)
         throws java.io.IOException
Fill the buffer with bytes starting at the given offset of the buffer and reading for length number of bytes.

Parameters:
buffer - The buffer
offset - The buffer offset
length - The number of bytes to read
Returns:
The number of bytes read
Throws:
java.io.IOException

read

public int read()
         throws java.io.IOException
Read one byte.

Returns:
The byte
Throws:
java.io.IOException

getCount

public long getCount()
Get the number of bytes read so far.

Returns:
The byte count

getChangeListeners

protected java.util.ArrayList getChangeListeners()
Get a List of attached ChangeListeners.

Returns:
The List of ChangeListeners

fireStateChanged

protected void fireStateChanged()
Fire a ChangeEvent.



Copyright (c) 2000-2003 Anthony Eden. All Rights Reserved.