Class LineBufferedReader

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.lang.Readable
    Direct Known Subclasses:
    JsonTokenizer

    public class LineBufferedReader
    extends java.io.Reader
    Helper class that manages a buffer if 1 line, or if requested pre-loads the content of the input reader, and pretends as if it was the same line-buffering reader. The whole point of this class is to be a base for tokenizer classes JSON and other simple text-based syntax parsers. The mix of the Reader and the line buffering enables the tokenizer to be pretty efficient (minimal data conversion) while having access to a stream of characters to be parsed. For this reason all of the internal state fields are made protected, though extending classes should not assign any fields but the 'lastChar'.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      java.lang.String getLine()
      Returns the current line in the buffer.
      int getLineNo()  
      int getLinePos()
      The position of the current (last read) char in the current line.
      java.util.List<java.lang.String> getRemainingLines​(boolean trimAndSkipEmpty)
      Read the rest of input from the reader, and get the lines from there.
      java.lang.String getRestOfLine()
      Return the rest of the current line.
      protected void maybeConsolidateBuffer()
      If the char buffer is nearing it's "end" and does not end with a newline (meaning it is a complete line), then take the reast of the current buffer and move it to the front of the buffer, and read until end of buffer, or end of line.
      int read()  
      int read​(char[] chars, int off, int len)  
      protected boolean readNextChar()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from class java.io.Reader

        mark, markSupported, read, read, ready, reset, skip, transferTo
    • Field Detail

      • reader

        protected final java.io.Reader reader
      • buffer

        protected final char[] buffer
      • preLoaded

        protected final boolean preLoaded
      • bufferLimit

        protected int bufferLimit
      • bufferLineEnd

        protected boolean bufferLineEnd
      • bufferOffset

        protected int bufferOffset
      • lineNo

        protected int lineNo
      • linePos

        protected int linePos
      • lastChar

        protected int lastChar
    • Constructor Detail

      • LineBufferedReader

        public LineBufferedReader​(java.io.Reader reader)
      • LineBufferedReader

        public LineBufferedReader​(java.io.Reader reader,
                                  int bufferSize)
      • LineBufferedReader

        public LineBufferedReader​(java.io.Reader reader,
                                  boolean preLoadAll)
      • LineBufferedReader

        public LineBufferedReader​(java.io.Reader reader,
                                  int bufferSize,
                                  boolean preLoadAll)
    • 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​(@Nonnull
                        char[] chars,
                        int off,
                        int len)
                 throws java.io.IOException
        Specified by:
        read in class java.io.Reader
        Throws:
        java.io.IOException
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in class java.io.Reader
      • getLineNo

        public int getLineNo()
        Returns:
        The current line number.
      • getLinePos

        public int getLinePos()
        The position of the current (last read) char in the current line.
        Returns:
        The char position.
      • getLine

        @Nonnull
        public java.lang.String getLine()
        Returns the current line in the buffer. Or empty string if not usable.
        Returns:
        The line string, not including the line-break.
      • getRestOfLine

        @Nonnull
        public java.lang.String getRestOfLine()
                                       throws java.io.IOException
        Return the rest of the current line. This is handy for handling unwanted content after the last expected token or character.
        Returns:
        The rest of the last read line. Not including leading and ending whitespaces, since these are allowed.
        Throws:
        java.io.IOException - If unable to read the rest of the line.
      • getRemainingLines

        @Nonnull
        public java.util.List<java.lang.String> getRemainingLines​(boolean trimAndSkipEmpty)
                                                           throws java.io.IOException
        Read the rest of input from the reader, and get the lines from there. This will consume the rest of the content of the reader.
        Parameters:
        trimAndSkipEmpty - If lines should be trimmed and empty lines should be skipped.
        Returns:
        List of lines after the current.
        Throws:
        java.io.IOException - When failing to read stream to end.
      • maybeConsolidateBuffer

        protected void maybeConsolidateBuffer()
                                       throws java.io.IOException
        If the char buffer is nearing it's "end" and does not end with a newline (meaning it is a complete line), then take the reast of the current buffer and move it to the front of the buffer, and read until end of buffer, or end of line.
        Throws:
        java.io.IOException - On IO errors.
      • readNextChar

        protected boolean readNextChar()
                                throws java.io.IOException
        Throws:
        java.io.IOException