Package com.fasterxml.jackson.core.util
Class ByteArrayBuilder
- java.lang.Object
-
- java.io.OutputStream
-
- com.fasterxml.jackson.core.util.ByteArrayBuilder
-
- All Implemented Interfaces:
BufferRecycler.Gettable,java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public final class ByteArrayBuilder extends java.io.OutputStream implements BufferRecycler.Gettable
Helper class that is similar toByteArrayOutputStreamin usage, but more geared to Jackson use cases internally. Specific changes include segment storage (no need to have linear backing buffer, can avoid reallocations, copying), as well API not based onOutputStream. In short, a very much specialized builder object.Also implements
OutputStreamto allow efficient aggregation of output content as a byte array, similar to howByteArrayOutputStreamworks, but somewhat more efficiently for many use cases.NOTE: maximum size limited to Java Array maximum, 2 gigabytes: this because usage pattern is to collect content for a `byte[]` and so although theoretically this builder can aggregate more content it will not be usable as things are. Behavior may be improved if we solve the access problem.
-
-
Field Summary
Fields Modifier and Type Field Description static byte[]NO_BYTES
-
Constructor Summary
Constructors Constructor Description ByteArrayBuilder()ByteArrayBuilder(int firstBlockSize)ByteArrayBuilder(BufferRecycler br)ByteArrayBuilder(BufferRecycler br, int firstBlockSize)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidappend(int i)voidappendFourBytes(int b32)voidappendThreeBytes(int b24)voidappendTwoBytes(int b16)BufferRecyclerbufferRecycler()voidclose()byte[]completeAndCoalesce(int lastBlockLength)Method that will complete "manual" output process, coalesce content (if necessary) and return results as a contiguous buffer.byte[]finishCurrentSegment()Method called when the current segment buffer is full; will append to current contents, allocate a new segment buffer and return itvoidflush()static ByteArrayBuilderfromInitial(byte[] initialBlock, int length)byte[]getClearAndRelease()Method functionally same as calling:byte[]getCurrentSegment()intgetCurrentSegmentLength()voidrelease()Clean up method to call to release all buffers this object may be using.voidreset()byte[]resetAndGetFirstSegment()Method called when starting "manual" output: will clear out current state and return the first segment buffer to fillvoidsetCurrentSegmentLength(int len)intsize()byte[]toByteArray()Method called when results are finalized and we can get the full aggregated result buffer to return to the callervoidwrite(byte[] b)voidwrite(byte[] b, int off, int len)voidwrite(int b)
-
-
-
Constructor Detail
-
ByteArrayBuilder
public ByteArrayBuilder()
-
ByteArrayBuilder
public ByteArrayBuilder(BufferRecycler br)
-
ByteArrayBuilder
public ByteArrayBuilder(int firstBlockSize)
-
ByteArrayBuilder
public ByteArrayBuilder(BufferRecycler br, int firstBlockSize)
-
-
Method Detail
-
fromInitial
public static ByteArrayBuilder fromInitial(byte[] initialBlock, int length)
-
reset
public void reset()
-
size
public int size()
- Returns:
- Number of bytes aggregated so far
- Since:
- 2.9
-
release
public void release()
Clean up method to call to release all buffers this object may be using. After calling the method, no other accessors can be used (and attempt to do so may result in an exception).
-
append
public void append(int i)
-
appendTwoBytes
public void appendTwoBytes(int b16)
-
appendThreeBytes
public void appendThreeBytes(int b24)
-
appendFourBytes
public void appendFourBytes(int b32)
-
toByteArray
public byte[] toByteArray()
Method called when results are finalized and we can get the full aggregated result buffer to return to the caller- Returns:
- Aggregated contents as a
byte[]
-
getClearAndRelease
public byte[] getClearAndRelease()
Method functionally same as calling:byte[] bytes = toByteArray(); release(); return bytes;
that is; aggregates output contained in the builder (if any), clear state; returns buffer(s) toBufferRecyclerconfigured, if any, and returns output to caller.- Since:
- 2.17
-
bufferRecycler
public BufferRecycler bufferRecycler()
- Specified by:
bufferRecyclerin interfaceBufferRecycler.Gettable- Returns:
- Buffer recycler instance object is configured with, if any;
whether this can be
nulldepends on type of object
-
resetAndGetFirstSegment
public byte[] resetAndGetFirstSegment()
Method called when starting "manual" output: will clear out current state and return the first segment buffer to fill- Returns:
- Segment to use for writing
-
finishCurrentSegment
public byte[] finishCurrentSegment()
Method called when the current segment buffer is full; will append to current contents, allocate a new segment buffer and return it- Returns:
- Segment to use for writing
-
completeAndCoalesce
public byte[] completeAndCoalesce(int lastBlockLength)
Method that will complete "manual" output process, coalesce content (if necessary) and return results as a contiguous buffer.- Parameters:
lastBlockLength- Amount of content in the current segment buffer.- Returns:
- Coalesced contents
-
getCurrentSegment
public byte[] getCurrentSegment()
-
setCurrentSegmentLength
public void setCurrentSegmentLength(int len)
-
getCurrentSegmentLength
public int getCurrentSegmentLength()
-
write
public void write(byte[] b)
- Overrides:
writein classjava.io.OutputStream
-
write
public void write(byte[] b, int off, int len)- Overrides:
writein classjava.io.OutputStream
-
write
public void write(int b)
- Specified by:
writein classjava.io.OutputStream
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.OutputStream
-
flush
public void flush()
- Specified by:
flushin interfacejava.io.Flushable- Overrides:
flushin classjava.io.OutputStream
-
-