Class CountedInputStream

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class CountedInputStream
    extends FilterInputStream
    A FilterInputStream which passes only a specified number of bytes, then returns an EOF condition.
    Author:
    Gary McGath
    • Constructor Detail

      • CountedInputStream

        public CountedInputStream​(InputStream instrm,
                                  int count)
        Parameters:
        instrm - The InputStream being counted
        count - The number of bytes to be allowed
    • Method Detail

      • read

        public int read()
                 throws IOException
        Reads a single byte from the stream and decrements the count of remaining bytes. If the count is exhausted, returns -1 to signify end of file.
        Overrides:
        read in class FilterInputStream
        Throws:
        IOException
      • read

        public int read​(byte[] b)
                 throws IOException
        Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. The number of bytes read will not exceed the number of bytes remaining in the count. The count is decremented by the number of bytes actually read.
        Overrides:
        read in class FilterInputStream
        Throws:
        IOException
      • 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. An attempt is made to read as many as len bytes, but a smaller number may be read, possibly zero. The number of bytes actually read is returned as an integer. The number of bytes read will not exceed the number of bytes remaining in the count. The count is decremented by the number of bytes actually read.
        Overrides:
        read in class FilterInputStream
        Throws:
        IOException