Final

java.io
Class CharArrayWriter

java.lang.Object
  extended by java.io.Writer
      extended by java.io.CharArrayWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable

public class CharArrayWriter
extends Writer

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 accomodate more characters as they are written.


Field Summary
protected  char[] buf
          Buffer for characters
protected  int count
          The ending index of the buffer.
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
CharArrayWriter()
          Constructs a new CharArrayWriter which has a buffer allocated with the default size of 32 characters.
CharArrayWriter(int initialSize)
          Constructs a new CharArrayWriter which has a buffer allocated with the size of initialSize characters.
 
Method Summary
 CharArrayWriter append(char c)
          Appends the given character to the contents of the receiver.
 CharArrayWriter append(CharSequence sequence)
          Appends the given character sequence to the contents of the receiver.
 CharArrayWriter append(CharSequence sequence, int start, int stop)
          Appends the given character sequence to the contents of the receiver.
 void close()
          Close this Writer.
 void flush()
          Flush this Writer.
 void reset()
          Reset this Writer.
 int size()
          Answer the size of this Writer in characters.
 char[] toCharArray()
          Answer the contents of the receiver as a char array.
 String toString()
          Answer the contents of this CharArrayWriter as a String.
 void write(char[] buffer, int offset, int length)
          Writes length characters starting at offset in buffer to this CharArrayWriter.
 void write(int oneChar)
          Writes the specified character oneChar to this CharArrayWriter.
 void write(String str, int offset, int length)
          Writes length number of characters starting at offset from the String str to this CharArrayWriter.
 void writeTo(Writer out)
          Writes the contents of this CharArrayWriter to another Writer.
 
Methods inherited from class java.io.Writer
write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

buf

protected char[] buf
Buffer for characters


count

protected int count
The ending index of the buffer.

Constructor Detail

CharArrayWriter

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.


CharArrayWriter

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.
Method Detail

close

public void close()
Close this Writer. This is the concrete implementation required. This particular implementation does nothing.

Specified by:
close in interface Closeable
Specified by:
close in class Writer

flush

public void flush()
Flush this Writer. This is the concrete implementation required. This particular implementation does nothing.

Specified by:
flush in interface Flushable
Specified by:
flush in class Writer

reset

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.


size

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.

toCharArray

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.

toString

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.

Overrides:
toString in class Object
Returns:
String this CharArrayWriters contents as a new String.

write

public void write(char[] buffer,
                  int offset,
                  int length)
Writes length characters starting at offset in buffer to this CharArrayWriter.

Specified by:
write in class Writer
Parameters:
buffer - the non-null array containing characters to write.
offset - offset in buf to retrieve characters
length - maximum number of characters to write

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.

Overrides:
write in class Writer
Parameters:
oneChar - The character to write

write

public void write(String str,
                  int offset,
                  int length)
Writes length number of characters starting at offset from the String str to this CharArrayWriter.

Overrides:
write in class Writer
Parameters:
str - the non-null String containing the characters to write.
offset - the starting point to retrieve characters.
length - the number of characters to retrieve and write.

writeTo

public void writeTo(Writer out)
             throws IOException
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.

append

public CharArrayWriter append(char c)
Description copied from interface: Appendable
Appends the given character to the contents of the receiver.

Specified by:
append in interface Appendable
Overrides:
append in class Writer
Parameters:
c - the character to append
Returns:
the receiver
See Also:
Appendable.append(char)

append

public CharArrayWriter append(CharSequence sequence)
Description copied from interface: Appendable
Appends the given character sequence to the contents of the receiver.

Specified by:
append in interface Appendable
Overrides:
append in class Writer
Parameters:
sequence - the character sequence to append, if null then the String "null" is appended
Returns:
the receiver
See Also:
Appendable.append(java.lang.CharSequence)

append

public CharArrayWriter append(CharSequence sequence,
                              int start,
                              int stop)
Description copied from interface: Appendable
Appends the given character sequence to the contents of the receiver.

Specified by:
append in interface Appendable
Overrides:
append in class Writer
Parameters:
sequence - the character sequence to append, if null then the String "null" is appended
start - the offset of the first character
stop - the offset one past the last character
Returns:
the receiver
See Also:
Appendable.append(java.lang.CharSequence, int, int)

Final

Licensed Materials - Property of IBM
© Copyright IBM Corp. 2006, 2008 All Rights Reserved.