BoundedReader (Commons IO 2.5)
Date: 2016-12-01

org.apache.commons.io.input

## Class BoundedReader

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

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

      public class BoundedReader
      extends java.io.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 java.io.Reader#mark(int), since reading too many characters removes the ability to do a successful reset.

  Since:
    2.5

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `private int` | `charsRead`
    `private static int` | `INVALID`
    `private int` | `markedAt`
    `private int` | `maxCharsFromTargetReader`
    `private int` | `readAheadLimit`
    `private java.io.Reader` | `target`

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

      `lock`

  - ### Constructor Summary

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

  - ### Method Summary

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

    - ### 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`

- - ### Field Detail

    - #### INVALID

          private static final int INVALID

      See Also:
        Constant Field Values

    - #### target

          private final java.io.Reader target

    - #### charsRead

          private int charsRead

    - #### markedAt

          private int markedAt

    - #### readAheadLimit

          private int readAheadLimit

    - #### maxCharsFromTargetReader

          private final int maxCharsFromTargetReader

  - ### Constructor Detail

    - #### BoundedReader

          public BoundedReader(java.io.Reader target,
                       int maxCharsFromTargetReader)
                        throws java.io.IOException

      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
      Throws:
        `java.io.IOException` - if mark fails

  - ### Method Detail

    - #### close

          public void close()
                     throws java.io.IOException

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

    - #### reset

          public void reset()
                     throws java.io.IOException

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

    - #### mark

          public void mark(int readAheadLimit)
                    throws java.io.IOException

      marks the target stream, @see java.io.Reader#mark(int).
      Overrides:
        `mark` in class `java.io.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:
        `java.io.IOException` - If an I/O error occurs while calling the underlying reader's mark method

    - #### read

          public int read()
                   throws java.io.IOException

      Reads a single character, @see java.io.Reader#read()
      Overrides:
        `read` in class `java.io.Reader`
      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

    - #### read

          public int read(char[] cbuf,
                 int off,
                 int len)
                   throws java.io.IOException

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

Copyright (c) 2002-2016 Apache Software Foundation