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

This class writes chars to an internal StringBuffer that can then be used to retrieve a String.


Field Summary
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
StringWriter()
          This method initializes a new StringWriter to write to a StringBuffer initially sized to a default size of 16 chars.
StringWriter(int size)
          This method initializes a new StringWriter to write to a StringBuffer with the specified initial size.
 
Method Summary
 StringWriter append(char c)
          Appends the Unicode character, c, to this Appendable object.
 StringWriter append(CharSequence cs)
          Appends the specified sequence of Unicode characters to this Appendable object.
 StringWriter append(CharSequence cs, int start, int end)
          Appends the specified subsequence of Unicode characters to this Appendable object, starting and ending at the specified positions within the sequence.
 void close()
          This method closes the stream.
 void flush()
          This method flushes any buffered characters to the underlying output.
 StringBuffer getBuffer()
          This method returns the StringBuffer object that this object is writing to.
 String toString()
          This method returns the contents of the internal StringBuffer as a String.
 void write(char[] chars, int offset, int len)
          This method writes len chars from the specified array starting at index offset in that array to this stream by appending the chars to the end of the internal buffer.
 void write(int oneChar)
          This method writes a single character to the output, storing it in the internal buffer.
 void write(String str)
          This method writes the characters in the specified String to the stream by appending them to the end of the internal buffer.
 void write(String str, int offset, int len)
          This method writes out len characters of the specified String to the stream starting at character position offset into the stream.
 
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()
This method initializes a new StringWriter to write to a StringBuffer initially sized to a default size of 16 chars.


StringWriter

public StringWriter(int size)
This method initializes a new StringWriter to write to a StringBuffer with the specified initial size.

Parameters:
size - The initial size to make the StringBuffer
Method Detail

close

public void close()
           throws IOException
This method closes the stream. The contents of the internal buffer can still be retrieved, but future writes are not guaranteed to work.

Specified by:
close in interface Closeable
Specified by:
close in class Writer
Throws:
IOException - If an error orrurs.

flush

public void flush()
This method flushes any buffered characters to the underlying output. It does nothing in this class.

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

getBuffer

public StringBuffer getBuffer()
This method returns the StringBuffer object that this object is writing to. Note that this is the actual internal buffer, so any operations performed on it will affect this stream object.

Returns:
The StringBuffer object being written to

toString

public String toString()
This method returns the contents of the internal StringBuffer as a String.

Overrides:
toString in class Object
Returns:
A String representing the chars written to this stream.
See Also:
Object.getClass(), Object.hashCode(), Class.getName(), Integer.toHexString(int)

write

public void write(int oneChar)
This method writes a single character to the output, storing it in the internal buffer.

Overrides:
write in class Writer
Parameters:
oneChar - The char to write, passed as an int.

write

public void write(char[] chars,
                  int offset,
                  int len)
This method writes len chars from the specified array starting at index offset in that array to this stream by appending the chars to the end of the internal buffer.

Specified by:
write in class Writer
Parameters:
chars - The array of chars to write
offset - The index into the array to start writing from
len - The number of chars to write

write

public void write(String str)
This method writes the characters in the specified String to the stream by appending them to the end of the internal buffer.

Overrides:
write in class Writer
Parameters:
str - The String to write to the stream.

write

public void write(String str,
                  int offset,
                  int len)
This method writes out len characters of the specified String to the stream starting at character position offset into the stream. This is done by appending the characters to the internal buffer.

Overrides:
write in class Writer
Parameters:
str - The String to write characters from
offset - The character position to start writing from
len - The number of characters to write.

append

public StringWriter append(char c)
Description copied from interface: Appendable
Appends the Unicode character, c, to this Appendable object.

Specified by:
append in interface Appendable
Overrides:
append in class Writer
Parameters:
c - the character to append.
Returns:
a reference to this object.
Since:
1.5

append

public StringWriter append(CharSequence cs)
Description copied from interface: Appendable
Appends the specified sequence of Unicode characters to this Appendable object. The entire sequence may not be appended, if constrained by the underlying implementation. For example, a buffer may reach its size limit before the entire sequence is appended.

Specified by:
append in interface Appendable
Overrides:
append in class Writer
Parameters:
cs - the character sequence to append. If seq is null, then the string "null" (the string representation of null) is appended.
Returns:
a reference to this object.
Since:
1.5

append

public StringWriter append(CharSequence cs,
                           int start,
                           int end)
Description copied from interface: Appendable
Appends the specified subsequence of Unicode characters to this Appendable object, starting and ending at the specified positions within the sequence. The entire sequence may not be appended, if constrained by the underlying implementation. For example, a buffer may reach its size limit before the entire sequence is appended. The behaviour of this method matches the behaviour of append(seq.subSequence(start,end)) when the sequence is not null.

Specified by:
append in interface Appendable
Overrides:
append in class Writer
Parameters:
cs - the character sequence to append. If seq is null, then the string "null" (the string representation of null) is appended.
start - the index of the first Unicode character to use from the sequence.
end - the index of the last Unicode character to use from the sequence.
Returns:
a reference to this object.
Since:
1.5