Class XMonitoredInputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class XMonitoredInputStream
    extends java.io.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

      Fields 
      Modifier and Type Field Description
      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 java.io.InputStream stream
      Wrapped, monitored input stream.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      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, nullInputStream, readAllBytes, readNBytes, readNBytes, reset, transferTo
      • 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 java.io.InputStream stream
        Wrapped, monitored input stream.
      • progressListener

        protected XProgressListener progressListener
        Progress listener which is being notified.
    • Constructor Detail

      • XMonitoredInputStream

        public XMonitoredInputStream​(java.io.File file,
                                     XProgressListener progressListener)
                              throws java.io.FileNotFoundException
        Creates a new monitored input stream.
        Parameters:
        file - The file to be read from.
        progressListener - Progress listener to be notified.
        Throws:
        java.io.FileNotFoundException
      • XMonitoredInputStream

        public XMonitoredInputStream​(java.io.InputStream stream,
                                     long size,
                                     javax.swing.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​(java.io.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​(java.io.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 java.io.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:
        java.io.IOException
      • getStepNumber

        public int getStepNumber()
        Returns the number of steps so far.
        Returns:
        Number of steps.
      • read

        public int read()
                 throws java.io.IOException
        Specified by:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • read

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

        public int read​(byte[] b)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • skip

        public long skip​(long n)
                  throws java.io.IOException
        Overrides:
        skip in class java.io.InputStream
        Throws:
        java.io.IOException