THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

20.5.6. CharArray Character Streams


The CharArray character streams are analogous to the ByteArray byte streamsthey let you use char
arrays as a source or destination without ever blocking. You construct CharArrayReader objects with an
array of char:


publicCharArrayReader(char[] buf, int offset, int count)

Creates a CharArrayReader from the specified array of characters using
only the subarray of buf from buf[offset] to
buf[offset+count-1] or the end of the array, whichever is smaller.
The input array is used directly, not copied, so you should take care not to
modify it while it is being used as an input source.

publicCharArrayReader(char[] buf)

Equivalent to CharArrayReader(buf,0,buf.length).

The CharArrayWriter class provides a dynamically growing char array to hold output. It adds
constructors and methods:


publicCharArrayWriter()

Creates a new CharArrayWriter with a default initial array size.

publicCharArrayWriter(int size)

Creates a new CharArrayWriter with the given initial array size.

public intsize()

Returns the number of characters generated thus far by output to the stream.

public char[]toCharArray()

Returns a copy of the characters generated thus far by output to the stream.
When you are finished writing into a CharArrayWriter via upstream
filter streams, you should flush the upstream objects before using
toCharArray.

public voidreset()

Resets the stream to reuse the current buffer, discarding its contents.

public StringtoString()

Returns the current contents of the buffer as a String.

public voidwriteTo(Writer out)tHRows IOException

Writes the current contents of the buffer to the stream out.
Free download pdf