Class ContinuousLineStream

java.lang.Object
com.mastfrog.util.streams.ContinuousLineStream
All Implemented Interfaces:
AutoCloseable, Iterator<CharSequence>

public final class ContinuousLineStream extends Object implements AutoCloseable, Iterator<CharSequence>
Reads a file line-by-line, but unlike a BufferedReader, does not give up when the end of the file is reached. It is also conservative about the amount of memory it consumes and avoids excessive memory-copies.

Implements Iterable<CharSequence> for convenience (these methods can throw RuntimeExceptions wrapping any encountered IOException).

This class is not thread-safe.

  • Constructor Details

  • Method Details

    • of

      public static ContinuousLineStream of(Path file)
      Create a UTF-8 line stream with a buffer size of 8192 bytes.
      Parameters:
      file - The file
      Returns:
      Throws:
      IllegalArgumentException - if the file is not readable
    • of

      public static ContinuousLineStream of(Path file, int bufferSize)
      Create a UTF-8 line stream with the specified byte buffer size.
      Parameters:
      file - The file
      bufferSize - the size of the read buffer which will be used to collect lines - for better performance, the size should be > the average line length in bytes for the character set
      Returns:
      Throws:
      IllegalArgumentException - if the file is not readable
    • of

      public static ContinuousLineStream of(Path file, int bufferSize, Charset charset)
      Create a line stream for the specified encoding with the specified byte buffer size.
      Parameters:
      file - The file
      bufferSize - the size of the read buffer which will be used to collect lines - for better performance, the size should be > the average line length in bytes for the character set
      charset - The character set the file's data uses
      Returns:
      Throws:
      IllegalArgumentException - if the file is not readable
    • of

      public static ContinuousLineStream of(File file)
      Create a UTF-8 line stream with a buffer size of 8192 bytes.
      Parameters:
      file - The file
      Returns:
      Throws:
      IllegalArgumentException - if the file is not readable
    • of

      public static ContinuousLineStream of(File file, int bufferSize)
      Create a UTF-8 line stream with the specified byte buffer size.
      Parameters:
      file - The file
      bufferSize - the size of the read buffer which will be used to collect lines - for better performance, the size should be > the average line length in bytes for the character set
      Returns:
      Throws:
      IllegalArgumentException - if the file is not readable
    • of

      public static ContinuousLineStream of(File file, int bufferSize, Charset charset)
      Create a line stream for the specified encoding with the specified byte buffer size.
      Parameters:
      file - The file
      bufferSize - the size of the read buffer which will be used to collect lines - for better performance, the size should be > the average line length in bytes for the character set
      charset - The character set the file's data uses
      Returns:
      Throws:
      IllegalArgumentException - if the file is not readable
    • isOpen

      public boolean isOpen()
    • hasMoreLines

      public boolean hasMoreLines() throws IOException
      Determine if there are currently any more lines available.
      Returns:
      True if there are more lines
      Throws:
      IOException - If something goes wrong
    • nextLine

      public CharSequence nextLine() throws IOException
      Get the next line, if any.
      Returns:
      The next line, or null if there are none
      Throws:
      IOException - If something goes wrong
    • position

      public long position() throws IOException
      Get the current position in the file
      Returns:
      The file position
      Throws:
      IOException - If the channel is closed or something else is wrong
    • position

      public void position(long pos) throws IOException
      Change the position for reading in the file, discarding any enqueued partial or complete lines.
      Parameters:
      pos - The new position
      Throws:
      IOException - If the position is invalid or something else is wrong
    • remainingData

      public String remainingData()
      In the case that the file did not end with a newline, get the remaining data from it
      Returns:
      The remaining data
    • available

      public long available() throws IOException
      Throws:
      IOException
    • closeReturningTail

      public List<CharSequence> closeReturningTail() throws IOException
      Close this stream, returning any already read lines and any partial line for which a newline had not yet been encountered; if at the end of the file, this will be the tail.
      Returns:
      A list of any remaining strings
      Throws:
      IOException
    • isAtEndOfFile

      public boolean isAtEndOfFile() throws IOException
      Throws:
      IOException
    • close

      public void close() throws IOException
      Close the underlying stream. Note there may still be partial lines or an unfinished line held when this instance is closed.
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException - If something goes wrong
    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface Iterator<CharSequence>
    • next

      public CharSequence next()
      Get the next char sequence, returning none if at the end of the file or nothing further is available.
      Specified by:
      next in interface Iterator<CharSequence>
      Returns:
      A char sequence or null
    • remove

      public void remove()
      Throws a UOE.
      Specified by:
      remove in interface Iterator<CharSequence>
    • toIterable

      public Iterable<CharSequence> toIterable()
      Return this as an iterable.
      Returns:
      An iterable of char sequences