saker.util Documentation TaskDoc JavaDoc Packages
public class UnsyncByteArrayOutputStream extends OutputStream implements ByteSink
Class similar to ByteArrayOutputStream, but the methods are not sychronized.

The class also contains more functions for more complex manipulations.

For writing structured data to the stream, use the subclass DataOutputUnsyncByteArrayOutputStream.

Fields
protected byte[]
The buffer that holds the bytes.
protected int
The number of valid data in the buffer.
Constructors
public
Creates a new instance with the default buffer size of 128.
public
Creates a new instance with the argument buffer size.
public
UnsyncByteArrayOutputStream(byte[] buf, int count)
Creates a new instance that is directly backed by the argument buffer, and currently contains count number of valid bytes.
Methods
public void
Closes this output stream and releases any system resources associated with this stream.
public void
ensureCapacity(int mincapacity)
Grows the internal buffer of the stream to be able to hold the specified amount of bytes.
public byte[]
Gets the current backing buffer of the stream.
public int
Gets the current maximum number of bytes the stream can hold.
public int
indexOf(byte b)
Gets the first occurrence of a byte value in the stream.
public int
indexOf(byte b, int offset)
Gets the first occurrence of a byte after a given offset.
public boolean
Checks if the stream currently holds any bytes.
public int
lastIndexOf(byte b)
Gets the last occurrence of a byte in the stream.
public int
lastIndexOf(byte b, int offset)
Gets the last occurrence of a byte after a given offset.
public long
Reads bytes from the argument input stream until no more bytes are available.
public int
readFrom(InputStream in, int count)
Reads at most the given number of bytes from the input stream.
public long
Reads bytes from the argument byte source and writes it to this byte sink.
public int
readFrom(ByteSource in, int count)
Reads at most the given number of bytes from the byte source.
public void
reduceSize(int size)
Reduces the number of valid bytes in the stream.
public void
repeat(byte b, int count)
Appends the same byte data to the stream multiple times.
public void
replaceByte(int v, int offset)
Replaces a byte in the stream at the given offset.
public void
Resets the stream to hold 0 bytes.
public int
Gets the current number of valid bytes in the stream.
public byte[]
Gets the currently present data in the stream in a newly allocated array.
public ByteArrayRegion
Converts the current data buffer to a ByteArrayRegion.
public String
Converts the current contents of the stream to a string representation by decoding it using UTF-8.
public String
toString(int offset, int length)
Converts the specified region of data of the stream to a string representation by decoding it using UTF-8.
public String
toString(int offset, int length, Charset charset)
Converts the specified region of data of the stream to a string representation by decoding it using the argument charset.
public String
toString(Charset charset)
Converts the current contents of the stream to string representation by decoding it using the argument charset.
public void
write(int b)
Writes the specified byte to this output stream.
public int
Copies the contets of the argument byte buffer to this output stream.
public void
Writes the bytes contained in the argument byte array to the byte sink.
public void
write(byte[] b)
Writes b.length bytes from the specified byte array to this output stream.
public void
write(byte[] b, int off, int len)
Writes len bytes from the specified byte array starting at offset off to this output stream.
public void
Writes the current contents of the stream to the argument.
public void
writeTo(DataOutput out, int offset, int length)
Writes a region of the current contents of the stream to the argument.
public void
Writes the current contents of the stream to the argument.
public void
writeTo(OutputStream out, int offset, int length)
Writes a region of the current contents of the stream to the argument.
public void
Writes the current contents of the stream to the argument.
public void
writeTo(PrintStream out, int offset, int length)
Writes a region of the current contents of the stream to the argument.
public long
Writes the current contents of the stream to the argument.
public void
writeTo(ByteSink out, int offset, int length)
Writes a region of the current contents of the stream to the argument.
protected byte[] buf
The buffer that holds the bytes.
protected int count
The number of valid data in the buffer.

This means that the region 0..count contains valid bytes.

Creates a new instance with the default buffer size of 128.
Creates a new instance with the argument buffer size.
sizeThe size.
NegativeArraySizeExceptionIf the size is negative.
public UnsyncByteArrayOutputStream(byte[] buf, int count) throws IllegalArgumentException
Creates a new instance that is directly backed by the argument buffer, and currently contains count number of valid bytes.

If more bytes are written to the stream, it may grow accordingly.

bufThe backing buffer.
countThe number of valid bytes in the buffer.
IllegalArgumentExceptionIf the count is negative, or greater than the buffer length.
public void close()
Overridden from: OutputStream
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.

The close method of OutputStream does nothing.

