Class NonBlockingLineReader

java.lang.Object
eu.cloudnetservice.node.impl.service.defaults.log.NonBlockingLineReader
All Implemented Interfaces:
Closeable, AutoCloseable

final class NonBlockingLineReader extends Object implements Closeable
A reader that can read lines from a given reader without blocking the caller while waiting for a newline.
Since:
4.0
  • 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

      private final StringBuilder stringBuffer
    • reader

      private Reader reader
  • Constructor Details

    • NonBlockingLineReader

      public NonBlockingLineReader(@NonNull @NonNull Reader reader)
      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

      @Nullable public @Nullable String readLine() throws IOException
      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:
      1. The buffer is getting larger than MAX_STRING_BUFFER_SIZE, in that case the complete buffered content is returned to prevent a memory overuse.
      2. This reader was closed, in that case the complete buffered content is returned to allow getting even an incomplete line from the buffer.
      3. 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.
    • readLineFromBuffer

      @Nullable private @Nullable String 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:
      1. The buffer is getting larger than MAX_STRING_BUFFER_SIZE, in that case the complete buffered content is returned to prevent a memory overuse.
      2. This reader was closed, in that case the complete buffered content is returned to allow getting even an incomplete line from the buffer.
      3. If both the above cases are not met then null is returned.
      Returns:
      a string containing a line or null, as described above.
    • bufferToString

      @NonNull private @NonNull String bufferToString(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. If endLf is 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

      public boolean ready() throws IOException
      Returns true if the next call to readLine() 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

      public void close() throws IOException
      Closes the underlying reader.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - if an I/O error occurs.