Class BoundedInputStream

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

    @NotThreadSafe
    public class BoundedInputStream
    extends java.io.FilterInputStream
    A wrapper for an InputStream that allows reading up to a specified number of bytes.
    • Field Summary

      • Fields inherited from class java.io.FilterInputStream

        in
    • Constructor Summary

      Constructors 
      Constructor Description
      BoundedInputStream​(java.io.InputStream inputStream, int bound)
      Creates a new instance of the BoundedInputStream class.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()  
      void close()  
      int getBound()  
      int getRemaining()  
      boolean markSupported()  
      int read()  
      int read​(byte[] buffer, int offset, int length)  
      long skip​(long count)  
      BoundedInputStream subStream​(int bound)
      Creates a new BoundedInputStream wrapping the same InputStream as this one, starting at the current position, with the given bound.
      • Methods inherited from class java.io.FilterInputStream

        mark, read, reset
      • Methods inherited from class java.io.InputStream

        nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
      • Methods inherited from class java.lang.Object

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

      • BoundedInputStream

        public BoundedInputStream​(java.io.InputStream inputStream,
                                  int bound)
        Creates a new instance of the BoundedInputStream class.
        Parameters:
        inputStream - The InputStream to wrap.
        bound - The maximum number of bytes that can be read with this instance.
    • Method Detail

      • 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.FilterInputStream
        Throws:
        java.io.IOException
      • read

        public int read()
                 throws java.io.IOException
        Overrides:
        read in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] buffer,
                        int offset,
                        int length)
                 throws java.io.IOException
        Overrides:
        read in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • skip

        public long skip​(long count)
                  throws java.io.IOException
        Overrides:
        skip in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • available

        public int available()
                      throws java.io.IOException
        Overrides:
        available in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • markSupported

        public boolean markSupported()
        Overrides:
        markSupported in class java.io.FilterInputStream
      • subStream

        public BoundedInputStream subStream​(int bound)
        Creates a new BoundedInputStream wrapping the same InputStream as this one, starting at the current position, with the given bound. NOTE: both this instance and the result of this method should not be both used at the same time to read from the InputStream. When this method returns, this instance's remaining count will be reduced by the given bound (that's since upon closing, the BoundedInputStream will auto-advance to its bound position).
        Parameters:
        bound - The bound of the sub-stream.
        Returns:
        A new instance of a BoundedInputStream.
      • getBound

        public int getBound()
      • getRemaining

        public int getRemaining()