Class ReaderCharSequence

  • All Implemented Interfaces:
    java.lang.CharSequence

    public class ReaderCharSequence
    extends java.lang.Object
    implements java.lang.CharSequence
    Adapter of reader class to CharSequence interface. Due to behavior differences CharSequence is not implemented perfectly.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      char charAt​(int index)  
      private void fillBuffer​(int index)  
      private int getMinIndex()  
      private int getRelativeIndex​(int index)
      Calculate buffer relative index from sequence index.
      int length()  
      private void readCharacter()  
      java.lang.CharSequence subSequence​(int start, int end)
      The length of returned subsequence can be smaller than (end - start) when the end of stream is reached.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.CharSequence

        chars, codePoints, toString
    • Field Detail

      • INFINITE_LENGTH

        public static final int INFINITE_LENGTH
        Value to be used as length when it is unknown. It must be less than Integer.MAX_VALUE because some code may add something to length.
        See Also:
        Constant Field Values
      • reader

        private java.io.Reader reader
      • lookahead

        private int lookahead
      • buffer

        private Buffer buffer
      • position

        private int position
      • length

        private int length
    • Constructor Detail

      • ReaderCharSequence

        public ReaderCharSequence​(java.io.Reader reader,
                                  int bufferLength,
                                  int length,
                                  int lookahead)
        Create.
        Parameters:
        reader - reader from which char sequence will be read
        bufferLength - size of the character buffer
        length - length of the input; when it cannot be determined it can be set to INFINITE_LENGTH (max value); cannot be set to Integer.MAX_VALUE because it may cause overflow.
        lookahead - number of characters to read after current position
      • ReaderCharSequence

        public ReaderCharSequence​(java.io.Reader reader,
                                  int bufferLength,
                                  int length)
      • ReaderCharSequence

        public ReaderCharSequence​(java.io.Reader reader,
                                  int bufferLength)
      • ReaderCharSequence

        public ReaderCharSequence​(java.io.Reader reader)
    • Method Detail

      • length

        public int length()
        Specified by:
        length in interface java.lang.CharSequence
      • charAt

        public char charAt​(int index)
        Specified by:
        charAt in interface java.lang.CharSequence
      • subSequence

        public java.lang.CharSequence subSequence​(int start,
                                                  int end)
        The length of returned subsequence can be smaller than (end - start) when the end of stream is reached.
        Specified by:
        subSequence in interface java.lang.CharSequence
      • getRelativeIndex

        private int getRelativeIndex​(int index)

        Calculate buffer relative index from sequence index.

         The equation is this:
         relative index = index - (position - buffer length)
         Example:
         position = 10
         buffer length = 5
         index = 7
                            
                            |   |
         position - buffer  |   |  index
                    length  V   V
         
         stream  |-|-|-|-|-|-|-|-|-|-|
                  0 1 2 3 4 5 6 7 8 9 10  <--- position
         
                                |
                                |  relative index
                                V
         
         buffer            |-|-|-|-|-|
                            0 1 2 3 4 
         
         relative index = 2
         
        Parameters:
        index - sequence index
        Returns:
        buffer relative index
      • getMinIndex

        private int getMinIndex()
      • fillBuffer

        private void fillBuffer​(int index)
      • readCharacter

        private void readCharacter()