Final

java.io
Class StringWriter

java.lang.Object
  extended by java.io.Writer
      extended by java.io.StringWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable

public class StringWriter
extends Writer

StringWriter is an class for writing Character Streams to a StringBuffer. The characters written can then be returned as a String. This is used for capturing output sent to a Writer by substituting a StringWriter.

See Also:
StringReader

Field Summary
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
StringWriter()
          Constructs a new StringWriter which has a StringBuffer allocated with the default size of 16 characters.
StringWriter(int initialSize)
          Constructs a new StringWriter which has a StringBuffer allocated with the size of initialSize characters.
 
Method Summary
 StringWriter append(char c)
          Appends the given character to the contents of the receiver.
 StringWriter append(CharSequence sequence)
          Appends the given character sequence to the contents of the receiver.
 StringWriter append(CharSequence sequence, int start, int stop)
          Appends the given character sequence to the contents of the receiver.
 void close()
          Close this Writer.
 void flush()
          Flush this Writer.
 StringBuffer getBuffer()
          Answer the contents of this StringWriter as a StringBuffer.
 String toString()
          Answer the contents of this StringWriter as a String.
 void write(char[] buffer, int offset, int count)
          Writes count characters starting at offset in buffer to this StringWriter.
 void write(int oneChar)
          Writes the specified character oneChar to this StringWriter.
 void write(String str)
          Writes the characters from the String str to this StringWriter.
 void write(String str, int offset, int count)
          Writes count number of characters starting at offset from the String str to this StringWriter.
 
Methods inherited from class java.io.Writer
write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StringWriter

public StringWriter()
Constructs a new StringWriter which has a StringBuffer allocated with the default size of 16 characters. The StringBuffer is also the lock used to synchronize access to this Writer.


StringWriter

public StringWriter(int initialSize)
Constructs a new StringWriter which has a StringBuffer allocated with the size of initialSize characters. The StringBuffer is also the lock used to synchronize access to this Writer.

Parameters:
initialSize - the intial number of characters
Method Detail

close

public void close()
           throws IOException
Close this Writer. This is the concrete implementation required. This particular implementation does nothing.

Specified by:
close in interface Closeable
Specified by:
close in class Writer
Throws:
IOException - If an IO error occurs closing this StringWriter.

flush

public void flush()
Flush this Writer. This is the concrete implementation required. This particular implementation does nothing.

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

getBuffer

public StringBuffer getBuffer()
Answer the contents of this StringWriter as a StringBuffer. Any changes made to the StringBuffer by the receiver or the caller are reflected in this StringWriter.

Returns:
this StringWriters local StringBuffer.

toString

public String toString()
Answer the contents of this StringWriter as a String. Any changes made to the StringBuffer by the receiver after returning will not be reflected in the String returned to the caller.

Overrides:
toString in class Object
Returns:
this StringWriters current contents as a String.

write

public void write(char[] buffer,
                  int offset,
                  int count)
Writes count characters starting at offset in buffer to this StringWriter.

Specified by:
write in class Writer
Parameters:
buffer - the non-null array containing characters to write.
offset - offset in buf to retrieve characters
count - maximum number of characters to write
Throws:
ArrayIndexOutOfBoundsException - If offset or count are outside of bounds.

write

public void write(int oneChar)
Writes the specified character oneChar to this StringWriter. This implementation writes the low order two bytes to the Stream.

Overrides:
write in class Writer
Parameters:
oneChar - The character to write

write

public void write(String str)
Writes the characters from the String str to this StringWriter.

Overrides:
write in class Writer
Parameters:
str - the non-null String containing the characters to write.

write

public void write(String str,
                  int offset,
                  int count)
Writes count number of characters starting at offset from the String str to this StringWriter.

Overrides:
write in class Writer
Parameters:
str - the non-null String containing the characters to write.
offset - the starting point to retrieve characters.
count - the number of characters to retrieve and write.
Throws:
ArrayIndexOutOfBoundsException - If offset or count are outside of bounds.

append

public StringWriter append(char c)
Description copied from interface: Appendable
Appends the given character to the contents of the receiver.

Specified by:
append in interface Appendable
Overrides:
append in class Writer
Parameters:
c - the character to append
Returns:
the receiver
See Also:
Appendable.append(char)

append

public StringWriter append(CharSequence sequence)
Description copied from interface: Appendable
Appends the given character sequence to the contents of the receiver.

Specified by:
append in interface Appendable
Overrides:
append in class Writer
Parameters:
sequence - the character sequence to append, if null then the String "null" is appended
Returns:
the receiver
See Also:
Appendable.append(java.lang.CharSequence)

append

public StringWriter append(CharSequence sequence,
                           int start,
                           int stop)
Description copied from interface: Appendable
Appends the given character sequence to the contents of the receiver.

Specified by:
append in interface Appendable
Overrides:
append in class Writer
Parameters:
sequence - the character sequence to append, if null then the String "null" is appended
start - the offset of the first character
stop - the offset one past the last character
Returns:
the receiver
See Also:
Appendable.append(java.lang.CharSequence, int, int)

Final

Licensed Materials - Property of IBM
© Copyright IBM Corp. 2006, 2008 All Rights Reserved.