java.io.CharArrayWriter
CharArrayWriter is used as a character output stream on a character array.
The buffer used to store the written characters will grow as needed to
accommodate more characters as they are written.
Summary
Fields
protected |
|
|
char[] |
buf |
Buffer for characters
|
protected |
|
|
int |
count |
The ending index of the buffer. |
protected |
|
|
Object |
lock |
The object used to synchronize access to the writer. |
Public Constructors
Public Methods
Methods inherited
from class
java.io.Writer
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait
Details
Fields
protected
char[]
buf
Buffer for characters
protected
int
count
The ending index of the buffer.
Public Constructors
public
CharArrayWriter()
Constructs a new CharArrayWriter which has a buffer allocated with the
default size of 32 characters. The buffer is also the lock
used to synchronize access to this Writer.
public
CharArrayWriter(int initialSize)
Constructs a new CharArrayWriter which has a buffer allocated with the
size of
initialSize
characters. The buffer is also the
lock
used to synchronize access to this Writer.
Parameters
initialSize
| the initial size of this CharArrayWriters buffer.
|
Public Methods
Append a char
c
to the CharArrayWriter. The
CharArrayWriter.append(
c
) works the same way as
CharArrayWriter.write(
c
).
Parameters
c
| The character appended to the CharArrayWriter. |
Append a CharSequence
csq
to the CharArrayWriter. The
CharArrayWriter.append(
csq
) works the same way as
CharArrayWriter.write(
csq
.toString()). If
csq
is null, then then "null" will be substituted for
csq
.
Parameters
csq
| The CharSequence appended to the CharArrayWriter. |
Append a subsequence of a CharSequence
csq
to the
CharArrayWriter. The first char and the last char of the subsequnce is
specified by the parameter
start
and
end
.
The CharArrayWriter.append(
csq
) works the same way as
CharArrayWriter.write(
csq
.subSequence(
start
,
end
).toString).
If
csq
is null, then "null" will be substituted for
csq
.
Parameters
csq
| The CharSequence appended to the CharArrayWriter. |
start
| The index of the first char in the CharSequence appended to
the CharArrayWriter. |
end
| The index of the char after the last one in the CharSequence
appended to the CharArrayWriter. |
Throws
IndexOutOfBoundsException
| If start is less than end, end is greater than the length of
the CharSequence, or start or end is negative.
|
public
void
close()
Close this Writer. This is the concrete implementation required. This
particular implementation does nothing.
public
void
flush()
Flush this Writer. This is the concrete implementation required. This
particular implementation does nothing.
public
void
reset()
Reset this Writer. The current write position is reset to the beginning
of the buffer. All written characters are lost and the size of this
writer is now 0.
public
int
size()
Answer the size of this Writer in characters. This number changes if this
Writer is reset or as more characters are written to it.
Returns
- int this CharArrayWriters current size in characters.
public
char[]
toCharArray()
Answer the contents of the receiver as a char array. The array returned
is a copy and any modifications made to this Writer after are not
reflected in the result.
Returns
- char[] this CharArrayWriters contents as a new char array.
public
String
toString()
Answer the contents of this CharArrayWriter as a String. The String
returned is a copy and any modifications made to this Writer after are
not reflected in the result.
Returns
- String this CharArrayWriters contents as a new String.
public
void
write(String str, int offset, int len)
Writes
count
number of characters starting at
offset
from the String
str
to this
CharArrayWriter.
Parameters
str
| the non-null String containing the characters to write. |
offset
| the starting point to retrieve characters. |
len
| the number of characters to retrieve and write.
|
public
void
write(int oneChar)
Writes the specified character
oneChar
to this
CharArrayWriter. This implementation writes the low order two bytes to
the Stream.
Parameters
oneChar
| The character to write
|
public
void
write(char[] c, int offset, int len)
Writes
count
characters starting at
offset
in
buf
to this CharArrayWriter.
Parameters
c
| the non-null array containing characters to write. |
offset
| offset in buf to retrieve characters |
len
| maximum number of characters to write
|
public
void
writeTo(Writer out)
Writes the contents of this CharArrayWriter to another Writer. The output
is all the characters that have been written to the receiver since the
last reset or since the creation.
Parameters
out
| the non-null Writer on which to write the contents. |
Throws
IOException
| If an error occurs attempting to write the contents out.
|