public final class ByteArrayBuilder extends OutputStream implements BufferRecycler.Gettable
ByteArrayOutputStream
in 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 on OutputStream
. In short, a very much
specialized builder object.
Also implements OutputStream
to allow
efficient aggregation of output content as a byte array, similar
to how ByteArrayOutputStream
works, 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.
Modifier and Type | Field and Description |
---|---|
static byte[] |
NO_BYTES |
Constructor and Description |
---|
ByteArrayBuilder() |
ByteArrayBuilder(BufferRecycler br) |
ByteArrayBuilder(BufferRecycler br,
int firstBlockSize) |
ByteArrayBuilder(int firstBlockSize) |
Modifier and Type | Method and Description |
---|---|
void |
append(int i) |
void |
appendFourBytes(int b32) |
void |
appendThreeBytes(int b24) |
void |
appendTwoBytes(int b16) |
BufferRecycler |
bufferRecycler() |
void |
close() |
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 it
|
void |
flush() |
static ByteArrayBuilder |
fromInitial(byte[] initialBlock,
int length) |
byte[] |
getClearAndRelease()
Method functionally same as calling:
|
byte[] |
getCurrentSegment() |
int |
getCurrentSegmentLength() |
void |
release()
Clean up method to call to release all buffers this object may be
using.
|
void |
reset() |
byte[] |
resetAndGetFirstSegment()
Method called when starting "manual" output: will clear out
current state and return the first segment buffer to fill
|
void |
setCurrentSegmentLength(int len) |
int |
size() |
byte[] |
toByteArray()
Method called when results are finalized and we can get the
full aggregated result buffer to return to the caller
|
void |
write(byte[] b) |
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
public ByteArrayBuilder()
public ByteArrayBuilder(BufferRecycler br)
public ByteArrayBuilder(int firstBlockSize)
public ByteArrayBuilder(BufferRecycler br, int firstBlockSize)
public static ByteArrayBuilder fromInitial(byte[] initialBlock, int length)
public void reset()
public int size()
public void release()
public void append(int i)
public void appendTwoBytes(int b16)
public void appendThreeBytes(int b24)
public void appendFourBytes(int b32)
public byte[] toByteArray()
byte[]
public byte[] getClearAndRelease()
byte[] bytes = toByteArray(); release(); return bytes;that is; aggregates output contained in the builder (if any), clear state; returns buffer(s) to
BufferRecycler
configured,
if any, and returns output to caller.public BufferRecycler bufferRecycler()
bufferRecycler
in interface BufferRecycler.Gettable
null
depends on type of objectpublic byte[] resetAndGetFirstSegment()
public byte[] finishCurrentSegment()
public byte[] completeAndCoalesce(int lastBlockLength)
lastBlockLength
- Amount of content in the current segment
buffer.public byte[] getCurrentSegment()
public void setCurrentSegmentLength(int len)
public int getCurrentSegmentLength()
public void write(byte[] b)
write
in class OutputStream
public void write(byte[] b, int off, int len)
write
in class OutputStream
public void write(int b)
write
in class OutputStream
public void close()
close
in interface Closeable
close
in interface AutoCloseable
close
in class OutputStream
public void flush()
flush
in interface Flushable
flush
in class OutputStream
Copyright © 2008–2024 FasterXML. All rights reserved.