Class StringBuilderWriter

  • All Implemented Interfaces:
    Closeable, Flushable, Appendable, AutoCloseable

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

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

    This class is an alternative of JDK java.io.StringWriter. It is backed up by StringBuilder for better performance without the need of synchronization of a StringBuffer.

    Instances of StringBuilderWriter are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringWriter be used.

    Since:
    4.0.7
    Author:
    Laurent Schoelens
    • Constructor Detail

      • StringBuilderWriter

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

        public StringBuilderWriter​(int initialSize)
        Create a new string-builder writer using the specified initial string-builder size.
        Parameters:
        initialSize - The number of char values that will fit into this StringBuilder before it is automatically expanded
        Throws:
        IllegalArgumentException - If initialSize is negative
      • StringBuilderWriter

        public StringBuilderWriter​(StringBuilder sb)
        Create a new string-builder writer using the specified string-builder.
        Parameters:
        sb - The string-builder to use. could be null and default-sized string-builder will be created
    • Method Detail

      • getBuilder

        public StringBuilder getBuilder()
        Return the string builder itself.
        Returns:
        StringBuilder holding the current string-builder value.
      • write

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

        public void write​(char[] cbuf)
                   throws IOException
        Writes an array of characters.
        Overrides:
        write in class Writer
        Parameters:
        cbuf - Array of characters to be written
        Throws:
        IOException - If an I/O error occurs
      • write

        public void write​(char[] cbuf,
                          int off,
                          int len)
                   throws IOException
        Writes a portion of an array of characters.
        Specified by:
        write in class Writer
        Parameters:
        cbuf - Array of characters
        off - Offset from which to start writing characters
        len - Number of characters to write
        Throws:
        IOException - If an I/O error occurs
      • write

        public void write​(String str)
                   throws IOException
        Writes a string.
        Overrides:
        write in class Writer
        Parameters:
        str - String to be written
        Throws:
        IOException - If an I/O error occurs
      • write

        public void write​(String str,
                          int off,
                          int len)
                   throws IOException
        Writes a portion of a string.
        Overrides:
        write in class Writer
        Parameters:
        str - A String
        off - Offset from which to start writing characters
        len - Number of characters to write
        Throws:
        IOException - If an I/O error occurs
      • append

        public Writer append​(CharSequence csq)
                      throws IOException
        Appends the specified character sequence to this writer.
        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
        Throws:
        IOException - If an I/O error occurs
      • toString

        public String toString()
        Return the buffer's current value as a string.
        Overrides:
        toString in class Object
      • flush

        public void flush()
        Flush the stream.

        The flush method of StringBuilderWriter does nothing.

        Specified by:
        flush in interface Flushable
        Specified by:
        flush in class Writer
      • close

        public void close()
        Closing a StringBuilderWriter 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