java.io.CharArrayWriter . Accumulates output in a character array that automatically grows as needed.
Data is retrieved using toCharArray(), toCharArrayReader()
and toString().
Closing a ClosableCharArrayWriter prevents
further write operations, but all other operations will succeed until after
the first invocation of free().
Freeing a ClosableCharArrayWriter closes the writer and
releases its internal buffer, preventing successful invocation of all
operations, with the exception of size(), close(),
isClosed(), free() and isFreed().
This class is especially useful when an accumulating writer must be
handed off to an extenal client under contract that the writer should
exhibit true Closable behaviour, both in response to internally tracked
events and to client invocation of the Writer.close() method.
| Modifier and Type | Field and Description |
|---|---|
| protected char[] | buf
Data buffer. |
| protected boolean | closed
Whether this writer is closed. |
| protected int | count
# of valid characters in buffer. |
| protected boolean | freed
Whether this writer is freed. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | ClosableCharArrayWriter(int
the initial size. size)Creates a new writer with a buffer capacity of the specified
|
| Modifier and Type | Method and Description |
|---|---|
| public synchronized int | Returns: the current capacity (the length of the internal data array)Returns the current capacity of this writer's data buffer. |
| protected synchronized void | |
| protected synchronized void | |
| public synchronized void | close()
Implements abstract java. Implements java. Closes this object for further writing. |
| protected char[] | Returns: copy oforiginal with the given newLengththe object to copy original, int the length of the copy newLength)Retrieves a copy of |
| public void | flush()
Implements abstract java. Implements java. By default, does nothing. |
| public synchronized void | |
| public synchronized boolean | |
| public synchronized boolean | |
| public synchronized void | reset()
Resets the |
| public synchronized void | setSize(int
the new size of this writer's accumulated data newSize)Sets the size of this writer's accumulated character data. |
| public synchronized int | Returns: the value of thecount field, which is the number
of valid characters accumulated in this writer.Returns the current size of this writer's accumulated character data. |
| public synchronized char[] | Returns: the current contents of this writer, as a character array.Creates a newly allocated character array. |
| public synchronized CharArrayReader | Returns: a reader representing this writer's accumulated character dataPerforms an efficient (zero-copy) conversion of the character data accumulated in this writer to a reader. |
| public synchronized String | Returns: String constructed from this writer's accumulated dataOverrides java. Converts this writer's accumulated data into a string. |
| public synchronized void | |
| public synchronized void | write(int
the single character to be written. c)Overrides java. Writes the specified single character. |
| public synchronized void | write(char[]
the source character sequence. c, int the start offset in the source character sequence. off, int the number of characters to write. len)Implements abstract java. Writes the designated portion of the designated character array . |
| public synchronized void | write(String
the string from which to write str, int the start offset in the string. off, int the number of characters to write. len)Overrides java. Efficiently writes the designated portion of the designated string. |
| public synchronized void |
| buf | back to summary |
|---|---|
| protected char[] buf Data buffer. | |
| closed | back to summary |
|---|---|
| protected boolean closed Whether this writer is closed. | |
| count | back to summary |
|---|---|
| protected int count # of valid characters in buffer. | |
| freed | back to summary |
|---|---|
| protected boolean freed Whether this writer is freed. | |
| ClosableCharArrayWriter | back to summary |
|---|---|
| public ClosableCharArrayWriter() Creates a new writer. The buffer capacity is initially 32 characters, although its size automatically increases when necessary. | |
| ClosableCharArrayWriter | back to summary |
|---|---|
| public ClosableCharArrayWriter(int size) throws IllegalArgumentException Creates a new writer with a buffer capacity of the specified
| |
| capacity | back to summary |
|---|---|
| public synchronized int capacity() throws IOException Returns the current capacity of this writer's data buffer.
| |
| checkClosed | back to summary |
|---|---|
protected synchronized void checkClosed() throws IOException
| |
| checkFreed | back to summary |
|---|---|
protected synchronized void checkFreed() throws IOException
| |
| close | back to summary |
|---|---|
| public synchronized void close() Implements abstract java. Implements java. Closes this object for further writing.
Other operations may continue to succeed until after the first invocation
of | |
| copyOf | back to summary |
|---|---|
| protected char[] copyOf(char[] original, int newLength) Retrieves a copy of
| |
| flush | back to summary |
|---|---|
| public void flush() throws IOException Implements abstract java. Implements java. By default, does nothing.
| |
| free | back to summary |
|---|---|
| public synchronized void free() Closes this object and releases the underlying buffer for garbage collection. | |
| isClosed | back to summary |
|---|---|
public synchronized boolean isClosed()
| |
| isFreed | back to summary |
|---|---|
public synchronized boolean isFreed()
| |
| reset | back to summary |
|---|---|
| public synchronized void reset() throws IOException Resets the
| |
| setSize | back to summary |
|---|---|
| public synchronized void setSize(int newSize) Sets the size of this writer's accumulated character data.
| |
| size | back to summary |
|---|---|
| public synchronized int size() Returns the current size of this writer's accumulated character data.
| |
| toCharArray | back to summary |
|---|---|
| public synchronized char[] toCharArray() throws IOException Creates a newly allocated character array. Its size is the current size of this writer and the valid contents of the buffer have been copied into it.
| |
| toCharArrayReader | back to summary |
|---|---|
| public synchronized CharArrayReader toCharArrayReader() throws IOException Performs an efficient (zero-copy) conversion of the character data accumulated in this writer to a reader.
To ensure the integrity of the resulting reader,
| |
| toString | back to summary |
|---|---|
| public synchronized String toString() Overrides java. Converts this writer's accumulated data into a string.
| |
| trimToSize | back to summary |
|---|---|
| public synchronized void trimToSize() throws IOException Attempts to reduce this writer's buffer capacity to its current size.
If the buffer is larger than necessary to hold its current sequence of
characters, then it may be resized to become more space efficient.
Calling this method may, but is not required to, affect the value
returned by a subsequent call to the
| |
| write | back to summary |
|---|---|
| public synchronized void write(int c) throws IOException Overrides java. Writes the specified single character.
| |
| write | back to summary |
|---|---|
| public synchronized void write(char[] c, int off, int len) throws IOException Implements abstract java. Writes the designated portion of the designated character array .
| |
| write | back to summary |
|---|---|
| public synchronized void write(String str, int off, int len) throws IOException Overrides java. Efficiently writes the designated portion of the designated string.
The operation occurs as if by calling
| |
| writeTo | back to summary |
|---|---|
| public synchronized void writeTo(Writer out) throws IOException Writes the complete contents of this writer's buffered data to the specified writer.
The operation occurs as if by calling
| |