org.deckfour.xes.util.progress
Class XMonitoredInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.deckfour.xes.util.progress.XMonitoredInputStream
All Implemented Interfaces:
Closeable

public class XMonitoredInputStream
extends InputStream

This class implements an input stream which can provide a progress listener with feedback about how much of the data in the stream has already been read. This is a useful utility for reading XML-based data, while still providing feedback about expected progress.

Author:
Christian W. Guenther (christian@deckfour.org)

Field Summary
protected  long bytesRead
          Number of bytes read so far.
protected  int lastStep
          The last step which has been notified about.
protected  XProgressListener progressListener
          Progress listener which is being notified.
protected  int stepNumber
          The number of steps to be used for progress expression.
protected  long stepSize
          Number of bytes per step.
protected  InputStream stream
          Wrapped, monitored input stream.
 
Constructor Summary
XMonitoredInputStream(File file, XProgressListener progressListener)
          Creates a new monitored input stream.
XMonitoredInputStream(InputStream stream, long size, JProgressBar progressBar)
          Creates a new monitored input stream.
XMonitoredInputStream(InputStream stream, long size, XProgressListener progressListener)
          Creates a new monitored input stream.
XMonitoredInputStream(InputStream stream, long size, XProgressListener progressListener, int stepNumber)
          Creates a new monitored input stream.
 
Method Summary
 int getStepNumber()
          Returns the number of steps so far.
 int read()
           
 int read(byte[] b)
           
 int read(byte[] b, int off, int len)
           
 long skip(long n)
           
protected  void update(long readBytes)
          This method is called by the actual input stream method to provide feedback about the number of read bytes.
 
Methods inherited from class java.io.InputStream
available, close, mark, markSupported, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

stepNumber

protected int stepNumber
The number of steps to be used for progress expression.


stepSize

protected long stepSize
Number of bytes per step.


lastStep

protected int lastStep
The last step which has been notified about.


bytesRead

protected long bytesRead
Number of bytes read so far.


stream

protected InputStream stream
Wrapped, monitored input stream.


progressListener

protected XProgressListener progressListener
Progress listener which is being notified.

Constructor Detail

XMonitoredInputStream

public XMonitoredInputStream(File file,
                             XProgressListener progressListener)
                      throws FileNotFoundException
Creates a new monitored input stream.

Parameters:
file - The file to be read from.
progressListener - Progress listener to be notified.
Throws:
FileNotFoundException

XMonitoredInputStream

public XMonitoredInputStream(InputStream stream,
                             long size,
                             JProgressBar progressBar)
Creates a new monitored input stream.

Parameters:
stream - Monitored, wrapped lower-level input stream.
size - Number of bytes to be read from the stream.
progressBar - Progress bar to be updated.

XMonitoredInputStream

public XMonitoredInputStream(InputStream stream,
                             long size,
                             XProgressListener progressListener)
Creates a new monitored input stream.

Parameters:
stream - Monitored, wrapped lower-level input stream.
size - Number of bytes to be read from the stream.
progressListener - Progress listener to be notified.

XMonitoredInputStream

public XMonitoredInputStream(InputStream stream,
                             long size,
                             XProgressListener progressListener,
                             int stepNumber)
Creates a new monitored input stream.

Parameters:
stream - Monitored, wrapped lower-level input stream.
size - Number of bytes to be read from the stream.
progressListener - Progress listener to be notified.
stepNumber - Number of steps used to express progress.
Method Detail

update

protected void update(long readBytes)
               throws IOException
This method is called by the actual input stream method to provide feedback about the number of read bytes. Notifies the attached progress listener if appropriate.

Parameters:
readBytes - The number of read bytes in this call.
Throws:
IOException

getStepNumber

public int getStepNumber()
Returns the number of steps so far.

Returns:
Number of steps.

read

public int read()
         throws IOException
Specified by:
read in class InputStream
Throws:
IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Overrides:
read in class InputStream
Throws:
IOException

read

public int read(byte[] b)
         throws IOException
Overrides:
read in class InputStream
Throws:
IOException

skip

public long skip(long n)
          throws IOException
Overrides:
skip in class InputStream
Throws:
IOException