see Writer class for descriptions of methods for CharArrayWriter. They are:
.close( ) Closes the stream, flushing it first.
.flush( ) Flushes all associated buffers in a stream without closing it.
.write( int c ) Writes a single char, supplied as the low-order two bytes of a four-byte int.
.write( char[ ] c ) Writes an array of characters.
.write( char[ ] c, int off, int len ) Writes a portion len of a character array, beginning at offset
.write( String s ) Writes a String.
.write( String s, int off, int len ) Writes a portion len of a String,
beginning at off
plus more of CharArrayWriter’s own methods are
explained below:
.toCharArray( ) below
■ CharArrayWriter allows you to use write(...) methods to add data to a CharArrayWriter character stream. Then you can retrieve the stream's contents in any of three ways: (1) as a regular character array, (2) as a String, or (3) they can be written directly to another stream.
(1) To retrieve the
stream's contents as a character array use the method toCharArray( )
(2) To retrieve them as a String use toString( )
(3) To write them
out at any time to another Writer stream
use the writeTo(...) method.
■ CharArrayWriter
is very similar to StringWriter
in that it lets you use I/O write(
) methods to dynamically build a buffer in memory whose contents you
can then retrieve in another form. i.e. The two snippets below show
how much CharArrayWriter
is like StringWriter. They produce identical results. see StringWriter
.
import
java.io.*;
CharArrayWriter
caw = new CharArrayWriter( );
caw.write(
"HELLO " );
caw.write(
"WORLD " );
System.out.println(
caw );
System.out.println(
caw.toString( ) );
StringWriter
sw = new StringWriter( );
sw.write(
"HELLO " );
sw.write(
"WORLD " );
System.out.println(
sw );
System.out.println( sw.toString( ) );
■ Unlike CharArrayReader, with CharArrayWriter you do not have to supply the character array in the constructor. All written data accumulates in memory. The buffer automatically increases to accommodate additions.
■ A CharArrayReader and CharArrayWriter combination can be used for piping in much the same way as a PipedReader and PipedWriter, except they don't carry the Piped restriction that, for the other to work, both ends must always be open and available, and that their threads must not close.
i.e. In the example below, a CharArrayWriter is passed from
the first thread, much like a PipedWriter.
But then the originating thread can end, and the second thread can continue on
without it. This is unlike the PipedWriter example provided in
the piping section, where, if the originating thread closes, the PipedReader at the other end
won't work any more. see piping
import
java.io.*;
public
class LikePipeThreads {
public static void main(String[ ] args)
{
LikePipeThreads P = new
LikePipeThreads( );
}
public LikePipeThreads( ) {
try {
CharArrayWriter caw = new
CharArrayWriter( );
caw.write( "123456789"
);
Thread t2 = new Thread( new
Class2( caw ));
t2.start( );
return;
} catch (Exception e) { }
}
}
class
Class2 implements Runnable {
CharArrayReader car;
CharArrayWriter caw;
public Class2( CharArrayWriter cw ) {
caw = cw;
}
public void run( ) {
try {
car = new CharArrayReader(
caw.toCharArray( ) );
int c = 0;
while( ( c = car.read( ) ) != -1
) {
System.out.print( (char) c );
}
} catch (Exception e) { }
}
}
■ This repeats the example above, but it does away with the CharArrayReader. The CharArrayWriter stream is now converted directly to a char array when it is received by the other thread. Of course, this means that the first thread cannot write to it any more after passing it.
import
java.io.*;
public
class LikePipeThreads2 {
public static void main(String[ ] args)
{
LikePipeThreads2 P = new
LikePipeThreads2( );
}
public LikePipeThreads2( ) {
try {
CharArrayWriter caw = new
CharArrayWriter( );
caw.write( "123456789"
);
Thread t2 = new Thread( new
Class2( caw ));
t2.start( ); //
this thread can now end without harm
return;
} catch (Exception e) { }
}
}
class
Class2 implements Runnable {
CharArrayWriter caw;
public Class2( CharArrayWriter cw ) {
caw = cw;
}
public void run( ) {
try {
char[ ] c = new char[ caw.size( )
]; // immediately
convert caw to a char array
c = caw.toCharArray( );
for ( int x = 0; x < c.length;
x++ ) {
System.out.print( c[x] );
}
} catch (Exception e) { }
}
}
■ CharArrayWriter adds five methods to those provided by Writer. They are described below:
char[ ] .toCharArray( ) method
■ This method returns a char array containing the characters written to this stream thus far. It allocates a new array. i.e.
import
java.io.*;
try
{
CharArrayWriter
caw = new CharArrayWriter( );
caw.write("HELLO
");
caw.write("WORLD
");
char[
] c = caw.toCharArray( );
System.out.println(
c );
} catch (IOException e) { }
■ If the contents of a CharArrayWriter stream are sent to a char array using the toCharArray( ) method, a CharArrayReader can read the resulting array.
■ This method, overridden from Object, returns a String of the whole CharArrayWriter stream written thus far. i.e.
import
java.io.*;
try
{
CharArrayWriter
caw = new CharArrayWriter( );
caw.write("HELLO
");
caw.write("WORLD
");
String
s = caw.toString( );
System.out.println(
s );
}
catch (IOException e) { }
■ This method returns an int telling you the number of chars actually written to the CharArrayWriter stream buffer thus far. If the reset( ) method is called, it resets this particular value to zero.
import
java.io.*;
try
{
CharArrayWriter
caw = new CharArrayWriter( );
caw.write("HELLO
");
caw.write("WORLD
");
int
x;
x
= caw.size( );
System.out.println(x
+ " characters written."); //
prints 12
caw.reset(
);
x
= caw.size( );
System.out.println(x
+ " characters written."); //
now prints 0
} catch (IOException e) { }
void .writeTo( Writer
) method
■ This method writes out to any Writer stream all the chars which were written to the CharArrayWriter stream thus far.
The snippet below demonstrates. It writes half of the alphabet, A through M, to a PipedWriter stream. It then uses CharArrayWriter's writeTo(...) method to write the digits 1234567890 from a CharArrayWriter stream to the same PipedWriter stream after the M. It then continues writing the rest of the alphabet, N to Z, to the PipedWriter stream. The result is that 1234567890 appears in the middle of the alphabet in the PipedWriter stream. The associated PipedReader stream shows this result when it reads the PipedWriter stream for printing.
import
Java.io.*;
PipedReader pr = new PipedReader( );
CharArrayWriter caw = new CharArrayWriter( );
String S =
"ABCEFGHIJKLMNOPQRSTUVWXYZ";
try {
PipedWriter pw = new PipedWriter( );
pw.connect( pr );
for ( int x = 0; x < S.length( ); x++)
{
if ( x == 13 ) {
caw.write( "1234567890"
); // 2 - if 13 written, insert the
ten numbers into the caw
caw.writeTo( pw ); // 3 - transfer the whole caw to the PipedWriter
}
pw.write( S, x, 1 ); // 1
- write all the letters of alphabet to the PipedWriter
}
for ( int x = 0; x < 36;
x++ ) {
System.out.print( (char) pr.read( )
);
}
} catch (IOException e) { }
■ This method discards everything in the CharArrayWriter stream and resets the size( ) count to zero. Using reset( ) is faster and more efficient than creating a new CharArrayWriter.