jsky.util.gui
Class ProgressBarFilterInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by jsky.util.gui.ProgressBarFilterInputStream
All Implemented Interfaces:
java.io.Closeable

public class ProgressBarFilterInputStream
extends java.io.FilterInputStream

Monitors reading from a given stream or URL and updates a given progress bar and text field to show the amount of data read so far.


Field Summary
protected  boolean interrupted
          Set this to interrupt the reading and throw an exception
protected  javax.swing.DefaultBoundedRangeModel model
          The model for the progress bar
protected  int nread
          The number of bytes read so far
protected  ProgressBarUtil progressBar
          The progress bar to use
protected  int size
          The size of the data in bytes, if known, otherwise 0
protected  long updateTime
          Time in ms of last update (used to slow down text field updates)
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
ProgressBarFilterInputStream(ProgressBarUtil progressBar, javax.swing.JTextField statusField, java.io.InputStream in, int size)
          Constructs an object to monitor the progress of an input stream using a given progress bar and text field.
ProgressBarFilterInputStream(ProgressBarUtil progressBar, javax.swing.JTextField statusField, java.net.URL url)
          Constructs an object to monitor the progress of an input stream using a given progress bar and text field.
ProgressBarFilterInputStream(StatusPanel statusPanel, java.net.URL url)
          Constructs an object to monitor the progress of an input stream using a given StatusPanel object.
 
Method Summary
 void checkForInterrupt()
          Throw an exception if interrupt() was called on this stream.
 void clear()
          Reset the progress bar to the idle state
 void close()
          Overrides FilterInputStream.close to close the progress bar as well as the stream.
 int getSize()
          Return the size of the data to read
 void interrupt()
          Interrupt the reading (causes the next read() to throw an exception).
 boolean isInterrupted()
          Return true if reading was interrupted
static void main(java.lang.String[] args)
          Test main
 int read()
          Overrides FilterInputStream.read to update the progress bar after the read.
 int read(byte[] b, int off, int len)
          Overrides FilterInputStream.read to update the progress bar after the read.
 void reset()
          Overrides FilterInputStream.reset to reset the progress bar as well as the stream.
protected  void setNumBytesRead(int n)
          Set the number of bytes that have been read, update the display (but not too often) and check for interrupt requests.
 void setSize(int size)
          Set the size of the data to read
 long skip(long n)
          Overrides FilterInputStream.skip to update the progress bar after the skip.
 
Methods inherited from class java.io.FilterInputStream
available, mark, markSupported, read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

progressBar

protected ProgressBarUtil progressBar
The progress bar to use


model

protected javax.swing.DefaultBoundedRangeModel model
The model for the progress bar


nread

protected int nread
The number of bytes read so far


size

protected int size
The size of the data in bytes, if known, otherwise 0


updateTime

protected long updateTime
Time in ms of last update (used to slow down text field updates)


interrupted

protected boolean interrupted
Set this to interrupt the reading and throw an exception

Constructor Detail

ProgressBarFilterInputStream

public ProgressBarFilterInputStream(ProgressBarUtil progressBar,
                                    javax.swing.JTextField statusField,
                                    java.io.InputStream in,
                                    int size)
Constructs an object to monitor the progress of an input stream using a given progress bar and text field.

Parameters:
progressBar - the progress bar to use
statusField - text field used to display status information
in - the input stream to be monitored
size - the size in bytes of the date to be read, or 0 if not known

ProgressBarFilterInputStream

public ProgressBarFilterInputStream(ProgressBarUtil progressBar,
                                    javax.swing.JTextField statusField,
                                    java.net.URL url)
Constructs an object to monitor the progress of an input stream using a given progress bar and text field.

Parameters:
progressBar - the progress bar to use
statusField - text field used to display status information
url - the URL to read

ProgressBarFilterInputStream

public ProgressBarFilterInputStream(StatusPanel statusPanel,
                                    java.net.URL url)
Constructs an object to monitor the progress of an input stream using a given StatusPanel object.

Parameters:
statusPanel - used to display status information
url - the URL to read
Method Detail

interrupt

public void interrupt()
Interrupt the reading (causes the next read() to throw an exception). This is normally called when a Stop or Cancel button is pushed.


isInterrupted

public boolean isInterrupted()
Return true if reading was interrupted


checkForInterrupt

public void checkForInterrupt()
                       throws java.io.IOException
Throw an exception if interrupt() was called on this stream.

Throws:
java.io.IOException

setSize

public void setSize(int size)
Set the size of the data to read


setNumBytesRead

protected void setNumBytesRead(int n)
Set the number of bytes that have been read, update the display (but not too often) and check for interrupt requests.


clear

public void clear()
Reset the progress bar to the idle state


getSize

public int getSize()
Return the size of the data to read


read

public int read()
         throws java.io.IOException
Overrides FilterInputStream.read to update the progress bar after the read.

Overrides:
read in class java.io.FilterInputStream
Throws:
java.io.IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Overrides FilterInputStream.read to update the progress bar after the read.

Overrides:
read in class java.io.FilterInputStream
Throws:
java.io.IOException

skip

public long skip(long n)
          throws java.io.IOException
Overrides FilterInputStream.skip to update the progress bar after the skip.

Overrides:
skip in class java.io.FilterInputStream
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Overrides FilterInputStream.close to close the progress bar as well as the stream.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.FilterInputStream
Throws:
java.io.IOException

reset

public void reset()
           throws java.io.IOException
Overrides FilterInputStream.reset to reset the progress bar as well as the stream.

Overrides:
reset in class java.io.FilterInputStream
Throws:
java.io.IOException

main

public static void main(java.lang.String[] args)
Test main