Class NonBlockingCharArrayWriter

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

public class NonBlockingCharArrayWriter extends Writer
A non-synchronized copy of the class CharArrayWriter.
It does not use the internal lock object.
Since:
8.6.4
Author:
Philip Helger
See Also:
  • Field Details

    • m_aBuf

      protected char[] m_aBuf
      The buffer where data is stored.
    • m_nCount

      protected int m_nCount
      The number of chars in the buffer.
  • Constructor Details

    • NonBlockingCharArrayWriter

      public NonBlockingCharArrayWriter()
      Creates a new NonBlockingCharArrayWriter.
    • NonBlockingCharArrayWriter

      public NonBlockingCharArrayWriter(@Nonnegative int nInitialSize)
      Creates a new NonBlockingCharArrayWriter with the specified initial size.
      Parameters:
      nInitialSize - an int specifying the initial buffer size.
      Throws:
      IllegalArgumentException - if initialSize is negative
  • Method Details

    • write

      public void write(int c)
      Writes a character to the buffer.
      Overrides:
      write in class Writer
    • write

      public void write(@Nonnull char[] aBuf)
      Writes an array of characters.
      Overrides:
      write in class Writer
      Parameters:
      aBuf - Array of characters to be written. May not be null.
      Since:
      9.4.3
    • write

      public void write(@Nonnull char[] aBuf, @Nonnegative int nOfs, @Nonnegative int nLen)
      Writes characters to the buffer.
      Specified by:
      write in class Writer
      Parameters:
      aBuf - the data to be written
      nOfs - the start offset in the data
      nLen - the number of chars that are written
    • write

      public void write(@Nonnull String sStr)
      Writes a string.
      Overrides:
      write in class Writer
      Parameters:
      sStr - String to be written. May not be null.
      Since:
      9.4.3
    • write

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

      public void writeTo(@Nonnull Writer out) throws IOException
      Writes the contents of the buffer to another character stream.
      Parameters:
      out - the output stream to write to
      Throws:
      IOException - If an I/O error occurs.
    • 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:
      csq - The character sequence to append. If csq is null, then the four characters "null" are appended to this writer.
      Returns:
      This writer
    • append

      public NonBlockingCharArrayWriter append(@Nullable CharSequence csq, int start, int end)
      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:
      csq - 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".
      start - The index of the first character in the subsequence
      end - 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 NonBlockingCharArrayWriter 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
    • reset

      public void reset()
      Resets the buffer so that you can use it again without throwing away the already allocated buffer.
    • toCharArray

      @Nonnull @ReturnsMutableCopy public char[] toCharArray()
      Returns a copy of the input data.
      Returns:
      an array of chars copied from the input data.
    • directGetBuffer

      @Nonnull @ReturnsMutableObject public char[] directGetBuffer()
      Returns:
      The internally used char array. Never null. Handle with care!
    • toByteArray

      @Nonnull @ReturnsMutableCopy public byte[] toByteArray(@Nonnull Charset aCharset)
      Returns a copy of the input data as bytes in the correct charset.
      Parameters:
      aCharset - The charset to be used. May not be null.
      Returns:
      an array of bytes. Never null.
    • getSize

      @Nonnegative public int getSize()
      Returns the current size of the buffer.
      Returns:
      an int representing the current size of the buffer.
    • getBufferSize

      @Nonnegative public int getBufferSize()
      Returns:
      The number of pre-allocated chars. Always ≥ 0.
    • isEmpty

      public boolean isEmpty()
    • isNotEmpty

      public boolean isNotEmpty()
    • startsWith

      public boolean startsWith(@Nonnull char[] aChars)
    • startsWith

      public boolean startsWith(@Nonnull char[] aChars, @Nonnegative int nOfs, @Nonnegative int nLen)
    • 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.
    • getAsString

      @Nonnull public String getAsString(@Nonnegative int nOfs, @Nonnegative int nLength)
      Converts input data to a string.
      Parameters:
      nOfs - The offset to start at. Must be ≥ 0.
      nLength - The number of characters to convert. Must be ≤ than getSize().
      Returns:
      the string.
    • flush

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

      public void close()
      Close the stream. This method does not release the buffer, since its contents might still be required. Note: Invoking this method in this class will have no effect.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class Writer