java.io
Class CharArrayWriter
java.lang.Object
|
+--java.io.Writer
|
+--java.io.CharArrayWriter
public class
CharArrayWriterextends
Writer This class allows data to be written to a char array buffer and
and then retrieved by an application. The internal char array
buffer is dynamically resized to hold all the data written. Please
be aware that writing large amounts to data to this stream will
cause large amounts of memory to be allocated.
The size of the internal buffer defaults to 32 and it is resized
in increments of 1024 chars. This behavior can be over-ridden by using the
following two properties:
- gnu.java.io.CharArrayWriter.initialBufferSize
- gnu.java.io.CharArrayWriter.bufferIncrementSize
There is a constructor that specified the initial buffer size and
that is the preferred way to set that value because it it portable
across all Java class library implementations.
Authors:- Aaron M. Renn (arenn@urbanophile.com)
- Tom Tromey <tromey@cygnus.com>
buf
protected char[] buf
The internal buffer where the data written is stored
count
protected int count
The number of chars that have been written to the buffer
CharArrayWriter
public CharArrayWriter()
This method initializes a new CharArrayWriter
with
the default buffer size of 32 chars. If a different initial
buffer size is desired, see the constructor
CharArrayWriter(int size)
.
CharArrayWriter
public CharArrayWriter(int size)
This method initializes a new CharArrayWriter
with
a specified initial buffer size.
Parameters:
close
public void close()
Closes the stream. This method is guaranteed not to free the contents
of the internal buffer, which can still be retrieved.
flush
public void flush()
This method flushes all buffered chars to the stream.
reset
public void reset()
This method discards all of the chars that have been written to the
internal buffer so far by setting the count
variable to
0. The internal buffer remains at its currently allocated size.
size
public int size()
This method returns the number of chars that have been written to
the buffer so far. This is the same as the value of the protected
count
variable. If the reset
method is
called, then this value is reset as well. Note that this method does
not return the length of the internal buffer, but only the number
of chars that have been written to it.
Returns:
- The number of chars in the internal buffer
See Also:
toCharArray
public char[] toCharArray()
This method returns a char array containing the chars that have been
written to this stream so far. This array is a copy of the valid
chars in the internal buffer and its length is equal to the number of
valid chars, not necessarily to the the length of the current
internal buffer. Note that since this method allocates a new array,
it should be used with caution when the internal buffer is very large.
toString
public String toString()
Returns the chars in the internal array as a String
. The
chars in the buffer are converted to characters using the system default
encoding. There is an overloaded toString()
method that
allows an application specified character encoding to be used.
Returns:
- A
String
containing the data written to this
stream so far
write
public void write(char[] buffer, int offset, int len)
This method writes len
chars from the passed in array
buf
starting at index offset
into that buffer
Parameters:
write
public void write(int oneChar)
This method writes the writes the specified char into the internal
buffer.
Parameters:
write
public void write(java.lang.String str, int offset, int len)
This method writes len
chars from the passed in
String
buf
starting at index
offset
into the internal buffer.
Parameters:
writeTo
public void writeTo(java.io.Writer out)
This method writes all the chars that have been written to this stream
from the internal buffer to the specified Writer
.
Parameters:
Throws:
The size of the internal buffer defaults to 32 and it is resized in increments of 1024 chars. This behavior can be over-ridden by using the following two properties:
There is a constructor that specified the initial buffer size and that is the preferred way to set that value because it it portable across all Java class library implementations.