Class NonBlockingBufferedWriter

java.lang.Object
java.io.Writer
com.helger.commons.io.stream.NonBlockingBufferedWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable

@NotThreadSafe public class NonBlockingBufferedWriter extends Writer
This is a non-blocking version of BufferedWriter. It is 1:1 rip without the synchronized statements.
Author:
Philip Helger
  • Constructor Details

    • NonBlockingBufferedWriter

      public NonBlockingBufferedWriter(@Nonnull Writer aWriter)
      Creates a buffered character-output stream that uses a default-sized output buffer.
      Parameters:
      aWriter - A Writer
    • NonBlockingBufferedWriter

      public NonBlockingBufferedWriter(@Nonnull Writer aWriter, @Nonnegative int nBufSize)
      Creates a new buffered character-output stream that uses an output buffer of the given size.
      Parameters:
      aWriter - A Writer
      nBufSize - Output-buffer size, a positive integer
      Throws:
      IllegalArgumentException - If size is ≤ 0
  • Method Details

    • flushBuffer

      protected void flushBuffer() throws IOException
      Flushes the output buffer to the underlying character stream, without flushing the stream itself. This method is non-private only so that it may be invoked by PrintStream.
      Throws:
      IOException - of the writer is not open
    • write

      public void write(int c) throws IOException
      Writes a single character.
      Overrides:
      write in class Writer
      Throws:
      IOException - If an I/O error occurs
    • write

      public void write(char[] cbuf, int nOfs, int nLen) throws IOException
      Writes a portion of an array of characters.

      Ordinarily this method stores characters from the given array into this stream's buffer, flushing the buffer to the underlying stream as needed. If the requested length is at least as large as the buffer, however, then this method will flush the buffer and write the characters directly to the underlying stream. Thus redundant BufferedWriters will not copy data unnecessarily.

      Specified by:
      write in class Writer
      Parameters:
      cbuf - A character array
      nOfs - Offset from which to start reading characters
      nLen - Number of characters to write
      Throws:
      IOException - If an I/O error occurs
    • write

      public void write(String s, int off, int len) throws IOException
      Writes a portion of a String.

      If the value of the len parameter is negative then no characters are written. This is contrary to the specification of this method in the superclass, which requires that an IndexOutOfBoundsException be thrown.

      Overrides:
      write in class Writer
      Parameters:
      s - String to be written
      off - Offset from which to start reading characters
      len - Number of characters to be written
      Throws:
      IOException - If an I/O error occurs
    • reset

      public void reset()
      Remove all content of the buffer.
    • getSize

      @Nonnegative public int getSize()
    • getBufferSize

      @Nonnegative public int getBufferSize()
    • isEmpty

      public boolean isEmpty()
    • getAsString

      @Nonnull @ReturnsMutableCopy public String getAsString()
      Converts input data to a string.
      Returns:
      the string.
    • getAsString

      @Nonnull public String getAsString(@Nonnegative int nLength)
      Converts input data to a string.
      Parameters:
      nLength - The number of characters to convert. Must be ≤ than getSize().
      Returns:
      the string.
    • newLine

      public void newLine() throws IOException
      Writes a line separator. The line separator string is defined by the system property line.separator, and is not necessarily a single newline ('\n') character.
      Throws:
      IOException - If an I/O error occurs
    • flush

      public void flush() throws IOException
      Flushes the stream.
      Specified by:
      flush in interface Flushable
      Specified by:
      flush in class Writer
      Throws:
      IOException - If an I/O error occurs
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class Writer
      Throws:
      IOException