Package com.mastfrog.util.streams
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 Summary
ConstructorsConstructorDescriptionContinuousLineStream(ContinuousStringStream stringStream, CharsetDecoder charsetDecoder, int charsPerBuffer) ContinuousLineStream(ContinuousStringStream stringStream, CharsetDecoder charsetDecoder, int charsPerBuffer, int maxLinesToBuffer) -
Method Summary
Modifier and TypeMethodDescriptionlongvoidclose()Close the underlying stream.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.booleanDetermine if there are currently any more lines available.booleanhasNext()booleanbooleanisOpen()next()Get the next char sequence, returning none if at the end of the file or nothing further is available.nextLine()Get the next line, if any.static ContinuousLineStreamCreate a UTF-8 line stream with a buffer size of 8192 bytes.static ContinuousLineStreamCreate a UTF-8 line stream with the specified byte buffer size.static ContinuousLineStreamCreate a line stream for the specified encoding with the specified byte buffer size.static ContinuousLineStreamCreate a UTF-8 line stream with a buffer size of 8192 bytes.static ContinuousLineStreamCreate a UTF-8 line stream with the specified byte buffer size.static ContinuousLineStreamCreate a line stream for the specified encoding with the specified byte buffer size.longposition()Get the current position in the filevoidposition(long pos) Change the position for reading in the file, discarding any enqueued partial or complete lines.In the case that the file did not end with a newline, get the remaining data from itvoidremove()Throws a UOE.Return this as an iterable.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Iterator
forEachRemaining
-
Constructor Details
-
ContinuousLineStream
public ContinuousLineStream(ContinuousStringStream stringStream, CharsetDecoder charsetDecoder, int charsPerBuffer) -
ContinuousLineStream
public ContinuousLineStream(ContinuousStringStream stringStream, CharsetDecoder charsetDecoder, int charsPerBuffer, int maxLinesToBuffer)
-
-
Method Details
-
of
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
Create a UTF-8 line stream with the specified byte buffer size.- Parameters:
file- The filebufferSize- 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
Create a line stream for the specified encoding with the specified byte buffer size.- Parameters:
file- The filebufferSize- 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 setcharset- The character set the file's data uses- Returns:
- Throws:
IllegalArgumentException- if the file is not readable
-
of
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
Create a UTF-8 line stream with the specified byte buffer size.- Parameters:
file- The filebufferSize- 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
Create a line stream for the specified encoding with the specified byte buffer size.- Parameters:
file- The filebufferSize- 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 setcharset- The character set the file's data uses- Returns:
- Throws:
IllegalArgumentException- if the file is not readable
-
isOpen
public boolean isOpen() -
hasMoreLines
Determine if there are currently any more lines available.- Returns:
- True if there are more lines
- Throws:
IOException- If something goes wrong
-
nextLine
Get the next line, if any.- Returns:
- The next line, or null if there are none
- Throws:
IOException- If something goes wrong
-
position
Get the current position in the file- Returns:
- The file position
- Throws:
IOException- If the channel is closed or something else is wrong
-
position
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
In the case that the file did not end with a newline, get the remaining data from it- Returns:
- The remaining data
-
available
- Throws:
IOException
-
closeReturningTail
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
- Throws:
IOException
-
close
Close the underlying stream. Note there may still be partial lines or an unfinished line held when this instance is closed.- Specified by:
closein interfaceAutoCloseable- Throws:
IOException- If something goes wrong
-
hasNext
public boolean hasNext()- Specified by:
hasNextin interfaceIterator<CharSequence>
-
next
Get the next char sequence, returning none if at the end of the file or nothing further is available.- Specified by:
nextin interfaceIterator<CharSequence>- Returns:
- A char sequence or null
-
remove
public void remove()Throws a UOE.- Specified by:
removein interfaceIterator<CharSequence>
-
toIterable
Return this as an iterable.- Returns:
- An iterable of char sequences
-