Class UStringWriter

java.lang.Object
java.io.Writer
cl.netswitch.lib.util.UStringWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable

public final class UStringWriter extends Writer
A character stream that collects its output in an unsynchronized string builder, which can then be used to construct a string.

Closing a UStringWriter has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

  • Constructor Details

    • UStringWriter

      public UStringWriter()
      Create a new string writer using the default initial string builder size.
    • UStringWriter

      public UStringWriter(int initialSize)
      Create a new string writer using the specified initial string builder size.
      Parameters:
      initialSize - the number of char values that will fit into the string builder before it is automatically expanded.
      Throws:
      IllegalArgumentException - if initialSize is negative.
  • Method Details

    • write

      public void write(int ch)
      Writes a single character.
      Overrides:
      write in class Writer
      Parameters:
      ch - the character to be written.
    • write

      public void write(char[] chars, int offset, int length)
      Writes a portion of an array of characters.
      Specified by:
      write in class Writer
      Parameters:
      chars - an array of characters.
      offset - the offset from which to start writing characters.
      length - the number of characters to write.
      Throws:
      IndexOutOfBoundsException - if arguments are out-of-bounds.
    • write

      public void write(String str)
      Writes a string.
      Overrides:
      write in class Writer
      Parameters:
      str - the string to be written.
    • write

      public void write(String str, int offset, int length)
      Writes a portion of a string.
      Overrides:
      write in class Writer
      Parameters:
      str - the string to be written.
      offset - the offset from which to start writing characters.
      length - the number of characters to write.
    • append

      public UStringWriter append(CharSequence csq)
      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 UStringWriter append(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 UStringWriter append(char ch)
      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:
      ch - the character to append.
      Returns:
      this writer.
    • toString

      public String toString()
      Returns the current contents of the buffer as a string.
      Overrides:
      toString in class Object
      Returns:
      the current contents of the buffer as a string.
    • getBuffer

      public StringBuilder getBuffer()
      Returns the buffer holding the current contents of this writer.
      Returns:
      the buffer holding the current contents of this writer.
    • flush

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

      public void close() throws IOException
      Closes this writer. Closing a UStringWriter 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
      Throws:
      IOException - if an I/O error occurs.