Class AsynchronousWriter
- All Implemented Interfaces:
Closeable
,Flushable
,Appendable
,AutoCloseable
Information is written in the order in which character arrays are committed to the underlying queue. Characters within a particular buffer are guaranteed to be written in the order received. This writer uses a producer/consumer paradigm with multiple threads and a blocking queue. This writer is thread-safe. In addition, a copy is made of all data written so that any data structure, such as a character buffer, can be reused after production has occurred.
Because this writer does not synchronize production, the writer is not guaranteed to throw an IOException
if the writer is closed by another thread
immediately before a write operation returns. This implies that that information written to the writer at the same time another thread closes this writer has
a slight possibility of being discarded with no error. If synchronized production is desired, access to this writer's production methods should be
synchronized externally. If access to production methods are synchronized, they should be synchronized on the lock used to construct the writer or, if no
custom lock was used to construct the writer, on the writer itself.
The close()
and drain()
operations block until finished.
The flush()
operation does not block until finished.
Whether or not this writer is synchronized, it will block when producing information if the underlying blocking queue has a fixed length and is full, as is
the standard contract for BlockingQueue
.
- Author:
- Garret Wilson
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected class
The consumer runnable that writes data to the underlying writer. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final char[]
The predefined, shared character buffer that, when produced, indicates that the underlying writer should close.protected static final char[]
The predefined, shared character buffer that, when produced, indicates that all queued writes should be drained.protected static final char[]
The predefined, shared character buffer that, when produced, indicates that the underlying writer should flush. -
Constructor Summary
ConstructorsConstructorDescriptionAsynchronousWriter
(Writer writer) Writer constructor with a defaultLinkedBlockingQueue
used for data production and consumption, with critical sections synchronized on the writer itself.AsynchronousWriter
(Writer writer, Object lock) Writer and lock constructor with a defaultLinkedBlockingQueue
used for data production and consumption.AsynchronousWriter
(Writer writer, BlockingQueue<char[]> blockingQueue) Writer and blocking queue constructor, with critical sections synchronized on the writer itself.AsynchronousWriter
(Writer writer, BlockingQueue<char[]> blockingQueue, Object lock) Writer, blocking queue, and lock constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close the stream, flushing it first.void
drain()
Drains the writer by ensuring all enqueued information has been written to the underlying writer.protected void
finalize()
Cleans up the asynchronous writer by ensuring that the writer is closed and thereby the consumer thread is ended.void
flush()
Flush the stream by writing all data to the underlying writer and then flushing the underlying writer.protected BlockingQueue<char[]>
protected Writer
boolean
isOpen()
protected void
produce
(char[] charBuffer) Queues an array of characters for asynchronous writing, throwing an exception if this writer has been closed.void
write
(char[] charBuffer, int offset, int length) Write a portion of an array of characters.void
write
(int c) Write a single character.void
Write a portion of a string.
-
Field Details
-
FLUSH_INDICATOR
protected static final char[] FLUSH_INDICATORThe predefined, shared character buffer that, when produced, indicates that the underlying writer should flush. -
CLOSE_INDICATOR
protected static final char[] CLOSE_INDICATORThe predefined, shared character buffer that, when produced, indicates that the underlying writer should close. -
DRAIN_INDICATOR
protected static final char[] DRAIN_INDICATORThe predefined, shared character buffer that, when produced, indicates that all queued writes should be drained. As the appearance of this indicator indicates that the queue has already been drained, it functions as sort of a signaling no-op.
-
-
Constructor Details
-
AsynchronousWriter
Writer constructor with a defaultLinkedBlockingQueue
used for data production and consumption, with critical sections synchronized on the writer itself.- Parameters:
writer
- The writer being decorated.- Throws:
NullPointerException
- if the given writer isnull
.
-
AsynchronousWriter
Writer and blocking queue constructor, with critical sections synchronized on the writer itself.- Parameters:
writer
- The writer being decorated.blockingQueue
- The implementation of blocking queue to use for producing and consuming data.- Throws:
NullPointerException
- if the given writer and/or blocking queue isnull
.
-
AsynchronousWriter
Writer and lock constructor with a defaultLinkedBlockingQueue
used for data production and consumption.- Parameters:
writer
- The writer being decorated.lock
- The object to synchronize on for operations such asflush()
andclose()
.- Throws:
NullPointerException
- if the given writer and/or lock isnull
.
-
AsynchronousWriter
Writer, blocking queue, and lock constructor.- Parameters:
writer
- The writer being decorated.blockingQueue
- The implementation of blocking queue to use for producing and consuming data.lock
- The object to synchronize on for operations such asflush()
andclose()
.- Throws:
NullPointerException
- if the given writer, blocking queue, and/or lock isnull
.
-
-
Method Details
-
getWriter
- Returns:
- The underlying writer to which information will be asynchronously written.
-
getBlockingQueue
- Returns:
- The underlying blocking queue used for producing and consuming information, or
null
if the underlying stream has been closed.
-
isOpen
public boolean isOpen()- Returns:
- Whether this writer is still open.
-
write
Write a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.- Overrides:
write
in classWriter
- Parameters:
c
- int specifying a character to be written.- Throws:
IOException
- if an I/O error occurs.
-
write
Write a portion of a string.- Overrides:
write
in classWriter
- Parameters:
string
- The string to write.offset
- The offset from which to start writing characters.length
- The number of characters to write.- Throws:
IOException
- if an I/O error occurs.
-
write
Write a portion of an array of characters. A copy of the character buffer- Specified by:
write
in classWriter
- Parameters:
charBuffer
- The array of characters to write.offset
- The offset from which to start writing characters.length
- The number of characters to write.- Throws:
IOException
- if an I/O error occurs.
-
flush
Flush the stream by writing all data to the underlying writer and then flushing the underlying writer. This implementation does not block waiting for data to be flush; flushing occurs asynchronously.- Specified by:
flush
in interfaceFlushable
- Specified by:
flush
in classWriter
- Throws:
IOException
- if an I/O error occurs
-
drain
Drains the writer by ensuring all enqueued information has been written to the underlying writer.- Throws:
IOException
- If in I/O error occurs.
-
close
Close the stream, flushing it first. This implementation does not block waiting for the writer to close; closing occurs asynchronously. Once a stream has been closed, further write() orflush()
invocations will cause anIOException
to be thrown. Closing a previously-closed stream, however, has no effect. This implementation does not call this class'flush()
method, but because this operation involves flushing the underlying writer this method's data production is synchronized.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in classWriter
- Throws:
IOException
- if an I/O error occurs.
-
produce
Queues an array of characters for asynchronous writing, throwing an exception if this writer has been closed. The calling method should not subsequently modify the contents of the buffer. No synchronization occurs in this method; any desired synchronization should occur in the caller. This implementation delegates toproduce(char[])
.- Parameters:
charBuffer
- The buffer of characters to write.- Throws:
IOException
- if this writer has been closed before production begins, or if this thread was interrupted while waiting for production to complete.
-
finalize
Cleans up the asynchronous writer by ensuring that the writer is closed and thereby the consumer thread is ended.
-