Package org.infinispan.commons.io
Class LazyByteArrayOutputStream
java.lang.Object
java.io.OutputStream
org.infinispan.commons.io.LazyByteArrayOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
ByteArrayOutputStream alternative exposing the internal buffer. Using this, callers don't need to call toByteArray()
which copies the internal buffer.
This class doubles the size until the internal buffer reaches a configurable max size (default is 4MB), after which it begins growing the buffer in 25% increments. This is intended to help prevent an OutOfMemoryError during a resize of a large buffer.
A version of this class was originally created by Bela Ban as part of the JGroups library.
This class is not threadsafe as it will not support concurrent readers and writers.
- Since:
- 13.0
- Author:
- Brian Stansberry, Dan Berindei
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault buffer size after which if more buffer capacity is needed the buffer will grow by 25% rather than 100%static final int -
Constructor Summary
ConstructorsConstructorDescriptionLazyByteArrayOutputStream(int size) LazyByteArrayOutputStream(int size, int maxDoublingSize) Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes. -
Method Summary
Modifier and TypeMethodDescriptionfinal intGets the highest internal buffer size after which if more capacity is needed the buffer will grow in 25% increments rather than 100%.intgetNewBufferSize(int curSize, int minNewSize) Gets the number of bytes to which the internal buffer should be resized.byte[]Gets the internal buffer array.byte[]Gets a buffer that is trimmed so that there are no excess bytes.intsize()Overriden only to avoid unneeded synchronizationvoidwrite(byte[] b, int off, int len) voidwrite(int b) Methods inherited from class java.io.OutputStream
close, flush, nullOutputStream, write
-
Field Details
-
DEFAULT_SIZE
public static final int DEFAULT_SIZE- See Also:
-
DEFAULT_DOUBLING_SIZE
public static final int DEFAULT_DOUBLING_SIZEDefault buffer size after which if more buffer capacity is needed the buffer will grow by 25% rather than 100%- See Also:
-
-
Constructor Details
-
LazyByteArrayOutputStream
public LazyByteArrayOutputStream() -
LazyByteArrayOutputStream
public LazyByteArrayOutputStream(int size) -
LazyByteArrayOutputStream
public LazyByteArrayOutputStream(int size, int maxDoublingSize) Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.- Parameters:
size- the initial size.maxDoublingSize- the buffer size, after which if more capacity is needed the buffer will grow by 25% rather than 100%- Throws:
IllegalArgumentException- if size is negative.
-
-
Method Details
-
getRawBuffer
public byte[] getRawBuffer()Gets the internal buffer array. Note that the length of this array will almost certainly be longer than the data written to it; callsize()to get the number of bytes of actual data. -
getTrimmedBuffer
public byte[] getTrimmedBuffer()Gets a buffer that is trimmed so that there are no excess bytes. If the current count does not match the underlying buffer than a new one is created with the written bytes.- Returns:
- a byte[] that contains all the bytes written to this stream
-
write
public void write(byte[] b, int off, int len) - Overrides:
writein classOutputStream
-
write
public void write(int b) - Specified by:
writein classOutputStream
-
getMaxDoublingSize
public final int getMaxDoublingSize()Gets the highest internal buffer size after which if more capacity is needed the buffer will grow in 25% increments rather than 100%. -
getNewBufferSize
public int getNewBufferSize(int curSize, int minNewSize) Gets the number of bytes to which the internal buffer should be resized.- Parameters:
curSize- the current number of bytesminNewSize- the minimum number of bytes required- Returns:
- the size to which the internal buffer should be resized
-
size
public int size()Overriden only to avoid unneeded synchronization
-