Class NonBlockingLineReader
java.lang.Object
eu.cloudnetservice.node.impl.service.defaults.log.NonBlockingLineReader
- All Implemented Interfaces:
Closeable, AutoCloseable
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intprivate final char[]private static final intprivate Readerprivate final StringBuilder -
Constructor Summary
ConstructorsConstructorDescriptionNonBlockingLineReader(@NonNull Reader reader) Constructs a new non-blocking line reader. -
Method Summary
Modifier and TypeMethodDescriptionbufferToString(int endIndex, boolean endLf) Converts the buffered chars into a string beginning at the first char in the buffer up until the given end index.voidclose()Closes the underlying reader.private intFinds the position of a newline character in the string buffer.readLine()Tries to read a line by reading from the underlying reader, unless this reader was closed.Tries to find a line separator in the buffered content and constructs a string from the chars up until the line separator.booleanready()Returns true if the next call toreadLine()will read something from the underlying buffer or if this reader is closed but some content is still available to be read.
-
Field Details
-
CHAR_BUFFER_SIZE
private static final int CHAR_BUFFER_SIZE- See Also:
-
MAX_STRING_BUFFER_SIZE
private static final int MAX_STRING_BUFFER_SIZE- See Also:
-
charBuffer
private final char[] charBuffer -
stringBuffer
-
reader
-
-
Constructor Details
-
NonBlockingLineReader
Constructs a new non-blocking line reader.- Parameters:
reader- the reader to delegate reader operations to.- Throws:
NullPointerException- if the given reader is null.
-
-
Method Details
-
readLine
Tries to read a line by reading from the underlying reader, unless this reader was closed. If the reader was closed or no line separator was found in the read content, there are three possibilities:- The buffer is getting larger than
MAX_STRING_BUFFER_SIZE, in that case the complete buffered content is returned to prevent a memory overuse. - This reader was closed, in that case the complete buffered content is returned to allow getting even an incomplete line from the buffer.
- If both the above cases are not met then null is returned.
- Returns:
- a string containing a line or null, as described above.
- Throws:
IOException- if an I/O error occurs.
- The buffer is getting larger than
-
readLineFromBuffer
Tries to find a line separator in the buffered content and constructs a string from the chars up until the line separator. If no line separator is found, there are three possibilities:- The buffer is getting larger than
MAX_STRING_BUFFER_SIZE, in that case the complete buffered content is returned to prevent a memory overuse. - This reader was closed, in that case the complete buffered content is returned to allow getting even an incomplete line from the buffer.
- If both the above cases are not met then null is returned.
- Returns:
- a string containing a line or null, as described above.
- The buffer is getting larger than
-
bufferToString
Converts the buffered chars into a string beginning at the first char in the buffer up until the given end index. IfendLfis true the CR, LF or CRLF is stripped from the buffer, else the last char is included in the returned string as well. The returned chars are removed from the string buffer.- Parameters:
endIndex- the index to which the chars should be included in the returned string.endLf- if the current buffer ends with an LF or CR.- Returns:
- a string constructed from the first char to the given end index of the string buffer.
-
findNewlineIndex
private int findNewlineIndex()Finds the position of a newline character in the string buffer. This method returns the index of a single LF or CR or the position of the LF in a CRLF case.- Returns:
- the index of the first newline, or -1 if no newline char was found.
-
ready
Returns true if the next call toreadLine()will read something from the underlying buffer or if this reader is closed but some content is still available to be read.- Returns:
- true if some content can be read immediately.
- Throws:
IOException- if an I/O error occurs.
-
close
Closes the underlying reader.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if an I/O error occurs.
-