Class DynamicByteArrayOutputStream
java.lang.Object
java.io.OutputStream
org.apache.derby.iapi.services.io.DynamicByteArrayOutputStream
- All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
A DynamicByteArrayOutputStream allows writing to a dynamically resizable
array of bytes. In addition to dynamic resizing, this extension allows
the user of this class to have more control over the position of the stream
and can get a direct reference of the array.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intprivate byte[]private static intprivate intprivate int -
Constructor Summary
ConstructorsConstructorDescriptionDynamicByteArrayOutputStream(byte[] data) DynamicByteArrayOutputStream(int size) -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voiddiscardLeft(int amountToShrinkBy) Shrink the buffer left by the amount given.private voidexpandBuffer(int minExtension) Expand the buffer by at least the number of bytes requested in minExtension.intGet the current position in the streambyte[]Get a reference to the byte array stored in the byte array output stream.intGet the current position in the streamintgetUsed()Get the number of bytes that was used.voidreset()Reset the stream for reusevoidsetBeginPosition(int newBeginPosition) Set the begin position of the stream pointer.voidsetPosition(int newPosition) Set the position of the stream pointer.voidwrite(byte[] b, int off, int len) voidwrite(int b) (package private) voidwriteCompleteStream(InputStream dataIn, int len) Methods inherited from class OutputStream
flush, nullOutputStream, write
-
Field Details
-
INITIAL_SIZE
private static int INITIAL_SIZE -
buf
private byte[] buf -
position
private int position -
used
private int used -
beginPosition
private int beginPosition
-
-
Constructor Details
-
DynamicByteArrayOutputStream
public DynamicByteArrayOutputStream() -
DynamicByteArrayOutputStream
public DynamicByteArrayOutputStream(int size) -
DynamicByteArrayOutputStream
public DynamicByteArrayOutputStream(byte[] data) -
DynamicByteArrayOutputStream
-
-
Method Details
-
write
public void write(int b) - Specified by:
writein classOutputStream
-
write
public void write(byte[] b, int off, int len) - Overrides:
writein classOutputStream
-
writeCompleteStream
- Throws:
IOException
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream
-
reset
public void reset()Reset the stream for reuse -
getByteArray
public byte[] getByteArray()Get a reference to the byte array stored in the byte array output stream. Note that the byte array may be longer that getPosition(). Bytes beyond and including the current poistion are invalid. -
getUsed
public int getUsed()Get the number of bytes that was used. -
getPosition
public int getPosition()Get the current position in the stream -
getBeginPosition
public int getBeginPosition()Get the current position in the stream -
setPosition
public void setPosition(int newPosition) Set the position of the stream pointer. It is up to the caller to make sure the stream has no gap of garbage in it or useful information is not left out at the end because the stream does not remember anything about the previous position. -
setBeginPosition
public void setBeginPosition(int newBeginPosition) Set the begin position of the stream pointer. If the newBeginPosition is larger than the stream itself, then, the begin position is not set. -
discardLeft
public void discardLeft(int amountToShrinkBy) Shrink the buffer left by the amount given. Ie. bytes from 0 to amountToShrinkBy are thrown away -
expandBuffer
private void expandBuffer(int minExtension) Expand the buffer by at least the number of bytes requested in minExtension. To optimize performance and reduce memory copies and allocation, we have a staged buffer expansion.- buf.length < 128k - increase by 4k
- buf.length < 1Mb - increase by 128k
- otherwise increase by 1Mb.
-