- java.lang.Object
-
- java.io.Writer
-
- org.hsqldb.lib.ClosableCharArrayWriter
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.Appendable,java.lang.AutoCloseable
public class ClosableCharArrayWriter extends java.io.WriterProvides Closable semantics ordinarily missing in aCharArrayWriter.Accumulates output in a character array that automatically grows as needed.
Data is retrieved using
toCharArray(),toCharArrayReader()andtoString().ClosingaClosableCharArrayWriterprevents further write operations, but all other operations will succeed until after the first invocation offree().Freeing a
ClosableCharArrayWritercloses the writer and releases its internal buffer, preventing successful invocation of all operations, with the exception ofsize(),close(),isClosed(),free()andisFreed().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
Constructors Constructor Description ClosableCharArrayWriter()Creates a new writer.ClosableCharArrayWriter(int size)Creates a new writer with a buffer capacity of the specifiedsize, in characters.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcapacity()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[]toCharArray()Creates a newly allocated character array.java.io.CharArrayReadertoCharArrayReader()Performs an efficient (zero-copy) conversion of the character data accumulated in this writer to a reader.java.lang.StringtoString()Converts this writer's accumulated data into a string.voidtrimToSize()Attempts 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.voidwrite(java.lang.String str, int off, int len)Efficiently writes the designated portion of the designated string.voidwriteTo(java.io.Writer out)Writes the complete contents of this writer's buffered data to the specified writer.
-
-
-
Constructor Detail
-
ClosableCharArrayWriter
public ClosableCharArrayWriter()
Creates a new writer.The buffer capacity is initially 32 characters, although its size automatically increases when necessary.
-
ClosableCharArrayWriter
public ClosableCharArrayWriter(int size) throws java.lang.IllegalArgumentExceptionCreates a new writer with a buffer capacity of the specifiedsize, in characters.- Parameters:
size- the initial size.- Throws:
java.lang.IllegalArgumentException- ifsizeis negative.
-
-
Method Detail
-
write
public void write(int c) throws java.io.IOExceptionWrites the specified single character.- Overrides:
writein classjava.io.Writer- Parameters:
c- the single character to be written.- Throws:
java.io.IOException- if an I/O error occurs. In particular, anIOExceptionmay be thrown if this writer has beenclosed.
-
write
public void write(char[] c, int off, int len) throws java.io.IOExceptionWrites the designated portion of the designated character array.
- Specified by:
writein classjava.io.Writer- Parameters:
c- the source character sequence.off- the start offset in the source character sequence.len- the number of characters to write.- Throws:
java.io.IOException- if an I/O error occurs. In particular, anIOExceptionmay be thrown if this writer has beenclosed.
-
write
public void write(java.lang.String str, int off, int len) throws java.io.IOExceptionEfficiently writes the designated portion of the designated string.The operation occurs as if by calling
str.getChars(off, off + len, buf, count).- Overrides:
writein classjava.io.Writer- Parameters:
str- the string from which to writeoff- the start offset in the string.len- the number of characters to write.- Throws:
java.io.IOException- if an I/O error occurs. In particular, anIOExceptionmay be thrown if this writer has beenclosed.
-
flush
public void flush() throws java.io.IOExceptionBy default, does nothing.- Specified by:
flushin interfacejava.io.Flushable- Specified by:
flushin classjava.io.Writer- Throws:
java.io.IOException- if an I/O error occurs. In particular, anIOExceptionmay be thrown if this writer has beenclosed.
-
writeTo
public void writeTo(java.io.Writer out) throws java.io.IOExceptionWrites 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:
java.io.IOException- if an I/O error occurs. In particular, anIOExceptionmay be thrown if this writer has beenfreed.
-
capacity
public int capacity() throws java.io.IOExceptionReturns the current capacity of this writer's data buffer.- Returns:
- the current capacity (the length of the internal data array)
- Throws:
java.io.IOException- if an I/O error occurs. In particular, anIOExceptionmay be thrown if this writer has beenfreed.
-
reset
public void reset() throws java.io.IOExceptionResets thecountfield of this writer to zero, so that all currently accumulated output is effectively discarded. Further write operations will reuse the allocated buffer space.
-
trimToSize
public void trimToSize() throws java.io.IOExceptionAttempts 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:
java.io.IOException- if buffer is freed
-
toCharArray
public char[] toCharArray() throws java.io.IOExceptionCreates 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.
-
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:
count
-
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:
java.lang.ArrayIndexOutOfBoundsException- if new size is negative
-
toCharArrayReader
public java.io.CharArrayReader toCharArrayReader() throws java.io.IOExceptionPerforms 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:
java.io.IOException- if an I/O error occurs. In particular, anIOExceptionmay be thrown if this writer has beenfreed.
-
toString
public java.lang.String toString()
Converts this writer's accumulated data into a string.- Overrides:
toStringin classjava.lang.Object- Returns:
- String constructed from this writer's accumulated data
- Throws:
java.lang.RuntimeException- may be thrown if this writer has beenfreed.
-
close
public void close()
Closes this object for further writing.Other operations may continue to succeed until after the first invocation of
free().- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein classjava.io.Writer
-
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.
-
-