-
- All Implemented Interfaces:
-
com.batch.android.msgpack.core.buffer.MessageBufferOutput,java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public class ArrayBufferOutput implements MessageBufferOutput
MessageBufferOutput adapter that writes data into a list of byte arrays.
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.
-
-
Constructor Summary
Constructors Constructor Description ArrayBufferOutput()ArrayBufferOutput(int bufferSize)
-
Method Summary
Modifier and Type Method Description intgetSize()Gets the size of the written data. Array<byte>toByteArray()Gets a copy of the written data as a byte array. MessageBuffertoMessageBuffer()Gets the written data as a MessageBuffer. List<MessageBuffer>toBufferList()Returns the written data as a list of MessageBuffer. voidclear()Clears the written data. MessageBuffernext(int minimumSize)Allocates the next buffer to write. voidwriteBuffer(int length)Writes the previously allocated buffer. voidwrite(Array<byte> buffer, int offset, int length)Writes an external payload data.This method should follow semantics of OutputStream. voidadd(Array<byte> buffer, int offset, int length)Writes an external payload data. voidclose()voidflush()-
-
Method Detail
-
getSize
int getSize()
Gets the size of the written data.
-
toByteArray
Array<byte> toByteArray()
Gets a copy of the written data as a byte array.
If your application needs better performance and smaller memory consumption, you may prefer toMessageBuffer or toBufferList to avoid copying.
-
toMessageBuffer
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 smallerthan a single buffer capacity.
-
toBufferList
List<MessageBuffer> toBufferList()
Returns the written data as a list of MessageBuffer.
Unlike toByteArray or toMessageBuffer, this is the fastest method that doesn'tcopy contents in any cases.
-
clear
void clear()
Clears the written data.
-
next
MessageBuffer next(int minimumSize)
Allocates the next buffer to write.
This method should return a MessageBuffer instance that has specified size of capacity at least.
When this method is called twice, the previously returned buffer is no longer used. This method may be calledtwice without call of writeBuffer in between. In this case, the buffer should bediscarded without flushing it to the output.
- Parameters:
minimumSize- the mimium required buffer size to allocate
-
writeBuffer
void writeBuffer(int length)
Writes the previously allocated buffer.
This method should write the buffer previously returned from next method until specified number ofbytes. Once the buffer is written, the buffer is no longer used.
This method is not always called for each next call. In this case, the buffer should be discardedwithout flushing it to the output when the next next is called.
- Parameters:
length- the number of bytes to write
-
write
void write(Array<byte> buffer, int offset, int length)
Writes an external payload data.This method should follow semantics of OutputStream.
- Parameters:
buffer- the data to writeoffset- the start offset in the datalength- the number of bytes to write
-
add
void add(Array<byte> buffer, int offset, int length)
Writes an external payload data.
Unlike write method, the buffer is given - this MessageBufferOutput implementationgets ownership of the buffer and may modify contents of the buffer. Contents of this buffer won't be modifiedby the caller.
- Parameters:
buffer- the data to addoffset- the start offset in the datalength- the number of bytes to add
-
close
void close()
-
flush
void flush()
-
-
-
-