public class CapacityByteArrayOutputStream extends OutputStream
ByteArrayOutputStream, but uses a different strategy for growing that does not involve copying.
Where ByteArrayOutputStream is backed by a single array that "grows" by copying into a new larger array, this output
stream grows by allocating a new array (slab) and adding it to a list of previous arrays.
Each new slab is allocated to be the same size as all the previous slabs combined, so these allocations become
exponentially less frequent, just like ByteArrayOutputStream, with one difference. This output stream accepts a
max capacity hint, which is a hint describing the max amount of data that will be written to this stream. As the
total size of this stream nears this max, this stream starts to grow linearly instead of exponentially.
So new slabs are allocated to be 1/5th of the max capacity hint,
instead of equal to the total previous size of all slabs. This is useful because it prevents allocating roughly
twice the needed space when a new slab is added just before the stream is done being used.
When reusing this stream it will adjust the initial slab size based on the previous data size, aiming for fewer
allocations, with the assumption that a similar amount of data will be written to this stream on re-use.
See (reset()).| Constructor and Description |
|---|
CapacityByteArrayOutputStream(int initialSlabSize)
Deprecated.
|
CapacityByteArrayOutputStream(int initialSlabSize,
int maxCapacityHint) |
| Modifier and Type | Method and Description |
|---|---|
int |
getCapacity() |
long |
getCurrentIndex() |
static int |
initialSlabSizeHeuristic(int minSlabSize,
int targetCapacity,
int targetNumSlabs)
Return an initial slab size such that a CapacityByteArrayOutputStream constructed with it
will end up allocating targetNumSlabs in order to reach targetCapacity.
|
String |
memUsageString(String prefix) |
void |
reset()
When re-using an instance with reset, it will adjust slab size based on previous data size.
|
void |
setByte(long index,
byte value)
Replace the byte stored at position index in this stream with value
|
long |
size() |
static CapacityByteArrayOutputStream |
withTargetNumSlabs(int minSlabSize,
int maxCapacityHint,
int targetNumSlabs)
Construct a CapacityByteArrayOutputStream configured such that its initial slab size is
determined by
initialSlabSizeHeuristic(int, int, int), with targetCapacity == maxCapacityHint |
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
void |
writeTo(OutputStream out)
Writes the complete contents of this buffer to the specified output stream argument.
|
close, flush, write@Deprecated public CapacityByteArrayOutputStream(int initialSlabSize)
CapacityByteArrayOutputStream(int, int)initialSlabSize - public CapacityByteArrayOutputStream(int initialSlabSize,
int maxCapacityHint)
initialSlabSize - the size to make the first slabmaxCapacityHint - a hint (not guarantee) of the max amount of data written to this streampublic static int initialSlabSizeHeuristic(int minSlabSize,
int targetCapacity,
int targetNumSlabs)
minSlabSize - no matter what we shouldn't make slabs any smaller than thistargetCapacity - after we've allocated targetNumSlabs how much capacity should we have?targetNumSlabs - how many slabs should it take to reach targetCapacity?public static CapacityByteArrayOutputStream withTargetNumSlabs(int minSlabSize, int maxCapacityHint, int targetNumSlabs)
initialSlabSizeHeuristic(int, int, int), with targetCapacity == maxCapacityHintpublic void write(int b)
write in class OutputStreampublic void write(byte[] b,
int off,
int len)
write in class OutputStreampublic void writeTo(OutputStream out) throws IOException
out.write(slab, 0, slab.length)) will be called once per slab.out - the output stream to which to write the data.IOException - if an I/O error occurs.public long size()
public int getCapacity()
public void reset()
public long getCurrentIndex()
setByte(long, byte) in order to change itpublic void setByte(long index,
byte value)
index - which byte to replacevalue - the value to replace it withCopyright © 2015 The Apache Software Foundation. All rights reserved.