public void ensureCapacity(int mincapacity)
Grows the internal buffer of the stream to be able to hold the specified amount of bytes.
mincapacityThe minimum bytes to be able to hold.
public byte[] getBuffer()
Gets the current backing buffer of the stream.

Any modifications made to the elements of the returned array may be propagated back to the stream. No copy is made.

The buffer.
public int getCapacity()
Gets the current maximum number of bytes the stream can hold.

The capacity of

The capacity.
public int indexOf(byte b)
Gets the first occurrence of a byte value in the stream.
bThe byte to find.
The index of the byte in the stream, or -1 if not found.
public int indexOf(byte b, int offset)
Gets the first occurrence of a byte after a given offset.
bThe byte to find.
offsetThe starting offset of the search. (inclusive)
The index of the byte in the stream, or -1 if not found.
public boolean isEmpty()
Checks if the stream currently holds any bytes.
true if the stream is empty.
public int lastIndexOf(byte b)
Gets the last occurrence of a byte in the stream.
bThe byte to find.
The last index of a byte in the stream, or -1 if not found.
public int lastIndexOf(byte b, int offset) throws IndexOutOfBoundsException
Gets the last occurrence of a byte after a given offset.
bThe byte to find.
offsetThe starting offset of the search. (inclusive)
The last index of a byte in the stream, or -1 if not found.
IndexOutOfBoundsExceptionIf the offset is negative, or greater than size().
Reads bytes from the argument input stream until no more bytes are available.
inThe input stream.
The number of bytes read.
IOExceptionIn case of I/O error.
NullPointerExceptionIf the argument is null.
public int readFrom(InputStream in, int count) throws IOException, NullPointerException
Reads at most the given number of bytes from the input stream.
inThe input stream.
countThe maximum number of bytes to read.
The actually read number of bytes. This is less than count if there were no more available bytes in the input stream.
IOExceptionIn case of I/O error.
NullPointerExceptionIf the input stream is null.
public long readFrom(ByteSource in) throws IOException
Overridden from: ByteSink
Reads bytes from the argument byte source and writes it to this byte sink.

This method will possibly read all bytes from the argument byte source and all the read bytes will be written to this byte sink. If the argument is a blocking source, then this method will block too.

Calling this method instead of copying the bytes externally can have advantages, as implementations can read the bytes into an internal buffer more efficiently, therefore avoiding unnecessary copying and allocations.

The default implementation calls ByteSource.writeTo(ByteSink) of the argument.

inThe byte source to read the input from.
The number of bytes read and written to this sink.
IOExceptionIn case of I/O error.
public int readFrom(ByteSource in, int count) throws IOException, NullPointerException
Reads at most the given number of bytes from the byte source.
inThe byte source.
countThe maximum number of bytes to read.
The actually read number of bytes. This is less than count if there were no more available bytes in the byte source.
IOExceptionIn case of I/O error.
NullPointerExceptionIf the byte source is null.
saker.util 0.8.2
public void reduceSize(int size) throws IllegalArgumentException
Reduces the number of valid bytes in the stream.

This method discards bytes from the end of the valid bytes region in the internal buffer. The array size of the internal buffer is unaffected. Only the number of valid bytes are modified in the stream.

This method can be used to throw away some written bytes from the stream.

sizeThe new number of valid bytes in the stream buffer.
IllegalArgumentExceptionIf the new size is greater than size().
public void repeat(byte b, int count)
Appends the same byte data to the stream multiple times.
bThe byte value to append to the stream.
countThe number of times the byte should be appended.
public void replaceByte(int v, int offset)
Replaces a byte in the stream at the given offset.

Only the lowest 8 bits of the argument value is used.

The capacity and the size of the stream is modified accordingly, so the offset will always point to a valid region. The buffer size and the valid number of bytes will be the maximum of the current size, and offset + 1.

vThe value to set to the byte at the given offset.
offsetThe offset of the byte to modify.
public void reset()
Resets the stream to hold 0 bytes.

Any current bytes in the stream are discarded.

The current data stored in the stream is not overwritten.

public int size()
Gets the current number of valid bytes in the stream.

This method is named size instead of getSize, to be aligned with the ByteArrayOutputStream.size() function naming.

The count of valid bytes.
public byte[] toByteArray()
Gets the currently present data in the stream in a newly allocated array.

The returned array has the same length as the number of valid bytes in the stream.

The data array.
Converts the current data buffer to a ByteArrayRegion.

The returned byte array region is backed by the internal buffer of the stream. Any modifications made to the returned array may (but not necessarily) propagate back to the stream.

