Package java.io
Class CharArrayWriter
java.lang.Object
java.io.Writer
java.io.CharArrayWriter
- All Implemented Interfaces:
Closeable,Flushable,Appendable,AutoCloseable
public class CharArrayWriter extends Writer
A specialized
Writer for class for writing content to an (internal)
char array. As bytes are written to this writer, the char array may be
expanded to hold more characters. When the writing is considered to be
finished, a copy of the char array can be requested from the class.- See Also:
CharArrayReader
-
Field Summary
-
Constructor Summary
Constructors Constructor Description CharArrayWriter()Constructs a newCharArrayWriterwhich has a buffer allocated with the default size of 32 characters.CharArrayWriter(int initialSize)Constructs a newCharArrayWriterwhich has a buffer allocated with the size ofinitialSizecharacters. -
Method Summary
Modifier and Type Method Description CharArrayWriterappend(char c)Appends a charcto theCharArrayWriter.CharArrayWriterappend(CharSequence csq)Appends aCharSequenceto theCharArrayWriter.CharArrayWriterappend(CharSequence csq, int start, int end)Append a subsequence of aCharSequenceto theCharArrayWriter.voidclose()Closes this writer.voidflush()Flushes this writer.voidreset()Resets this writer.intsize()Returns the size of this writer, that is the number of characters it stores.char[]toCharArray()Returns the contents of the receiver as a char array.StringtoString()Returns the contents of thisCharArrayWriteras a string.voidwrite(char[] buffer, int offset, int len)Writescountcharacters starting atoffsetincto this writer.voidwrite(int oneChar)Writes the specified characteroneCharto this writer.voidwrite(String str, int offset, int count)Writescountcharacters starting atoffsetfrom the stringstrto this CharArrayWriter.voidwriteTo(Writer out)Writes the contents of thisCharArrayWriterto anotherWriter.
-
Field Details
-
buf
protected char[] bufThe buffer for characters. -
count
protected int countThe ending index of the buffer.
-
-
Constructor Details
-
CharArrayWriter
public CharArrayWriter()Constructs a newCharArrayWriterwhich has a buffer allocated with the default size of 32 characters. This buffer is also used as thelockto synchronize access to this writer. -
CharArrayWriter
public CharArrayWriter(int initialSize)Constructs a newCharArrayWriterwhich has a buffer allocated with the size ofinitialSizecharacters. The buffer is also used as thelockto synchronize access to this writer.- Parameters:
initialSize- the initial size of this CharArrayWriters buffer.- Throws:
IllegalArgumentException- ifinitialSize < 0.
-
-
Method Details
-
close
public void close()Closes this writer. The implementation inCharArrayWriterdoes nothing. -
flush
public void flush()Flushes this writer. The implementation inCharArrayWriterdoes nothing. -
reset
public void reset()Resets 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 set to 0. -
size
public int size()Returns the size of this writer, that is the number of characters it stores. This number changes if this writer is reset or when more characters are written to it.- Returns:
- this CharArrayWriter's current size in characters.
-
toCharArray
public char[] toCharArray()Returns the contents of the receiver as a char array. The array returned is a copy and any modifications made to this writer after calling this method are not reflected in the result.- Returns:
- this CharArrayWriter's contents as a new char array.
-
toString
Returns the contents of thisCharArrayWriteras a string. The string returned is a copy and any modifications made to this writer after calling this method are not reflected in the result. -
write
public void write(char[] buffer, int offset, int len)Writescountcharacters starting atoffsetincto this writer.- Specified by:
writein classWriter- Parameters:
buffer- the non-null array containing characters to write.offset- the index of the first character inbufto write.len- maximum number of characters to write.- Throws:
IndexOutOfBoundsException- ifoffset < 0orlen < 0, or ifoffset + lenis bigger than the size ofc.
-
write
public void write(int oneChar)Writes the specified characteroneCharto this writer. This implementation writes the two low order bytes of the integeroneCharto the buffer. -
write
Writescountcharacters starting atoffsetfrom the stringstrto this CharArrayWriter.- Overrides:
writein classWriter- Parameters:
str- the non-null string containing the characters to write.offset- the index of the first character instrto write.count- the number of characters fromstrto write.- Throws:
NullPointerException- ifstrisnull.StringIndexOutOfBoundsException- ifoffset < 0orcount < 0, or ifoffset + countis bigger than the length ofstr.
-
writeTo
Writes the contents of thisCharArrayWriterto anotherWriter. The output is all the characters that have been written to the receiver since the last reset or since it was created.- Parameters:
out- the non-nullWriteron which to write the contents.- Throws:
NullPointerException- ifoutisnull.IOException- if an error occurs attempting to write out the contents.
-
append
Appends a charcto theCharArrayWriter. The method works the same way aswrite(c).- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter- Parameters:
c- the character appended to the CharArrayWriter.- Returns:
- this CharArrayWriter.
-
append
Appends aCharSequenceto theCharArrayWriter. The method works the same way aswrite(csq.toString()). Ifcsqisnull, then it will be substituted with the string"null".- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter- Parameters:
csq- theCharSequenceappended to theCharArrayWriter, may benull.- Returns:
- this CharArrayWriter.
-
append
Append a subsequence of aCharSequenceto theCharArrayWriter. The first and last characters of the subsequence are specified by the parametersstartandend. A call toCharArrayWriter.append(csq)works the same way asCharArrayWriter.write(csq.subSequence(start, end).toString). Ifcsqisnull, then it will be substituted with the string"null".- Specified by:
appendin interfaceAppendable- Overrides:
appendin classWriter- Parameters:
csq- theCharSequenceappended to theCharArrayWriter, may benull.start- the index of the first character in theCharSequenceappended to theCharArrayWriter.end- the index of the character after the last one in theCharSequenceappended to theCharArrayWriter.- Returns:
- this CharArrayWriter.
- Throws:
IndexOutOfBoundsException- ifstart < 0,end < 0,start > end, or ifendis greater than the length ofcsq.
-