Top Description Fields Constructors Methods
org.apache.derby.iapi.services.io

public Class DynamicByteArrayOutputStream

extends OutputStream
Class Inheritance
Imports
org.apache.derby.shared.common.sanity.SanityManager, java.io.InputStream, .IOException, .OutputStream

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

Modifier and TypeField and Description
private int
private byte[]
private static int
private int
private int

Constructor Summary

AccessConstructor and Description
public
public
public
public

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Closes this output stream and releases any system resources associated with this stream.

public void
discardLeft(int
The amount to lop off
amountToShrinkBy
)

Shrink the buffer left by the amount given.

private void
expandBuffer(int minExtension)

Expand the buffer by at least the number of bytes requested in minExtension.

public int

Returns:

the current position in the stream
getBeginPosition
()

Get the current position in the stream

public byte[]

Returns:

the wrapped byte array
getByteArray
()

Get a reference to the byte array stored in the byte array output stream.

public int

Returns:

the current position in the stream
getPosition
()

Get the current position in the stream

public int

Returns:

the number of used bytes
getUsed
()

Get the number of bytes that was used.

public void
reset()

Reset the stream for reuse

public void
setBeginPosition(int
The new start position
newBeginPosition
)

Set the begin position of the stream pointer.

public void
setPosition(int
The new position of the stream pointer
newPosition
)

Set the position of the stream pointer.

public void
write(int
the byte.
b
)

Implements abstract java.io.OutputStream.write.

Writes the specified byte to this output stream.

public void
write(byte[]
the data.
b
,
int
the start offset in the data.
off
,
int
the number of bytes to write.
len
)

Overrides java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream.

pack-priv void
Inherited from java.io.OutputStream:
flushnullOutputStreamwrite

Field Detail

beginPositionback to summary
private int beginPosition
bufback to summary
private byte[] buf
INITIAL_SIZEback to summary
private static int INITIAL_SIZE
positionback to summary
private int position
usedback to summary
private int used

Constructor Detail

DynamicByteArrayOutputStreamback to summary
public DynamicByteArrayOutputStream()
DynamicByteArrayOutputStreamback to summary
public DynamicByteArrayOutputStream(int size)
DynamicByteArrayOutputStreamback to summary
public DynamicByteArrayOutputStream(byte[] data)
DynamicByteArrayOutputStreamback to summary
public DynamicByteArrayOutputStream(DynamicByteArrayOutputStream toBeCloned)

Method Detail

closeback to summary
public void close()

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.OutputStream.close.

Closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.

discardLeftback to summary
public void discardLeft(int amountToShrinkBy)

Shrink the buffer left by the amount given. Ie. bytes from 0 to amountToShrinkBy are thrown away

Parameters
amountToShrinkBy:int

The amount to lop off

expandBufferback to summary
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.
In all cases, if minExpansion is greater than the value about then the buffer will be increased by minExtension.
getBeginPositionback to summary
public int getBeginPosition()

Get the current position in the stream

Returns:int

the current position in the stream

getByteArrayback to summary
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.

Returns:byte[]

the wrapped byte array

getPositionback to summary
public int getPosition()

Get the current position in the stream

Returns:int

the current position in the stream

getUsedback to summary
public int getUsed()

Get the number of bytes that was used.

Returns:int

the number of used bytes

resetback to summary
public void reset()

Reset the stream for reuse

setBeginPositionback to summary
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.

Parameters
newBeginPosition:int

The new start position

setPositionback to summary
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.

Parameters
newPosition:int

The new position of the stream pointer

writeback to summary
public void write(int b)

Implements abstract java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Parameters
b:int

the byte.

writeback to summary
public void write(byte[] b, int off, int len)

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.

If b is null, a NullPointerException is thrown.

If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.

Parameters
b:byte[]

the data.

off:int

the start offset in the data.

len:int

the number of bytes to write.

writeCompleteStreamback to summary
pack-priv void writeCompleteStream(InputStream dataIn, int len) throws IOException