Class SubStream

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

    public final class SubStream
    extends java.io.FilterInputStream
    An InputStream reading up to a specified number of bytes from an underlying stream.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private long bytesLeft  
      private int markLimit  
      • Fields inherited from class java.io.FilterInputStream

        in
    • Constructor Summary

      Constructors 
      Constructor Description
      SubStream​(java.io.InputStream stream, long length)
      Creates a SubStream of the given stream.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()  
      void close()
      Marks this stream as closed.
      private long findMaxLen​(long length)
      Finds the maximum number of bytes we can read or skip, from this stream.
      void mark​(int readLimit)  
      int read()  
      int read​(byte[] bytes)  
      int read​(byte[] bytes, int off, int len)  
      void reset()  
      long skip​(long length)  
      • Methods inherited from class java.io.FilterInputStream

        markSupported
      • 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
    • Field Detail

      • bytesLeft

        private long bytesLeft
      • markLimit

        private int markLimit
    • Constructor Detail

      • SubStream

        public SubStream​(java.io.InputStream stream,
                         long length)
        Creates a SubStream of the given stream.
        Parameters:
        stream - the underlying input stream
        length - maximum number of bytes to read from this stream
    • Method Detail

      • close

        public void close()
                   throws java.io.IOException
        Marks this stream as closed. This implementation does not close the underlying stream.
        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
      • available

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

        public void mark​(int readLimit)
        Overrides:
        mark in class java.io.FilterInputStream
      • reset

        public void reset()
                   throws java.io.IOException
        Overrides:
        reset 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[] bytes)
                 throws java.io.IOException
        Overrides:
        read in class java.io.FilterInputStream
        Throws:
        java.io.IOException
      • read

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

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

        private long findMaxLen​(long length)
        Finds the maximum number of bytes we can read or skip, from this stream.
        Parameters:
        length - the requested length
        Returns:
        the maximum number of bytes to read