Class LimitReader

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

    public final class LimitReader
    extends java.io.Reader
    implements Limit
    A Reader that provides methods to limit the range that can be read from the reader.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean limitInPlace  
      private java.io.Reader reader  
      private int remainingCharacters  
      • Fields inherited from class java.io.Reader

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      LimitReader​(java.io.Reader reader)
      Construct a LimitReader and call the clearLimit() method.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int clearLimit()
      Clear any limit set by setLimit.
      void close()  
      int getLimit()
      return limit of the stream that can be read without throwing EOFException
      int read()  
      int read​(char[] c, int off, int len)  
      void setLimit​(int length)
      Set the limit of the stream that can be read.
      long skip​(long count)  
      • Methods inherited from class java.io.Reader

        mark, markSupported, nullReader, read, read, ready, reset, transferTo
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • remainingCharacters

        private int remainingCharacters
      • limitInPlace

        private boolean limitInPlace
      • reader

        private java.io.Reader reader
    • Constructor Detail

      • LimitReader

        public LimitReader​(java.io.Reader reader)
        Construct a LimitReader and call the clearLimit() method.
    • Method Detail

      • read

        public int read()
                 throws java.io.IOException
        Overrides:
        read in class java.io.Reader
        Throws:
        java.io.IOException
      • read

        public int read​(char[] c,
                        int off,
                        int len)
                 throws java.io.IOException
        Specified by:
        read in class java.io.Reader
        Throws:
        java.io.IOException
      • skip

        public long skip​(long count)
                  throws java.io.IOException
        Overrides:
        skip in class java.io.Reader
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in class java.io.Reader
        Throws:
        java.io.IOException
      • setLimit

        public void setLimit​(int length)
        Set the limit of the stream that can be read. After this call up to and including length characters can be read from or skipped in the stream. Any attempt to read more than length characters will result in an EOFException
        Specified by:
        setLimit in interface Limit
      • getLimit

        public final int getLimit()
        return limit of the stream that can be read without throwing EOFException
        Returns:
        the remaining characters left to be read from the stream
      • clearLimit

        public int clearLimit()
        Clear any limit set by setLimit. After this call no limit checking will be made on any read until a setLimit()) call is made.
        Specified by:
        clearLimit in interface Limit
        Returns:
        the number of bytes within the limit that have not been read. -1 if not limit was set.