java.io

Class StringWriter

Implemented Interfaces:
Appendable, Closeable, Flushable

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)
StringWriter
append(CharSequence cs)
StringWriter
append(CharSequence cs, int start, int end)
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

append, append, append, close, flush, write, write, write, write, write

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

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 Details

append

public StringWriter append(char c)
Specified by:
append in interface Appendable
Overrides:
append in interface Writer
Since:
1.5

append

public StringWriter append(CharSequence cs)
Specified by:
append in interface Appendable
Overrides:
append in interface Writer
Since:
1.5

append

public StringWriter append(CharSequence cs,
                           int start,
                           int end)
Specified by:
append in interface Appendable
Overrides:
append in interface Writer
Since:
1.5

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
Overrides:
close in interface 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
Overrides:
flush in interface 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 interface Object
Returns:
A String representing the chars written to this stream.

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.
Overrides:
write in interface 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(int oneChar)
This method writes a single character to the output, storing it in the internal buffer.
Overrides:
write in interface Writer
Parameters:
oneChar - The char to write, passed as an int.

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 interface 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 interface Writer
Parameters:
str - The String to write characters from
offset - The character position to start writing from
len - The number of characters to write.

StringWriter.java -- Writes bytes to a StringBuffer Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.