Class RewindableInputStream

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_XMLDECL_BUFFER_SIZE
      Default buffer size before we've finished with the XMLDecl: I think the name should be left unchanged to give a hint for possible use of this class :)
      protected byte[] fData
      Internal buffer of bytes already read from source input stream.
      protected int fEndOffset
      Position where the end of underlying stream was encountered.
      protected InputStream fInputStream
      Source input stream we are wrapping with rewindable one.
      protected int fLength
      Number of read bytes currently stored in fData buffer.
      protected int fMark
      Offset of the "marked" position in the stream relative to its beginning.
      protected boolean fMayReadChunks
      Tells whether read(byte[], int, int) method is allowed to read multiple bytes beyond the internal buffer (true) or not (true is the default)
      protected int fOffset
      Offset of the next byte to be read from the stream relative to the beginning of the stream (and fData array as well)
      protected int fStartOffset
      Position in the stream that to which stream pointer can be reset with rewind method invocation.
    • Constructor Summary

      Constructors 
      Constructor Description
      RewindableInputStream​(InputStream is)
      Creates new RewindableInputStream object with internal buffer of default size and default value of chunked reading flag (which is _currently_ true).
      RewindableInputStream​(InputStream is, boolean chunkedMode)
      Creates new RewindableInputStream with internal buffer of specified size and no chunk reading beyound the buffer limits allowed.
      RewindableInputStream​(InputStream is, boolean chunkedMode, int initialSize)
      Primary constructor that allows to specify all parameters exlicitly affecting class work (initial size of the internal buffer and chunk read mode).
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()
      Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.
      void close()
      Closes underlying byte stream.
      void disableChunkedMode()
      More conscious alias for setChunkedMode(false).
      void enableChunkedMode()
      More conscious alias for setChunkedMode(true).
      void mark​(int howMuch)
      Sets a mark to the current position in the stream.
      boolean markSupported()
      Tells that this stream supports mark/reset capability.
      int read()
      Reads next byte from this stream.
      int read​(byte[] b, int off, int len)
      Reads up to len bytes of data from the input stream into an array of bytes.
      void reset()
      Returns stream pointer to the position previously remembered using mark method (or to beginning of the stream, if there were no mark method calls).
      void rewind()
      Quickly reset stream pointer to the beginning of the stream or to position which offset was specified during the last setStartOffset call.
      void setChunkedMode​(boolean chunkedMode)
      Allows to change the behavior of the stream regarding chunked reading at runtime.
      void setStartOffset​(int offset)
      Sets the position somewhere in the stream to which the stream pointer will be reset after rewind invocation.
      long skip​(long n)
      Skips over and discards n bytes of data from this input stream.
    • Field Detail

      • DEFAULT_XMLDECL_BUFFER_SIZE

        public static final int DEFAULT_XMLDECL_BUFFER_SIZE
        Default buffer size before we've finished with the XMLDecl: I think the name should be left unchanged to give a hint for possible use of this class :)
        See Also:
        Constant Field Values
      • fMayReadChunks

        protected boolean fMayReadChunks
        Tells whether read(byte[], int, int) method is allowed to read multiple bytes beyond the internal buffer (true) or not (true is the default)
      • fInputStream

        protected InputStream fInputStream
        Source input stream we are wrapping with rewindable one.
      • fData

        protected byte[] fData
        Internal buffer of bytes already read from source input stream. Allows to access the same byte more than once.
      • fStartOffset

        protected int fStartOffset
        Position in the stream that to which stream pointer can be reset with rewind method invocation.
      • fEndOffset

        protected int fEndOffset
        Position where the end of underlying stream was encountered. Potentially in RewindableInputStream instance stream's "end" could be reached more than once, so it is a good thing to know where original stream ended to avoid IOExceptions.
      • fOffset

        protected int fOffset
        Offset of the next byte to be read from the stream relative to the beginning of the stream (and fData array as well)
      • fLength

        protected int fLength
        Number of read bytes currently stored in fData buffer. Size of the buffer itself can be greater than this value, obviously.
      • fMark

        protected int fMark
        Offset of the "marked" position in the stream relative to its beginning.
    • Constructor Detail

      • RewindableInputStream

        public RewindableInputStream​(InputStream is)
        Creates new RewindableInputStream object with internal buffer of default size and default value of chunked reading flag (which is _currently_ true).
        Parameters:
        is - InputStream that needs basic reset/rewind functionality.
      • RewindableInputStream

        public RewindableInputStream​(InputStream is,
                                     boolean chunkedMode)
        Creates new RewindableInputStream with internal buffer of specified size and no chunk reading beyound the buffer limits allowed.
        Parameters:
        is - InputStream that needs some reset/rewind functionality.
        chunkedMode - See the RewindableInputStream(InputStream, boolean, int) constructor description.
      • RewindableInputStream

        public RewindableInputStream​(InputStream is,
                                     boolean chunkedMode,
                                     int initialSize)
        Primary constructor that allows to specify all parameters exlicitly affecting class work (initial size of the internal buffer and chunk read mode).
        Parameters:
        is - InputStream that needs some reset/rewind functionality.
        chunkedMode -

        Initial value of fMayReadChunks flag which determines whether multiple bytes can be read from the underlying stream in single reading operation or not. This value can be changed using setChunkedMode (or its aliases). For specific purpose of inferring encoding/charset of XML document typical usage policy is to disable chunked reads while obtaining XML declaration and then enable it to speed up reading the rest of document.

        initialSize - Initial size of the internal buffer array.
    • Method Detail

      • setStartOffset

        public void setStartOffset​(int offset)
        Sets the position somewhere in the stream to which the stream pointer will be reset after rewind invocation. By default this position is the beginning of the stream.
        Parameters:
        offset - New value for "fStartOffset".
      • setChunkedMode

        public void setChunkedMode​(boolean chunkedMode)
        Allows to change the behavior of the stream regarding chunked reading at runtime. If you allowed chunked reading and then read some data from the stream, you better forget about resetting or rewinding it after that.
        Parameters:
        chunkedMode - New value for fMayReadChunks.
      • enableChunkedMode

        public void enableChunkedMode()
        More conscious alias for setChunkedMode(true). While last method is a general purpose mutator, code may look a bit more clear if you use specialized methods to enable/disable chunk read mode.
      • disableChunkedMode

        public void disableChunkedMode()
        More conscious alias for setChunkedMode(false). While last method is a general purpose mutator, code may look a bit more clear if you use specialized methods to enable/disable chunk read mode.
      • rewind

        public void rewind()
        Quickly reset stream pointer to the beginning of the stream or to position which offset was specified during the last setStartOffset call.
      • read

        public int read()
                 throws IOException
        Reads next byte from this stream. This byte is either being read from underlying InputStream or taken from the internal buffer in case it was already read at some point before.
        Specified by:
        read in class InputStream
        Returns:
        Next byte of data or -1 if end of stream is reached.
        Throws:
        IOException - in case of any I/O errors.
      • read

        public int read​(byte[] b,
                        int off,
                        int len)
                 throws IOException
        Reads up to len bytes of data from the input stream into an array of bytes. In its current implementation it cannot return more bytes than left in the buffer if in "non-chunked" mode ( fMayReadChunks == false). After reaching the end of the buffer, each invocation of this method will read exactly 1 byte then. In "chunked" mode this method may return more than 1 byte, but it doesn't buffer the result.

        From the other hand, for the task of reading xml declaration, such behavior may be desirable, as we probably don't need reset/rewind functionality after we finished with charset deduction. It is good idea to call enableChunkedMode after that, in order to improve perfomance and lessen memoery consumption when reading the rest of the data.

        Overrides:
        read in class InputStream
        Returns:
        Total number of bytes actually read or -1 if end of stream has been reached.
        Throws:
        IOException - when an I/O error occurs while reading data
        IndexOutOfBoundsException - in case of invalid off, len and b.length combination
      • skip

        public long skip​(long n)
                  throws IOException
        Skips over and discards n bytes of data from this input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. The actual number of bytes skipped is returned. If n is negative, no bytes are skipped.
        Overrides:
        skip in class InputStream
        Parameters:
        n - Number of bytes to be skipped.
        Returns:
        Number of bytes actually skipped.
        Throws:
        IOException - if an I/O error occurs.
      • available

        public int available()
                      throws IOException
        Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream. For RewindableInputStream this can be:
        • Number of unread bytes in the fData buffer, i.e. those between current position (fOffset) and total bytes quantity in the buffer (fLength).
        • Result of underlying InputStream's available call if there are no unread bytes in the buffer.
        • -1 if end of stream is reached.
        Overrides:
        available in class InputStream
        Returns:
        the number of bytes that can be read from this input stream without blocking.
        Throws:
        IOException - when an I/O error occurs.
      • mark

        public void mark​(int howMuch)
        Sets a mark to the current position in the stream.
        Overrides:
        mark in class InputStream
        Parameters:
        howMuch - Not used in this implementation I guess.
      • reset

        public void reset()
        Returns stream pointer to the position previously remembered using mark method (or to beginning of the stream, if there were no mark method calls).
        Overrides:
        reset in class InputStream
      • markSupported

        public boolean markSupported()
        Tells that this stream supports mark/reset capability. This one definitely supports it :)
        Overrides:
        markSupported in class InputStream
        Returns:
        true if this stream instance supports the mark and reset methods; false otherwise.