Class ArrayBufferOutput
- java.lang.Object
-
- com.batch.android.msgpack.core.buffer.ArrayBufferOutput
-
- All Implemented Interfaces:
MessageBufferOutput,java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public class ArrayBufferOutput extends java.lang.Object 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(byte[] buffer, int offset, int length)Writes an external payload data.voidclear()Clears the written data.voidclose()voidflush()intgetSize()Gets the size of the written data.MessageBuffernext(int minimumSize)Allocates the next buffer to write.java.util.List<MessageBuffer>toBufferList()Returns the written data as a list of MessageBuffer.byte[]toByteArray()Gets a copy of the written data as a byte array.MessageBuffertoMessageBuffer()Gets the written data as a MessageBuffer.voidwrite(byte[] buffer, int offset, int length)Writes an external payload data.voidwriteBuffer(int length)Writes the previously allocated buffer.
-
-
-
Method Detail
-
getSize
public int getSize()
Gets the size of the written data.- Returns:
- number of bytes
-
toByteArray
public 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()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
-
clear
public void clear()
Clears the written data.
-
next
public MessageBuffer next(int minimumSize)
Description copied from interface:MessageBufferOutputAllocates 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 called twice without call of
MessageBufferOutput.writeBuffer(int)in between. In this case, the buffer should be discarded without flushing it to the output.- Specified by:
nextin interfaceMessageBufferOutput- Parameters:
minimumSize- the mimium required buffer size to allocate- Returns:
- the MessageBuffer instance with at least minimumSize bytes of capacity
-
writeBuffer
public void writeBuffer(int length)
Description copied from interface:MessageBufferOutputWrites the previously allocated buffer.This method should write the buffer previously returned from
MessageBufferOutput.next(int)method until specified number of bytes. Once the buffer is written, the buffer is no longer used.This method is not always called for each
MessageBufferOutput.next(int)call. In this case, the buffer should be discarded without flushing it to the output when the nextMessageBufferOutput.next(int)is called.- Specified by:
writeBufferin interfaceMessageBufferOutput- Parameters:
length- the number of bytes to write
-
write
public void write(byte[] buffer, int offset, int length)Description copied from interface:MessageBufferOutputWrites an external payload data. This method should follow semantics of OutputStream.- Specified by:
writein interfaceMessageBufferOutput- Parameters:
buffer- the data to writeoffset- the start offset in the datalength- the number of bytes to write
-
add
public void add(byte[] buffer, int offset, int length)Description copied from interface:MessageBufferOutputWrites an external payload data.Unlike
MessageBufferOutput.write(byte[], int, int)method, the buffer is given - this MessageBufferOutput implementation gets ownership of the buffer and may modify contents of the buffer. Contents of this buffer won't be modified by the caller.- Specified by:
addin interfaceMessageBufferOutput- Parameters:
buffer- the data to addoffset- the start offset in the datalength- the number of bytes to add
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
flush
public void flush()
- Specified by:
flushin interfacejava.io.Flushable
-
-