Class NonBlockingStringWriter

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

@NotThreadSafe public class NonBlockingStringWriter extends Writer implements IHasSize
A non-synchronized copy of the class StringWriter.
It uses StringBuilder instead of StringBuffer and therefore does not need synchronized access!
Author:
Philip Helger
See Also:
  • Constructor Details

    • NonBlockingStringWriter

      public NonBlockingStringWriter()
      Create a new string writer using the default initial string-buffer size.
    • NonBlockingStringWriter

      public NonBlockingStringWriter(@Nonnegative int nInitialSize)
      Create a new string writer using the specified initial string-buffer size.
      Parameters:
      nInitialSize - The number of char values that will fit into this buffer before it is automatically expanded
      Throws:
      IllegalArgumentException - If initialSize is negative
  • Method Details

    • write

      public void write(int c)
      Write a single character.
      Overrides:
      write in class Writer
    • write

      public void write(@Nonnull char[] aBuf, @Nonnegative int nOfs, @Nonnegative int nLen)
      Write a portion of an array of characters.
      Specified by:
      write in class Writer
      Parameters:
      aBuf - Array of characters
      nOfs - Offset from which to start writing characters
      nLen - Number of characters to write
    • write

      public void write(@Nullable String sStr)
      Write a string.
      Overrides:
      write in class Writer
    • write

      public void write(@Nonnull String sStr, int nOfs, int nLen)
      Write a portion of a string.
      Overrides:
      write in class Writer
      Parameters:
      sStr - String to be written
      nOfs - Offset from which to start writing characters
      nLen - Number of characters to write
    • append

      Appends the specified character sequence to this writer.

      An invocation of this method of the form out.append(csq) behaves in exactly the same way as the invocation

       out.write (csq.toString ())
       

      Depending on the specification of toString for the character sequence csq, the entire sequence may not be appended. For instance, invoking the toString method of a character buffer will return a subsequence whose content depends upon the buffer's position and limit.

      Specified by:
      append in interface Appendable
      Overrides:
      append in class Writer
      Parameters:
      aCS - The character sequence to append. If csq is null, then the four characters "null" are appended to this writer.
      Returns:
      This writer
    • append

      public NonBlockingStringWriter append(CharSequence aCS, int nStart, int nEnd)
      Appends a subsequence of the specified character sequence to this writer.

      An invocation of this method of the form out.append(csq, start, end) when csq is not null, behaves in exactly the same way as the invocation

       out.write (csq.subSequence (start, end).toString ())
       
      Specified by:
      append in interface Appendable
      Overrides:
      append in class Writer
      Parameters:
      aCS - The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".
      nStart - The index of the first character in the subsequence
      nEnd - The index of the character following the last character in the subsequence
      Returns:
      This writer
      Throws:
      IndexOutOfBoundsException - If start or end are negative, start is greater than end, or end is greater than csq.length()
    • append

      public NonBlockingStringWriter append(char c)
      Appends the specified character to this writer.

      An invocation of this method of the form out.append(c) behaves in exactly the same way as the invocation

       out.write (c)
       
      Specified by:
      append in interface Appendable
      Overrides:
      append in class Writer
      Parameters:
      c - The 16-bit character to append
      Returns:
      This writer
    • flush

      public void flush()
      Flush the stream.
      Specified by:
      flush in interface Flushable
      Specified by:
      flush in class Writer
    • close

      public void close()
      Closing a StringWriter has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class Writer
    • directGetStringBuilder

      @Nonnull @ReturnsMutableObject("design") public StringBuilder directGetStringBuilder()
      Returns:
      The contained StringBuilder. Never null. Handle with care!
    • getAsCharArray

      @Nonnull @ReturnsMutableCopy public char[] getAsCharArray()
      Returns:
      Return the buffer's current value as a string.
    • getAsString

      @Nonnull public String getAsString()
      Returns:
      the buffer's current value as a string.
    • reset

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

      @Nonnegative public int size()
      Specified by:
      size in interface IHasSize
      Returns:
      The number of contained elements. Always ≥ 0.
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface IHasSize
      Returns:
      true if no items are present, false if at least a single item is present.
      See Also:
    • toString

      @Deprecated public String toString()
      Deprecated.
      Don't call this; use getAsString instead
      Overrides:
      toString in class Object
      Returns:
      the buffer's current value as a string.