Class BoundedReader

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

    public class BoundedReader
    extends ProxyReader
    A reader that imposes a limit to the number of characters that can be read from an underlying reader, returning EOF when this limit is reached, regardless of state of underlying reader.

    One use case is to avoid overrunning the readAheadLimit supplied to Reader.mark(int), since reading too many characters removes the ability to do a successful reset.

    Since:
    2.5
    • Field Summary

      • Fields inherited from class java.io.FilterReader

        in
      • Fields inherited from class java.io.Reader

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      BoundedReader​(java.io.Reader target, int maxCharsFromTargetReader)
      Constructs a bounded reader
    • Constructor Detail

      • BoundedReader

        public BoundedReader​(java.io.Reader target,
                             int maxCharsFromTargetReader)
        Constructs a bounded reader
        Parameters:
        target - The target stream that will be used.
        maxCharsFromTargetReader - The maximum number of characters that can be read from target.
    • Method Detail

      • mark

        public void mark​(int readAheadLimit)
                  throws java.io.IOException
        marks the target stream
        Overrides:
        mark in class ProxyReader
        Parameters:
        readAheadLimit - The number of characters that can be read while still retaining the ability to do #reset(). Note that this parameter is not validated with respect to maxCharsFromTargetReader. There is no way to pass past maxCharsFromTargetReader, even if this value is greater.
        Throws:
        java.io.IOException - If an I/O error occurs while calling the underlying reader's mark method.
        See Also:
        Reader.mark(int)
      • read

        public int read()
                 throws java.io.IOException
        Reads a single character
        Overrides:
        read in class ProxyReader
        Returns:
        -1 on EOF or the character read.
        Throws:
        java.io.IOException - If an I/O error occurs while calling the underlying reader's read method.
        See Also:
        Reader.read()
      • read

        public int read​(char[] cbuf,
                        int off,
                        int len)
                 throws java.io.IOException
        Reads into an array
        Overrides:
        read in class ProxyReader
        Parameters:
        cbuf - The buffer to fill.
        off - The offset.
        len - The number of chars to read.
        Returns:
        the number of chars read.
        Throws:
        java.lang.NullPointerException - if the buffer is null.
        java.lang.IndexOutOfBoundsException - if off or len are negative, or if off + len is greater than cbuf.length.
        java.io.IOException - If an I/O error occurs while calling the underlying reader's read method.
        See Also:
        Reader.read(char[], int, int)
      • reset

        public void reset()
                   throws java.io.IOException
        Resets the target to the latest mark,
        Overrides:
        reset in class ProxyReader
        Throws:
        java.io.IOException - If an I/O error occurs while calling the underlying reader's reset method.
        See Also:
        Reader.reset()
      • skip

        public long skip​(long n)
                  throws java.io.IOException
        Description copied from class: ProxyReader
        Invokes the delegate's skip(long) method.
        Overrides:
        skip in class ProxyReader
        Parameters:
        n - the number of bytes to skip.
        Returns:
        the number of bytes to skipped or EOF if the end of stream.
        Throws:
        java.io.IOException - if an I/O error occurs.