APIdia Launch Partner
Top Description Fields Constructors Methods
java.io

public Class CharArrayWriter

extends Writer
Class Inheritance
Imports
java.util.Arrays, .Objects

This class implements a character buffer that can be used as a Writer. The buffer automatically grows when data is written to the stream. The data can be retrieved using toCharArray() and toString().

Note

Invoking close() on this class has no effect, and methods of this class can be called after the stream has closed without generating an IOException.

Author
Herb Jellinek
Since
1.1

Field Summary

Modifier and TypeField and Description
protected char[]
buf

The buffer where data is stored.

protected int
count

The number of chars in the buffer.

Inherited from java.io.Writer:
lock

Constructor Summary

AccessConstructor and Description
public
CharArrayWriter()

Creates a new CharArrayWriter.

public
CharArrayWriter(int
an int specifying the initial buffer size.
initialSize
)

Creates a new CharArrayWriter with the specified initial size.

Method Summary

Modifier and TypeMethod and Description
public CharArrayWriter

Returns:

This writer
append
(CharSequence
The character sequence to append. If csq is null, then the four characters "null" are appended to this writer.
csq
)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Appends the specified character sequence to this writer.

public CharArrayWriter

Returns:

This writer
append
(CharSequence
The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".
csq
,
int
The index of the first character in the subsequence
start
,
int
The index of the character following the last character in the subsequence
end
)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Appends a subsequence of the specified character sequence to this writer.

public CharArrayWriter

Returns:

This writer
append
(char
The 16-bit character to append
c
)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Appends the specified character to this writer.

public void
close()

Implements abstract java.io.Writer.close.

Implements java.io.Closeable.close.

Close the stream.

public void
flush()

Implements abstract java.io.Writer.flush.

Implements java.io.Flushable.flush.

Flush the stream.

public void
reset()

Resets the buffer so that you can use it again without throwing away the already allocated buffer.

public int

Returns:

an int representing the current size of the buffer.
size
()

Returns the current size of the buffer.

public char[]

Returns:

an array of chars copied from the input data.
toCharArray
()

Returns a copy of the input data.

public String

Returns:

the string.
toString
()

Overrides java.lang.Object.toString.

Converts input data to a string.

public void
write(int c)

Overrides java.io.Writer.write.

Writes a character to the buffer.

public void
write(char[]
the data to be written
c
,
int
the start offset in the data
off
,
int
the number of chars that are written
len
)

Implements abstract java.io.Writer.write.

Writes characters to the buffer.

public void
write(String
String to be written from
str
,
int
Offset from which to start reading characters
off
,
int
Number of characters to be written
len
)

Overrides java.io.Writer.write.

Write a portion of a string to the buffer.

public void
writeTo(Writer
the output stream to write to
out
)

Writes the contents of the buffer to another character stream.

Inherited from java.io.Writer:
nullWriterwritewrite

Field Detail

bufback to summary
protected char[] buf

The buffer where data is stored.

countback to summary
protected int count

The number of chars in the buffer.

Constructor Detail

CharArrayWriterback to summary
public CharArrayWriter()

Creates a new CharArrayWriter.

CharArrayWriterback to summary
public CharArrayWriter(int initialSize)

Creates a new CharArrayWriter with the specified initial size.

Parameters
initialSize:int

an int specifying the initial buffer size.

Exceptions
IllegalArgumentException:
if initialSize is negative

Method Detail

appendback to summary
public CharArrayWriter append(CharSequence csq)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Appends the specified character sequence to this writer.

An invocation of this method of the form out.append(csq) when csq is not null, behaves in exactly the same way as the invocation

out.write(csq.toString())
out.write(csq.toString())

Depending on the specification of toString for the character sequence csq, the entire sequence may not be appended. For instance, invoking the toString method of a character buffer will return a subsequence whose content depends upon the buffer's position and limit.

Parameters
csq:CharSequence

The character sequence to append. If csq is null, then the four characters "null" are appended to this writer.

Returns:CharArrayWriter

This writer

Since
1.5
appendback to summary
public CharArrayWriter append(CharSequence csq, int start, int end)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Appends a subsequence of the specified character sequence to this writer.

An invocation of this method of the form out.append(csq, start, end) when csq is not null, behaves in exactly the same way as the invocation

out.write(csq.subSequence(start, end).toString())
out.write(csq.subSequence(start, end).toString())
Parameters
csq:CharSequence

The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".

start:int

The index of the first character in the subsequence

end:int

The index of the character following the last character in the subsequence

Returns:CharArrayWriter

This writer

Exceptions
IndexOutOfBoundsException:
If start or end are negative, start is greater than end, or end is greater than csq.length()
Since
1.5
appendback to summary
public CharArrayWriter append(char c)

Overrides java.io.Writer.append.

Implements java.lang.Appendable.append.

Appends the specified character to this writer.

An invocation of this method of the form out.append(c) behaves in exactly the same way as the invocation

out.write(c)
out.write(c)
Parameters
c:char

The 16-bit character to append

Returns:CharArrayWriter

This writer

Since
1.5
closeback to summary
public void close()

Implements abstract java.io.Writer.close.

Implements java.io.Closeable.close.

Close the stream. This method does not release the buffer, since its contents might still be required.

Note

Invoking this method in this class will have no effect.

flushback to summary
public void flush()

Implements abstract java.io.Writer.flush.

Implements java.io.Flushable.flush.

Flush the stream.

The flush method of CharArrayWriter does nothing.

resetback to summary
public void reset()

Resets the buffer so that you can use it again without throwing away the already allocated buffer.

sizeback to summary
public int size()

Returns the current size of the buffer.

Returns:int

an int representing the current size of the buffer.

toCharArrayback to summary
public char[] toCharArray()

Returns a copy of the input data.

Returns:char[]

an array of chars copied from the input data.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Converts input data to a string.

Returns:String

the string.

writeback to summary
public void write(int c)

Overrides java.io.Writer.write.

Writes a character to the buffer.

writeback to summary
public void write(char[] c, int off, int len)

Implements abstract java.io.Writer.write.

Writes characters to the buffer.

Parameters
c:char[]

the data to be written

off:int

the start offset in the data

len:int

the number of chars that are written

Exceptions
IndexOutOfBoundsException:
If off is negative, or len is negative, or off + len is negative or greater than the length of the given array
writeback to summary
public void write(String str, int off, int len)

Overrides java.io.Writer.write.

Write a portion of a string to the buffer.

Parameters
str:String

String to be written from

off:int

Offset from which to start reading characters

len:int

Number of characters to be written

Exceptions
IndexOutOfBoundsException:
If off is negative, or len is negative, or off + len is negative or greater than the length of the given string
writeToback to summary
public void writeTo(Writer out) throws IOException

Writes the contents of the buffer to another character stream.

Parameters
out:Writer

the output stream to write to

Exceptions
IOException:
If an I/O error occurs.