Class RereadableInputStream

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

    public class RereadableInputStream
    extends java.io.InputStream
    Wraps an input stream, reading it only once, but making it available for rereading an arbitrary number of times. The stream's bytes are stored in memory up to a user specified maximum, and then stored in a temporary file which is deleted when this class' close() method is called.
    • Constructor Summary

      Constructors 
      Constructor Description
      RereadableInputStream​(java.io.InputStream inputStream, int maxBytesInMemory, boolean readToEndOfStreamOnFirstRewind, boolean closeOriginalStreamOnClose)
      Creates a rereadable input stream.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes the input stream and removes the temporary file if one was created.
      int getSize()
      Returns the number of bytes read from the original stream.
      int read()
      Reads a byte from the stream, saving it in the store if it is being read from the original stream.
      void rewind()
      "Rewinds" the stream to the beginning for rereading.
      • Methods inherited from class java.io.InputStream

        available, mark, markSupported, nullInputStream, read, read, readAllBytes, readNBytes, readNBytes, reset, skip, transferTo
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • RereadableInputStream

        public RereadableInputStream​(java.io.InputStream inputStream,
                                     int maxBytesInMemory,
                                     boolean readToEndOfStreamOnFirstRewind,
                                     boolean closeOriginalStreamOnClose)
        Creates a rereadable input stream.
        Parameters:
        inputStream - stream containing the source of data
        maxBytesInMemory - maximum number of bytes to use to store the stream's contents in memory before switching to disk; note that the instance will preallocate a byte array whose size is maxBytesInMemory. This byte array will be made available for garbage collection (i.e. its reference set to null) when the content size exceeds the array's size, when close() is called, or when there are no more references to the instance.
        readToEndOfStreamOnFirstRewind - Specifies whether or not to read to the end of stream on first rewind. If this is set to false, then when rewind() is first called, only those bytes already read from the original stream will be available from then on.
    • Method Detail

      • read

        public int read()
                 throws java.io.IOException
        Reads a byte from the stream, saving it in the store if it is being read from the original stream. Implements the abstract InputStream.read().
        Specified by:
        read in class java.io.InputStream
        Returns:
        the read byte, or -1 on end of stream.
        Throws:
        java.io.IOException
      • rewind

        public void rewind()
                    throws java.io.IOException
        "Rewinds" the stream to the beginning for rereading.
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Closes the input stream and removes the temporary file if one was created.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.InputStream
        Throws:
        java.io.IOException
      • getSize

        public int getSize()
        Returns the number of bytes read from the original stream.
        Returns:
        number of bytes read