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,
ByteBufferAllocator allocator)
Deprecated.
|
CapacityByteArrayOutputStream(int initialSlabSize,
int maxCapacityHint)
Deprecated.
|
CapacityByteArrayOutputStream(int initialSlabSize,
int maxCapacityHint,
ByteBufferAllocator allocator) |
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
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) |
static CapacityByteArrayOutputStream |
withTargetNumSlabs(int minSlabSize,
int maxCapacityHint,
int targetNumSlabs,
ByteBufferAllocator allocator)
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.
|
flush, write@Deprecated public CapacityByteArrayOutputStream(int initialSlabSize)
CapacityByteArrayOutputStream(int, int, ByteBufferAllocator)initialSlabSize - an initial slab size@Deprecated public CapacityByteArrayOutputStream(int initialSlabSize, ByteBufferAllocator allocator)
CapacityByteArrayOutputStream(int, int, ByteBufferAllocator)initialSlabSize - an initial slab sizeallocator - an allocator to use when creating byte buffers for slabs@Deprecated public CapacityByteArrayOutputStream(int initialSlabSize, int maxCapacityHint)
CapacityByteArrayOutputStream(int, int, ByteBufferAllocator)initialSlabSize - the size to make the first slabmaxCapacityHint - a hint (not guarantee) of the max amount of data written to this streampublic CapacityByteArrayOutputStream(int initialSlabSize,
int maxCapacityHint,
ByteBufferAllocator allocator)
initialSlabSize - the size to make the first slabmaxCapacityHint - a hint (not guarantee) of the max amount of data written to this streamallocator - an allocator to use when creating byte buffers for slabspublic 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)
public static CapacityByteArrayOutputStream withTargetNumSlabs(int minSlabSize, int maxCapacityHint, int targetNumSlabs, ByteBufferAllocator allocator)
initialSlabSizeHeuristic(int, int, int), with targetCapacity == maxCapacityHintminSlabSize - a minimum slab sizemaxCapacityHint - a hint for the maximum required capacitytargetNumSlabs - the target number of slabsallocator - an allocator to use when creating byte buffers for slabspublic 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 withpublic String memUsageString(String prefix)
prefix - a prefix to be used for every new line in the stringpublic void close()
close in interface Closeableclose in interface AutoCloseableclose in class OutputStreamCopyright © 2018 The Apache Software Foundation. All rights reserved.