Class RewindableReader

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

    public class RewindableReader
    extends java.io.Reader
    Caches the initial characters that are read from the supplied Reader and allows to rewind these. As soon as more than size characters have been read, rewinding fails.
    Author:
    Thorben Lindhauer
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected char[] buffer  
      protected int pos  
      protected boolean rewindable  
      protected java.io.PushbackReader wrappedReader  
      • Fields inherited from class java.io.Reader

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      RewindableReader​(java.io.Reader input, int size)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      int getCurrentRewindableCapacity()  
      int getRewindBufferSize()  
      void mark​(int readlimit)  
      boolean markSupported()  
      int read()  
      int read​(char[] cbuf, int off, int len)  
      void reset()  
      void rewind()
      Rewinds the reader such that the initial characters are returned when invoking read().
      long skip​(long n)  
      • Methods inherited from class java.io.Reader

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

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

      • wrappedReader

        protected java.io.PushbackReader wrappedReader
      • buffer

        protected char[] buffer
      • pos

        protected int pos
      • rewindable

        protected boolean rewindable
    • Constructor Detail

      • RewindableReader

        public RewindableReader​(java.io.Reader input,
                                int size)
    • Method Detail

      • read

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

        public int read()
                 throws java.io.IOException
        Overrides:
        read 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
      • mark

        public void mark​(int readlimit)
                  throws java.io.IOException
        Overrides:
        mark in class java.io.Reader
        Throws:
        java.io.IOException
      • markSupported

        public boolean markSupported()
        Overrides:
        markSupported in class java.io.Reader
      • reset

        public void reset()
                   throws java.io.IOException
        Overrides:
        reset in class java.io.Reader
        Throws:
        java.io.IOException
      • skip

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

        public void rewind()
                    throws java.io.IOException
        Rewinds the reader such that the initial characters are returned when invoking read(). Throws an exception if more than the buffering limit has already been read.
        Throws:
        java.io.IOException
      • getRewindBufferSize

        public int getRewindBufferSize()
      • getCurrentRewindableCapacity

        public int getCurrentRewindableCapacity()
        Returns:
        the number of characters that can still be read and rewound.