Class LineReader

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class LineReader
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Fast class to read a file, line per line.

    Lines must be split using Unix newlines \n, linefeeds \r are ignored.

    This is a rather minimal implementation, which supposedly pays off in performance. In particular, this class allows recycling the buffer, which will yield less object allocations and thus less garbage collection.

    Usage example:

     StringBuilder buf = new StringBuilder();
     LineReader reader = new LineReader(inputStream);
     // Clear buffer, and append next line.
     while(reader.readLine(buf.setLength(0))) {
       // process string in buffer.
     }
     
    Since:
    0.7.0
    Author:
    Erich Schubert
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) char[] buffer
      Character buffer
      private static int BUFFER_SIZE
      Default buffer size to use
      (package private) int end
      Current position, and length of buffer
      (package private) java.io.Reader in
      Input stream to read from
      (package private) int pos
      Current position, and length of buffer
    • Constructor Summary

      Constructors 
      Constructor Description
      LineReader​(java.io.InputStream in)
      Constructor
      LineReader​(java.io.InputStream in, int buffersize)
      Constructor
      LineReader​(java.io.Reader in)
      Constructor
      LineReader​(java.io.Reader in, int buffersize)
      Constructor
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      boolean readLine​(java.lang.Appendable buf)
      Read a line into the given buffer.
      • Methods inherited from class java.lang.Object

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

      • BUFFER_SIZE

        private static final int BUFFER_SIZE
        Default buffer size to use
        See Also:
        Constant Field Values
      • in

        java.io.Reader in
        Input stream to read from
      • buffer

        char[] buffer
        Character buffer
      • pos

        int pos
        Current position, and length of buffer
      • end

        int end
        Current position, and length of buffer
    • Constructor Detail

      • LineReader

        public LineReader​(java.io.InputStream in)
        Constructor
        Parameters:
        in - Stream
      • LineReader

        public LineReader​(java.io.Reader in)
        Constructor
        Parameters:
        in - Reader
      • LineReader

        public LineReader​(java.io.InputStream in,
                          int buffersize)
        Constructor
        Parameters:
        in - Reader
        buffersize - Buffer size
      • LineReader

        public LineReader​(java.io.Reader in,
                          int buffersize)
        Constructor
        Parameters:
        in - Reader
        buffersize - Buffer size
    • Method Detail

      • readLine

        public boolean readLine​(java.lang.Appendable buf)
                         throws java.io.IOException
        Read a line into the given buffer.
        Parameters:
        buf - Buffer.
        Returns:
        true if some characters have been read.
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.io.IOException