Class ClosableCharArrayWriter
- All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable
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.
- Since:
- 1.8.x
- Author:
- Campbell Burnet (campbell-burnet@users dot sourceforge.net)
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new writer.ClosableCharArrayWriter(int size) Creates a new writer with a buffer capacity of the specifiedsize, in characters. -
Method Summary
Modifier and TypeMethodDescriptionintcapacity()Returns the current capacity of this writer's data buffer.voidclose()Closes this object for further writing.voidflush()By default, does nothing.voidfree()Closes this object and releases the underlying buffer for garbage collection.booleanisClosed()booleanisFreed()voidreset()Resets thecountfield of this writer to zero, so that all currently accumulated output is effectively discarded.voidsetSize(int newSize) Sets the size of this writer's accumulated character data.intsize()Returns the current size of this writer's accumulated character data.char[]Creates a newly allocated character array.Performs an efficient (zero-copy) conversion of the character data accumulated in this writer to a reader.toString()Converts this writer's accumulated data into a string.voidAttempts to reduce this writer's buffer capacity to its current size.voidwrite(char[] c, int off, int len) Writes the designated portion of the designated character arrayvoidwrite(int c) Writes the specified single character.voidEfficiently writes the designated portion of the designated string.voidWrites the complete contents of this writer's buffered data to the specified writer.
-
Constructor Details
-
ClosableCharArrayWriter
public ClosableCharArrayWriter()Creates a new writer.The buffer capacity is initially 32 characters, although its size automatically increases when necessary.
-
ClosableCharArrayWriter
Creates a new writer with a buffer capacity of the specifiedsize, in characters.- Parameters:
size- the initial size.- Throws:
IllegalArgumentException- ifsizeis negative.
-
-
Method Details
-
write
Writes the specified single character.- Overrides:
writein classWriter- Parameters:
c- the single character to be written.- Throws:
IOException- if an I/O error occurs. In particular, anIOExceptionmay be thrown if this writer has beenclosed.
-
write
Writes the designated portion of the designated character array.
- Specified by:
writein classWriter- Parameters:
c- the source character sequence.off- the start offset in the source character sequence.len- the number of characters to write.- Throws:
IOException- if an I/O error occurs. In particular, anIOExceptionmay be thrown if this writer has beenclosed.
-
write
Efficiently writes the designated portion of the designated string.The operation occurs as if by calling
str.getChars(off, off + len, buf, count).- Overrides:
writein classWriter- Parameters:
str- the string from which to writeoff- the start offset in the string.len- the number of characters to write.- Throws:
IOException- if an I/O error occurs. In particular, anIOExceptionmay be thrown if this writer has beenclosed.
-
flush
By default, does nothing. -
writeTo
Writes the complete contents of this writer's buffered data to the specified writer.The operation occurs as if by calling
out.write(buf, 0, count).- Parameters:
out- the writer to which to write the data.- Throws:
IOException- if an I/O error occurs. In particular, anIOExceptionmay be thrown if this writer has beenfreed.
-
capacity
Returns the current capacity of this writer's data buffer.- Returns:
- the current capacity (the length of the internal data array)
- Throws:
IOException- if an I/O error occurs. In particular, anIOExceptionmay be thrown if this writer has beenfreed.
-
reset
Resets thecountfield of this writer to zero, so that all currently accumulated output is effectively discarded. Further write operations will reuse the allocated buffer space.- Throws:
IOException- if an I/O error occurs. In particular, anIOExceptionmay be thrown if this output stream has beenclosed.- See Also:
-
trimToSize
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
capacity()method.- Throws:
IOException- if buffer is freed
-
toCharArray
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.- Returns:
- the current contents of this writer, as a character array.
- Throws:
IOException- if an I/O error occurs. In particular, anIOExceptionmay be thrown if this writer has beenfreed.- See Also:
-
size
public int size()Returns the current size of this writer's accumulated character data.- Returns:
- the value of the
countfield, which is the number of valid characters accumulated in this writer. - See Also:
-
setSize
public void setSize(int newSize) Sets the size of this writer's accumulated character data.- Parameters:
newSize- the new size of this writer's accumulated data- Throws:
ArrayIndexOutOfBoundsException- if new size is negative
-
toCharArrayReader
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,
freeis invoked upon this writer as a side-effect.- Returns:
- a reader representing this writer's accumulated character data
- Throws:
IOException- if an I/O error occurs. In particular, anIOExceptionmay be thrown if this writer has beenfreed.
-
toString
Converts this writer's accumulated data into a string.- Overrides:
toStringin classObject- Returns:
- String constructed from this writer's accumulated data
- Throws:
RuntimeException- may be thrown if this writer has beenfreed.
-
close
-
isClosed
public boolean isClosed()- Returns:
trueif this writer is closed, elsefalse
-
free
public void free()Closes this object and releases the underlying buffer for garbage collection. -
isFreed
public boolean isFreed()- Returns:
trueif this writer is freed; elsefalse.
-