If any operations are called on the stream after this method returns, the modifications might not propagate back, as the internal buffer may be reallocated.

The byte array region representing the valid data in the stream.
public String toString()
Converts the current contents of the stream to a string representation by decoding it using UTF-8.
The decoded string.
public String toString(int offset, int length) throws IndexOutOfBoundsException
Converts the specified region of data of the stream to a string representation by decoding it using UTF-8.
offsetThe starting offset index. (inclusive)
lengthThe number of bytes to convert.
The string representation.
IndexOutOfBoundsExceptionIf the offset is negative, or greater than size().
public String toString(int offset, int length, Charset charset) throws IndexOutOfBoundsException
Converts the specified region of data of the stream to a string representation by decoding it using the argument charset.
offsetThe starting offset index. (inclusive)
lengthThe number of bytes to convert.
charsetThe charset to use to decode the raw bytes.
The string representation.
IndexOutOfBoundsExceptionIf the offset is negative, or greater than size().
public String toString(Charset charset)
Converts the current contents of the stream to string representation by decoding it using the argument charset.
charsetThe charset to use to decode the raw bytes.
The string representation.
public void write(int b)
Overridden from: OutputStream
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.

Subclasses of OutputStream must provide an implementation for this method.

bthe byte.
public int write(ByteBuffer buffer) throws NullPointerException
Copies the contets of the argument byte buffer to this output stream.

The number of bytes copied equals to Buffer.remaining().

bufferThe byte buffer.
The number of bytes copied.
NullPointerExceptionIf the buffer is null.
public void write(ByteArrayRegion buf)
Overridden from: ByteSink
Writes the bytes contained in the argument byte array to the byte sink.

This method works similarly to OutputStream.write(byte[], int, int).

bufThe bytes to write.
public void write(byte[] b)
Overridden from: OutputStream
Writes b.length bytes from the specified byte array to this output stream. The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length).
bthe data.
public void write(byte[] b, int off, int len)
Overridden from: OutputStream
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.

The write method of OutputStream calls the write method of one argument on each of the bytes to be written out. Subclasses are encouraged to override this method and provide a more efficient implementation.

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.

bthe data.
offthe start offset in the data.
lenthe number of bytes to write.
public void writeTo(DataOutput out) throws IOException
Writes the current contents of the stream to the argument.
outThe output to write the contents to.
IOExceptionIn case of I/O error.
public void writeTo(DataOutput out, int offset, int length) throws IOException, IndexOutOfBoundsException
Writes a region of the current contents of the stream to the argument.
outThe output to write the contents to.
offsetThe starting offset of the region to write. (inclusive)
lengthThe number of bytes to write to the argument.
IOExceptionIn case of I/O error.
IndexOutOfBoundsExceptionIf offset + len > size() || offset < 0.
public void writeTo(OutputStream out) throws IOException
Writes the current contents of the stream to the argument.
outThe output to write the contents to.
IOExceptionIn case of I/O error.
public void writeTo(OutputStream out, int offset, int length) throws IOException, IndexOutOfBoundsException
Writes a region of the current contents of the stream to the argument.
outThe output to write the contents to.
offsetThe starting offset of the region to write. (inclusive)
lengthThe number of bytes to write to the argument.
IOExceptionIn case of I/O error.
IndexOutOfBoundsExceptionIf offset + len > size() || offset < 0.
public void writeTo(PrintStream out)
Writes the current contents of the stream to the argument.
outThe output to write the contents to.
public void writeTo(PrintStream out, int offset, int length) throws IndexOutOfBoundsException
Writes a region of the current contents of the stream to the argument.
outThe output to write the contents to.
offsetThe starting offset of the region to write. (inclusive)
lengthThe number of bytes to write to the argument.
IndexOutOfBoundsExceptionIf offset + len > size() || offset < 0.
public long writeTo(ByteSink out) throws IOException
Writes the current contents of the stream to the argument.

This method returns the number of bytes written to the stream to have the method signature be compatible with ByteSource.writeTo(ByteSink).

outThe output to write the contents to.
The number of bytes written to the argument. This is the same as size().
IOExceptionIn case of I/O error.
public void writeTo(ByteSink out, int offset, int length) throws IOException, IndexOutOfBoundsException
Writes a region of the current contents of the stream to the argument.
outThe output to write the contents to.
offsetThe starting offset of the region to write. (inclusive)
lengthThe number of bytes to write to the argument.
IOExceptionIn case of I/O error.
IndexOutOfBoundsExceptionIf offset + len > size() || offset < 0.