org.apache.commons.io.input

## Class BoundedReader

- java.lang.Object
- - java.io.Reader
  - - org.apache.commons.io.input.BoundedReader

- All Implemented Interfaces:
    Closeable, AutoCloseable, Readable

      public class BoundedReader
      extends Reader

  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.Reader

      `lock`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `BoundedReader(Reader target, int maxCharsFromTargetReader)` Constructs a bounded reader

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()` Closes the target
    `void` | `mark(int readAheadLimit)` marks the target stream
    `int` | `read()` Reads a single character
    `int` | `read(char[] cbuf, int off, int len)` Reads into an array
    `void` | `reset()` Resets the target to the latest mark,

    - ### Methods inherited from class java.io.Reader

      `markSupported, read, read, ready, skip`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### BoundedReader

          public BoundedReader(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

    - #### close

          public void close()
                     throws IOException

      Closes the target
      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Specified by:
        `close` in class `Reader`
      Throws:
        `IOException` - If an I/O error occurs while calling the underlying reader's close method

    - #### reset

          public void reset()
                     throws IOException

      Resets the target to the latest mark,
      Overrides:
        `reset` in class `Reader`
      Throws:
        `IOException` - If an I/O error occurs while calling the underlying reader's reset method
      See Also:
        `Reader.reset()`

    - #### mark

          public void mark(int readAheadLimit)
                    throws IOException

      marks the target stream
      Overrides:
        `mark` in class `Reader`
      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:
        `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 IOException

      Reads a single character
      Overrides:
        `read` in class `Reader`
      Returns:
        -1 on EOF or the character read
      Throws:
        `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 IOException

      Reads into an array
      Specified by:
        `read` in class `Reader`
      Parameters:
        `cbuf` - The buffer to fill
        `off` - The offset
        `len` - The number of chars to read
      Returns:
        the number of chars read
      Throws:
        `IOException` - If an I/O error occurs while calling the underlying reader's read method
      See Also:
        `Reader.read(char[], int, int)`

Copyright © 2002–2021 The Apache Software Foundation. All rights reserved.