- 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.Writer
Provides Closable semantics ordinarily missing in aCharArrayWriter
.Accumulates output in a character array that automatically grows as needed.
Data is retrieved using
toCharArray()
,toCharArrayReader()
andtoString()
.Closing
aClosableCharArrayWriter
prevents further write operations, but all other operations will succeed until after the first invocation offree()
.Freeing a
ClosableCharArrayWriter
closes 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 int
capacity()
Returns the current capacity of this writer's data buffer.void
close()
Closes this object for further writing.void
flush()
By default, does nothing.void
free()
Closes this object and releases the underlying buffer for garbage collection.boolean
isClosed()
boolean
isFreed()
void
reset()
Resets thecount
field of this writer to zero, so that all currently accumulated output is effectively discarded.void
setSize(int newSize)
Sets the size of this writer's accumulated character data.int
size()
Returns the current size of this writer's accumulated character data.char[]
toCharArray()
Creates a newly allocated character array.java.io.CharArrayReader
toCharArrayReader()
Performs an efficient (zero-copy) conversion of the character data accumulated in this writer to a reader.java.lang.String
toString()
Converts this writer's accumulated data into a string.void
trimToSize()
Attempts to reduce this writer's buffer capacity to its current size.void
write(char[] c, int off, int len)
Writes the designated portion of the designated character arrayvoid
write(int c)
Writes the specified single character.void
write(java.lang.String str, int off, int len)
Efficiently writes the designated portion of the designated string.void
writeTo(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.IllegalArgumentException
Creates a new writer with a buffer capacity of the specifiedsize
, in characters.- Parameters:
size
- the initial size.- Throws:
java.lang.IllegalArgumentException
- ifsize
is negative.
-
-
Method Detail
-
write
public void write(int c) throws java.io.IOException
Writes the specified single character.- Overrides:
write
in classjava.io.Writer
- Parameters:
c
- the single character to be written.- Throws:
java.io.IOException
- if an I/O error occurs. In particular, anIOException
may be thrown if this writer has beenclosed
.
-
write
public void write(char[] c, int off, int len) throws java.io.IOException
Writes the designated portion of the designated character array.
- Specified by:
write
in 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, anIOException
may be thrown if this writer has beenclosed
.
-
write
public void write(java.lang.String str, int off, int len) throws java.io.IOException
Efficiently writes the designated portion of the designated string.The operation occurs as if by calling
str.getChars(off, off + len, buf, count)
.- Overrides:
write
in 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, anIOException
may be thrown if this writer has beenclosed
.
-
flush
public void flush() throws java.io.IOException
By default, does nothing.- Specified by:
flush
in interfacejava.io.Flushable
- Specified by:
flush
in classjava.io.Writer
- Throws:
java.io.IOException
- if an I/O error occurs. In particular, anIOException
may be thrown if this writer has beenclosed
.
-
writeTo
public void writeTo(java.io.Writer out) throws java.io.IOException
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:
java.io.IOException
- if an I/O error occurs. In particular, anIOException
may be thrown if this writer has beenfreed
.
-
capacity
public int capacity() throws java.io.IOException
Returns 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, anIOException
may be thrown if this writer has beenfreed
.
-
reset
public void reset() throws java.io.IOException
Resets thecount
field 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.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
capacity()
method.- Throws:
java.io.IOException
- if buffer is freed
-
toCharArray
public char[] toCharArray() throws java.io.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.
-
size
public int size()
Returns the current size of this writer's accumulated character data.- Returns:
- the value of the
count
field, 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.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,
free
is 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, anIOException
may be thrown if this writer has beenfreed
.
-
toString
public java.lang.String toString()
Converts this writer's accumulated data into a string.- Overrides:
toString
in 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:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in classjava.io.Writer
-
isClosed
public boolean isClosed()
- Returns:
true
if 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:
true
if this writer is freed; elsefalse
.
-
-