Class MessageBufferPacker
- java.lang.Object
-
- org.msgpack.core.MessagePacker
-
- org.msgpack.core.MessageBufferPacker
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
public class MessageBufferPacker extends MessagePacker
MessagePacker that is useful to produce byte array output.This class allocates a new buffer instead of resizing the buffer when data doesn't fit in the initial capacity. This is faster than ByteArrayOutputStream especially when size of written bytes is large because resizing a buffer usually needs to copy contents of the buffer.
-
-
Field Summary
-
Fields inherited from class org.msgpack.core.MessagePacker
out
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
MessageBufferPacker(ArrayBufferOutput out, MessagePack.PackerConfig config)
protected
MessageBufferPacker(MessagePack.PackerConfig config)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Clears the written data.int
getBufferSize()
MessageBufferOutput
reset(MessageBufferOutput out)
Replaces underlying output.java.util.List<MessageBuffer>
toBufferList()
Returns the written data as a list of MessageBuffer.byte[]
toByteArray()
Gets copy of the written data as a byte array.MessageBuffer
toMessageBuffer()
Gets the written data as a MessageBuffer.-
Methods inherited from class org.msgpack.core.MessagePacker
addPayload, addPayload, close, flush, getTotalWrittenBytes, packArrayHeader, packBigInteger, packBinaryHeader, packBoolean, packByte, packDouble, packExtensionTypeHeader, packFloat, packInt, packLong, packMapHeader, packNil, packRawStringHeader, packShort, packString, packValue, writePayload, writePayload
-
-
-
-
Constructor Detail
-
MessageBufferPacker
protected MessageBufferPacker(MessagePack.PackerConfig config)
-
MessageBufferPacker
protected MessageBufferPacker(ArrayBufferOutput out, MessagePack.PackerConfig config)
-
-
Method Detail
-
reset
public MessageBufferOutput reset(MessageBufferOutput out) throws java.io.IOException
Description copied from class:MessagePacker
Replaces underlying output.This method flushes current internal buffer to the output, swaps it with the new given output, then returns the old output.
This method doesn't close the old output.
- Overrides:
reset
in classMessagePacker
- Parameters:
out
- new output- Returns:
- the old output
- Throws:
java.io.IOException
- when underlying output throws IOException
-
clear
public void clear()
Description copied from class:MessagePacker
Clears the written data.- Overrides:
clear
in classMessagePacker
-
toByteArray
public byte[] toByteArray()
Gets copy of the written data as a byte array.If your application needs better performance and smaller memory consumption, you may prefer
toMessageBuffer()
ortoBufferList()
to avoid copying.- Returns:
- the byte array
-
toMessageBuffer
public MessageBuffer toMessageBuffer()
Gets the written data as a MessageBuffer.Unlike
toByteArray()
, this method omits copy of the contents if size of the written data is smaller than a single buffer capacity.- Returns:
- the MessageBuffer instance
-
toBufferList
public java.util.List<MessageBuffer> toBufferList()
Returns the written data as a list of MessageBuffer.Unlike
toByteArray()
ortoMessageBuffer()
, this is the fastest method that doesn't copy contents in any cases.- Returns:
- the list of MessageBuffer instances
-
getBufferSize
public int getBufferSize()
- Returns:
- the size of the buffer in use
-
-