Class CircularBufferInputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    PeekableInputStream

    public class CircularBufferInputStream
    extends java.io.InputStream
    Implements a buffered input stream, which is internally based on a CircularByteBuffer. Unlike the BufferedInputStream, this one doesn't need to reallocate byte arrays internally.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected CircularByteBuffer buffer
      Internal buffer.
      protected int bufferSize
      Internal buffer size.
      protected java.io.InputStream in
      What we are streaming, used to fill the internal buffer.
    • Constructor Summary

      Constructors 
      Constructor Description
      CircularBufferInputStream​(java.io.InputStream inputStream)
      Creates a new instance, which filters the given input stream, and uses a reasonable default buffer size (IOUtils.DEFAULT_BUFFER_SIZE).
      CircularBufferInputStream​(java.io.InputStream inputStream, int bufferSize)
      Creates a new instance, which filters the given input stream, and uses the given buffer size.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      protected void fillBuffer()
      Fills the buffer with the contents of the input stream.
      protected boolean haveBytes​(int count)
      Fills the buffer from the input stream until the given number of bytes have been added to the buffer.
      int read()  
      int read​(byte[] buffer)  
      int read​(byte[] targetBuffer, int offset, int length)  
      • Methods inherited from class java.io.InputStream

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

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • in

        protected final java.io.InputStream in
        What we are streaming, used to fill the internal buffer.
      • bufferSize

        protected final int bufferSize
        Internal buffer size.
    • Constructor Detail

      • CircularBufferInputStream

        public CircularBufferInputStream​(java.io.InputStream inputStream,
                                         int bufferSize)
        Creates a new instance, which filters the given input stream, and uses the given buffer size.
        Parameters:
        inputStream - The input stream, which is being buffered.
        bufferSize - The size of the CircularByteBuffer, which is used internally.
      • CircularBufferInputStream

        public CircularBufferInputStream​(java.io.InputStream inputStream)
        Creates a new instance, which filters the given input stream, and uses a reasonable default buffer size (IOUtils.DEFAULT_BUFFER_SIZE).
        Parameters:
        inputStream - The input stream, which is being buffered.
    • Method Detail

      • fillBuffer

        protected void fillBuffer()
                           throws java.io.IOException
        Fills the buffer with the contents of the input stream.
        Throws:
        java.io.IOException - in case of an error while reading from the input stream.
      • haveBytes

        protected boolean haveBytes​(int count)
                             throws java.io.IOException
        Fills the buffer from the input stream until the given number of bytes have been added to the buffer.
        Parameters:
        count - number of byte to fill into the buffer
        Returns:
        true if the buffer has bytes
        Throws:
        java.io.IOException - in case of an error while reading from the input stream.
      • 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[] buffer)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] targetBuffer,
                        int offset,
                        int length)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        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