Interface MessageWriter
- All Superinterfaces:
AutoCloseable,Closeable
Writes values encoded in MessagePack to a channel, output stream, MessageOutput, or MessageSink.Provider.
To create a new message writer, call of(java.nio.channels.WritableByteChannel).
To write values, call the various write(boolean) and writeXxx methods.
To flush a message writer, call flush().
To close a message writer, call close().
Continued use of a closed message writer can result in data corruption.
For usage examples, see Writing data.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceA builder of message writer options. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this message writer.voidflush()Flushes this message writer.static MessageWriterof(OutputStream stream) Returns a new message writer that writes to the given output stream.static MessageWriterof(OutputStream stream, Consumer<MessageWriter.OptionBuilder> optionHandler) Returns a new message writer with the given options that writes to the given output stream.static MessageWriterof(WritableByteChannel channel) Returns a new message writer that writes to the given channel.static MessageWriterof(WritableByteChannel channel, Consumer<MessageWriter.OptionBuilder> optionHandler) Returns a new message writer with the given options that writes to the given channel.static MessageWriterof(MessageOutput.Buffer output) Returns a new message writer that writes to the given buffer output.static MessageWriterof(MessageOutput.Buffer output, Consumer<MessageWriter.OptionBuilder> optionHandler) Returns a new message writer with the given options that writes to the given buffer output.static MessageWriterof(MessageSink.Provider provider) Returns a new message writer that writes to the given sink provider.static MessageWriterof(MessageSink.Provider provider, Consumer<MessageWriter.OptionBuilder> optionHandler) Returns a new message writer with the given options that writes to the given sink provider.static MessageWriterReturns a new message writer that discards any bytes written.static MessageWriterofDiscarding(ByteBuffer buffer) Returns a new message writer with the given underlying buffer that discards any bytes written.voidwrite(boolean value) Writes a boolean value.voidwrite(byte value) Writes an integer value that fits intobyte.voidwrite(double value) Writes a floating point value that fits intodouble.voidwrite(float value) Writes a floating point value that fits intofloat.voidwrite(int value) Writes an integer value that fits intoint.voidwrite(long value) Writes an integer value that fits intolong.voidwrite(short value) Writes an integer value that fits intoshort.voidwrite(CharSequence string) Writes a string value.voidWrites a timestamp value.<T> voidwrite(T value, MessageEncoder<T> encoder) Encodes and writes a value.voidwriteArrayHeader(int elementCount) Starts writing an array value.voidwriteBinaryHeader(int length) Starts writing a binary value.voidwriteExtensionHeader(int length, byte type) Starts writing an extension value.voidwriteIdentifier(String identifier) Writes a string value that is used as identifier.voidwriteMapHeader(int entryCount) Starts writing a map value.voidwriteNil()Writes a nil (null) value.longwritePayload(InputStream stream, long length) Writes up tolengthbytes read from the given input stream.voidwritePayload(ByteBuffer buffer) Writes the remaining bytes of the given byte buffer.voidwritePayload(ByteBuffer... buffers) Writes the remaining bytes of the given byte buffers.longwritePayload(ReadableByteChannel channel, long length) Writes up tolengthbytes read from the given channel.voidwriteStringHeader(int length) Starts writing a string value.voidwriteUnsigned(byte value) Writes an integer value that fits into abyteinterpreted as unsigned value.voidwriteUnsigned(int value) Writes an integer value that fits into anintinterpreted as unsigned value.voidwriteUnsigned(long value) Writes an integer value that fits into alonginterpreted as unsigned value.voidwriteUnsigned(short value) Writes an integer value that fits into ashortinterpreted as unsigned value.
-
Method Details
-
of
Returns a new message writer that writes to the given channel.- Parameters:
channel- the channel to write to- Returns:
- a new message writer that writes to the given channel
-
of
static MessageWriter of(WritableByteChannel channel, Consumer<MessageWriter.OptionBuilder> optionHandler) Returns a new message writer with the given options that writes to the given channel.- Parameters:
channel- the channel to write tooptionHandler- a handler that receives anMessageWriter.OptionBuilder- Returns:
- a new message writer with the given options that writes to the given channel
-
of
Returns a new message writer that writes to the given output stream.- Parameters:
stream- the output stream to write to- Returns:
- a new message writer that writes to the given output stream
-
of
Returns a new message writer with the given options that writes to the given output stream.- Parameters:
stream- the output stream to write tooptionHandler- a handler that receives anMessageWriter.OptionBuilder- Returns:
- a new message writer with the given options that writes to the given output stream
-
of
Returns a new message writer that writes to the given buffer output.- Parameters:
output- the buffer output to write to- Returns:
- a new message writer that writes to the given buffer output
-
of
static MessageWriter of(MessageOutput.Buffer output, Consumer<MessageWriter.OptionBuilder> optionHandler) Returns a new message writer with the given options that writes to the given buffer output.- Parameters:
output- the buffer output to write tooptionHandler- a handler that receives anMessageWriter.OptionBuilder- Returns:
- a new message writer that writes to the given buffer output
-
of
Returns a new message writer that writes to the given sink provider.- Parameters:
provider- the sink provider to write to- Returns:
- a new message writer that writes to the given sink provider
-
of
static MessageWriter of(MessageSink.Provider provider, Consumer<MessageWriter.OptionBuilder> optionHandler) Returns a new message writer with the given options that writes to the given sink provider.- Parameters:
provider- the sink provider to write tooptionHandler- a handler that receives anMessageWriter.OptionBuilder- Returns:
- a new message writer with the given options that writes to the given sink provider
-
ofDiscarding
Returns a new message writer that discards any bytes written.- Returns:
- a new message writer that discards any bytes written
-
ofDiscarding
Returns a new message writer with the given underlying buffer that discards any bytes written.
This method is intended for testing and benchmarking. It should not be used in production code.
- Parameters:
buffer- the buffer to use- Returns:
- a new message writer with the given underlying buffer that discards any bytes written
-
writeNil
Writes a nil (null) value.- Throws:
IOException- if an I/O error occurs
-
write
Writes a boolean value.- Parameters:
value- the boolean value to write- Throws:
IOException- if an I/O error occurs
-
write
Writes an integer value that fits intobyte.- Parameters:
value- the integer value to write- Throws:
IOException- if an I/O error occurs
-
write
Writes an integer value that fits intoshort.- Parameters:
value- the integer value to write- Throws:
IOException- if an I/O error occurs
-
write
Writes an integer value that fits intoint.- Parameters:
value- the integer value to write- Throws:
IOException- if an I/O error occurs
-
write
Writes an integer value that fits intolong.- Parameters:
value- the integer value to write- Throws:
IOException- if an I/O error occurs
-
write
Writes a floating point value that fits intofloat.- Parameters:
value- the floating point value to write- Throws:
IOException- if an I/O error occurs
-
write
Writes a floating point value that fits intodouble.- Parameters:
value- the floating point value to write- Throws:
IOException- if an I/O error occurs
-
write
Writes a timestamp value.- Parameters:
value- the timestamp value to write- Throws:
IOException- if an I/O error occurs
-
write
Writes a string value.
To write a string value as a sequence of bytes, call
writeStringHeader(int)followed bywritePayload(java.nio.ByteBuffer).- Parameters:
string- the string value to write- Throws:
IOException- if an I/O error occurs
-
writeIdentifier
Writes a string value that is used as identifier.
Calling this method has the same effect as calling
write(CharSequence)except that it indicates to this message writer that it may want to cache the given value.- Parameters:
identifier- the identifier to write- Throws:
IOException- if an I/O error occurs
-
write
Encodes and writes a value.- Type Parameters:
T- the type of value to encode and write- Parameters:
value- the value to encode and writeencoder- the encoder to use- Throws:
IOException- if an I/O error occurs
-
writeArrayHeader
Starts writing an array value.
A call to this method must be followed by
elementCountwriteXxxcalls that write the array's elements.- Parameters:
elementCount- the number of elements in the array- Throws:
IOException- if an I/O error occurs
-
writeMapHeader
Starts writing a map value.
A call to this method must be followed by
entryCount * 2writeXxxcalls that alternately write the map's keys and values.- Parameters:
entryCount- the number of entries in the map- Throws:
IOException- if an I/O error occurs
-
writeStringHeader
Starts writing a string value.
A call to this method must be immediately followed by calls to
writePayload(java.nio.ByteBuffer)that writelengthbytes in total.This method is a low-level alternative to
write(CharSequence).- Parameters:
length- the length, in bytes, of the string value's payload- Throws:
IOException- if an I/O error occurs
-
writeBinaryHeader
Starts writing a binary value.
A call to this method must be immediately followed by calls to
writePayload(java.nio.ByteBuffer)that writelengthbytes in total.- Parameters:
length- , the length, in bytes, of the binary value's payload- Throws:
IOException- if an I/O error occurs
-
writeExtensionHeader
Starts writing an extension value.- Parameters:
length- the length, in bytes, of the extension valuetype- the numeric identifier of the extension value's type- Throws:
IOException- if an I/O error occurs
-
writePayload
Writes the remaining bytes of the given byte buffer.
Calls to
writePayload(java.nio.ByteBuffer)must be preceeded by a call towriteStringHeader(int),writeBinaryHeader(int), orwriteExtensionHeader(int, byte).- Parameters:
buffer- the byte buffer to write- Throws:
IOException- if an I/O error occurs
-
writePayload
Writes the remaining bytes of the given byte buffers.
Calls to
writePayload(java.nio.ByteBuffer)must be preceeded by a call towriteStringHeader(int),writeBinaryHeader(int), orwriteExtensionHeader(int, byte).- Parameters:
buffers- the byte buffers to write- Throws:
IOException- if an I/O error occurs
-
writePayload
Writes up to
lengthbytes read from the given channel.Fewer than
lengthbytes may be written if the channel's end of input is reached.Calls to
writePayload(java.nio.ByteBuffer)must be preceeded by a call towriteStringHeader(int),writeBinaryHeader(int), orwriteExtensionHeader(int, byte).- Parameters:
channel- the channel to read fromlength- the number of bytes to write- Returns:
- the actual number of bytes written (between 0 and
length) - Throws:
IOException- if an I/O error occurs
-
writePayload
Writes up to
lengthbytes read from the given input stream.Fewer than
lengthbytes may be written if the input stream's end of input is reached.Calls to
writePayload(java.nio.ByteBuffer)must be preceeded by a call towriteStringHeader(int),writeBinaryHeader(int), orwriteExtensionHeader(int, byte).- Parameters:
stream- the input stream to read fromlength- the number of bytes to write- Returns:
- the actual number of bytes written (between 0 and
length) - Throws:
IOException- if an I/O error occurs
-
writeUnsigned
Writes an integer value that fits into abyteinterpreted as unsigned value.- Parameters:
value- the integer value to write- Throws:
IOException- if an I/O error occurs
-
writeUnsigned
Writes an integer value that fits into ashortinterpreted as unsigned value.- Parameters:
value- the integer value to write- Throws:
IOException- if an I/O error occurs
-
writeUnsigned
Writes an integer value that fits into anintinterpreted as unsigned value.- Parameters:
value- the integer value to write- Throws:
IOException- if an I/O error occurs
-
writeUnsigned
Writes an integer value that fits into alonginterpreted as unsigned value.- Parameters:
value- the integer value to write- Throws:
IOException- if an I/O error occurs
-
flush
Flushes this message writer.
It is not necessary to explicitly flush a writer before closing it.
If this writer was created with
of(OutputStream), this method callsOutputStream.flush(). If this writer was created withof(MessageSink.Provider), this method callsMessageSink.Provider.close().- Throws:
IOException- if an I/O error occurs
-
close
Closes this message writer.
It is not necessary to explicitly flush a writer before closing it.
If this writer was created with
of(WritableByteChannel), this method callsChannel.close(). If this writer was created withof(OutputStream), this method callsOutputStream.close(). If this writer was created withof(MessageSink.Provider), this method callsMessageSink.Provider.close().Subsequent calls to this method have no effect. Continued use of a closed message writer can result in data corruption and may throw
IllegalStateException.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if an I/O error occurs
-