org.tbull.util
Class StringBuilderWriter

Object
  extended by java.io.Writer
      extended by org.tbull.util.StringBuilderWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable, CharSequence

public class StringBuilderWriter
extends Writer
implements CharSequence

Like StringWriter, but completely unsynchronized.

While StringWriter writes to a StringBuffer, which is fully synchronized, this class writes to a StringBuilder, which is the unsynchronized version of StringBuffer.

Use this if you don't need the sync or want to take care of that yourself.

You can use the StringBuilderWriter and the underlying StringBuilder interchangeably as long as you don't do that concurrently from several threads (or you provide for appropriate locking). The writer will always append to the current end of the buffer, regardless of what you do with the buffer. You get hold of the underlying StringBuilder either by constructing the writer with a self-supplied buffer, or by a call to getBuilder().

For your convenience, this class also implements the CharSequence interface, so that you can easily examine the accumulated string data without constructing an intermediate String object. Again, you can combine this with interleaving write access as long as you take care of concurrency issues.

Ceterum censeo HTML in Javadoc is the dumbest idea ever.

See Also:
CharSequenceReader

Constructor Summary
StringBuilderWriter()
          Creates a new stringbuilder writer.
StringBuilderWriter(int initialCapacity)
          Creates a new stringbuilder writer using the specified initial capacity.
StringBuilderWriter(StringBuilder sb)
          Creates a new stringbuilder writer using the supplied StringBuilder instead of creating a new one.
 
Method Summary
 StringBuilderWriter append(char c)
           
 StringBuilderWriter append(CharSequence csq)
           
 StringBuilderWriter append(CharSequence csq, int start, int end)
           
 char charAt(int index)
           
 void close()
          Does nothing.
 void flush()
          Does nothing.
 StringBuilder getBuilder()
          Returns the underlying StringBuilder.
 int length()
          Returns the number of characters in the accumulated string data.
 CharSequence subSequence(int start, int end)
           
 String toString()
          Returns the results of the operation so far.
 void write(char[] cbuf, int off, int len)
           
 void write(int c)
           
 void write(String str)
           
 void write(String str, int off, int len)
           
 
Methods inherited from class java.io.Writer
write
 
Methods inherited from class Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StringBuilderWriter

public StringBuilderWriter()
Creates a new stringbuilder writer.


StringBuilderWriter

public StringBuilderWriter(int initialCapacity)
                    throws IllegalArgumentException
Creates a new stringbuilder writer using the specified initial capacity.

Parameters:
initialCapacity - The number of char values that will fit into the buffer before it is automatically expanded.
Throws:
IllegalArgumentException - If initialCapacity <= 0.

StringBuilderWriter

public StringBuilderWriter(StringBuilder sb)
Creates a new stringbuilder writer using the supplied StringBuilder instead of creating a new one. The buffer does not need to be empty. The writer always appends to the end of the buffer.

Method Detail

getBuilder

public StringBuilder getBuilder()
Returns the underlying StringBuilder. This is a life object and still in use! It is safe to continue using the writer afterwards, you can even intermix calls to the writer and to the stringbuilder, as long as you don't do that concurrently from several threads. The writer will always append to the current end of the stringbuilder.


charAt

public char charAt(int index)
            throws IndexOutOfBoundsException
Specified by:
charAt in interface CharSequence
Throws:
IndexOutOfBoundsException

length

public int length()
Returns the number of characters in the accumulated string data.

Specified by:
length in interface CharSequence

subSequence

public CharSequence subSequence(int start,
                                int end)
                         throws IndexOutOfBoundsException
Specified by:
subSequence in interface CharSequence
Throws:
IndexOutOfBoundsException

toString

public String toString()
Returns the results of the operation so far.

Specified by:
toString in interface CharSequence
Overrides:
toString in class Object

append

public StringBuilderWriter append(char c)
Specified by:
append in interface Appendable
Overrides:
append in class Writer

append

public StringBuilderWriter append(CharSequence csq,
                                  int start,
                                  int end)
                           throws IndexOutOfBoundsException
Specified by:
append in interface Appendable
Overrides:
append in class Writer
Throws:
IndexOutOfBoundsException

append

public StringBuilderWriter append(CharSequence csq)
Specified by:
append in interface Appendable
Overrides:
append in class Writer

write

public void write(int c)
Overrides:
write in class Writer

write

public void write(char[] cbuf,
                  int off,
                  int len)
           throws IndexOutOfBoundsException
Specified by:
write in class Writer
Throws:
IndexOutOfBoundsException

write

public void write(String str)
Overrides:
write in class Writer

write

public void write(String str,
                  int off,
                  int len)
           throws IndexOutOfBoundsException
Overrides:
write in class Writer
Throws:
IndexOutOfBoundsException

close

public void close()
Does nothing.

Specified by:
close in interface Closeable
Specified by:
close in class Writer

flush

public void flush()
Does nothing.

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