Package org.jgrapes.io.util
Class ByteBufferWriter
- All Implemented Interfaces:
Closeable
,Flushable
,Appendable
,AutoCloseable
An
Writer
that encodes the data written to it and stores it
in a ByteBuffer
obtained from a queue.When a byte buffer
is full, an Output
event (default) is generated and a
new buffer is fetched from the queue.
The function of this class can also be achieved by wrapping a
ByteBufferOutputStream
in a OutputStreamWriter
.
The major advantage of this class is that it drops the
IOException
s (which cannot happen) from the methods.
Besides, it should be more resource efficient.
-
Field Summary
Fields inherited from class org.jgrapes.io.util.AbstractBufferWriter
buffer, channel
-
Constructor Summary
ConstructorDescriptionByteBufferWriter
(IOSubchannel channel) Creates a new instance that usesOutput
events to dispatch buffers on the given channel, using the channel’s response pipeline.ByteBufferWriter
(IOSubchannel channel, EventPipeline eventPipeline) Creates a new instance that usesOutput
events to dispatch buffers on the given channel, using the given event pipeline. -
Method Summary
Modifier and TypeMethodDescriptionappend
(char ch) append
(CharSequence csq) append
(CharSequence csq, int start, int end) Sets the charset to be used for converting the written data to bytes which defaults to UTF-8.protected void
Ensure that a buffer for output data is available.Sets the charset to be used for converting the written data to bytes to the charset specified as system propertynative.encoding
.setEventAssociations
(Map<Object, Object> associations) Configure associations that are applied to the generated Output events, seeEvent.setAssociated(java.lang.Object, java.lang.Object)
.Suppresses sending of a close event when the stream is closed.Suppresses setting the end of record flag when the stream is flushed or closed.void
write
(char[] cbuf) void
write
(char[] data, int offset, int length) void
write
(int ch) void
void
Methods inherited from class org.jgrapes.io.util.AbstractBufferWriter
close, flush, flush
Methods inherited from class java.io.Writer
nullWriter
-
Constructor Details
-
ByteBufferWriter
Creates a new instance that usesOutput
events to dispatch buffers on the given channel, using the given event pipeline.- Parameters:
channel
- the channel to fire events oneventPipeline
- the event pipeline used for firing events
-
ByteBufferWriter
Creates a new instance that usesOutput
events to dispatch buffers on the given channel, using the channel’s response pipeline.- Parameters:
channel
- the channel to fire events on
-
-
Method Details
-
sendInputEvents
Description copied from class:AbstractBufferWriter
- Overrides:
sendInputEvents
in classAbstractBufferWriter<ByteBuffer>
- Returns:
- this object for easy chaining
-
suppressClose
Description copied from class:AbstractBufferWriter
Suppresses sending of a close event when the stream is closed.- Overrides:
suppressClose
in classAbstractBufferWriter<ByteBuffer>
- Returns:
- this object for easy chaining
-
suppressEndOfRecord
Description copied from class:AbstractBufferWriter
Suppresses setting the end of record flag when the stream is flushed or closed.- Overrides:
suppressEndOfRecord
in classAbstractBufferWriter<ByteBuffer>
- Returns:
- this object for easy chaining
- See Also:
-
setEventAssociations
Description copied from class:AbstractBufferWriter
Configure associations that are applied to the generated Output events, seeEvent.setAssociated(java.lang.Object, java.lang.Object)
.- Overrides:
setEventAssociations
in classAbstractBufferWriter<ByteBuffer>
- Parameters:
associations
- the associations to apply- Returns:
- this object for easy chaining
-
charset
Sets the charset to be used for converting the written data to bytes which defaults to UTF-8.Must be set before the first invocation of any write method.
- Parameters:
charset
- the charset- Returns:
- the writer
-
nativeCharset
Sets the charset to be used for converting the written data to bytes to the charset specified as system propertynative.encoding
.If this property does not specify a valid charset,
Charset.defaultCharset()
is used.Must be invoked before the first write (or append) operation.
- Returns:
- the writer
-
ensureBufferAvailable
Description copied from class:AbstractBufferWriter
Ensure that a buffer for output data is available.- Specified by:
ensureBufferAvailable
in classAbstractBufferWriter<ByteBuffer>
- Throws:
InterruptedException
- the interrupted exception
-
write
- Specified by:
write
in classAbstractBufferWriter<ByteBuffer>
-
write
- Overrides:
write
in classAbstractBufferWriter<ByteBuffer>
-
write
- Specified by:
write
in classAbstractBufferWriter<ByteBuffer>
-
write
- Overrides:
write
in classAbstractBufferWriter<ByteBuffer>
-
write
- Overrides:
write
in classAbstractBufferWriter<ByteBuffer>
-
append
- Specified by:
append
in interfaceAppendable
- Overrides:
append
in classWriter
-
append
- Specified by:
append
in interfaceAppendable
- Overrides:
append
in classWriter
-
append
- Specified by:
append
in interfaceAppendable
- Overrides:
append
in classWriter
-