Class UByteArrayOutputStream
java.lang.Object
java.io.OutputStream
cl.netswitch.lib.util.UByteArrayOutputStream
- All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
Unsynchronized
OutputStream in which data is written to a byte array.
The buffer automatically grows as data is written to it. The data can be
retrieved using toByteArray() and toString().
Closing a UByteArrayOutputStream has no effect. The methods in this
class can be called after the stream has been closed without generating an
IOException.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a newUByteArrayOutputStream.UByteArrayOutputStream(int size) Creates a newUByteArrayOutputStream, with a buffer capacity of the specified size, in bytes. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closing aUByteArrayOutputStreamhas no effect.byte[]Returns the backing byte array of thisUByteArrayOutputStream.voidreset()Resets thecountfield of thisUByteArrayOutputStreamto zero, so that all currently accumulated output in the output stream is discarded.intsize()Returns the current size of the buffer of thisUByteArrayOutputStream.byte[]Creates a newly allocated byte array.toString()Converts the buffer's contents into a string decoding bytes using the platform's default character set.Converts the buffer's contents into a string by decoding the bytes using the specifiedcharset.voidwrite(byte[] bytes, int offset, int length) Writeslengthbytes from the specified byte array starting at offsetoffsetto thisUByteArrayOutputStream.voidwrite(int value) Writes the specified byte to thisUByteArrayOutputStream.voidwriteBytes(byte[] bytes) Writes the complete contents of the specified byte array to thisUByteArrayOutputStream.voidwriteTo(OutputStream output) Writes the complete contents of thisUByteArrayOutputStreamto the specified output stream argument, as if by calling the output stream's write method usingoutput.write(buffer, 0, count).Methods inherited from class OutputStream
flush, nullOutputStream, write
-
Constructor Details
-
UByteArrayOutputStream
public UByteArrayOutputStream()Creates a newUByteArrayOutputStream. The buffer capacity is initially 32 bytes, though its size increases if necessary. -
UByteArrayOutputStream
public UByteArrayOutputStream(int size) Creates a newUByteArrayOutputStream, with a buffer capacity of the specified size, in bytes.- Parameters:
size- the initial size.- Throws:
IllegalArgumentException- if size is negative.
-
-
Method Details
-
write
public void write(int value) Writes the specified byte to thisUByteArrayOutputStream.- Specified by:
writein classOutputStream- Parameters:
value- the byte to be written.
-
write
public void write(byte[] bytes, int offset, int length) Writeslengthbytes from the specified byte array starting at offsetoffsetto thisUByteArrayOutputStream.- Overrides:
writein classOutputStream- Parameters:
bytes- the data.offset- the start offset in the data.length- the number of bytes to write.- Throws:
NullPointerException- ifbytesisnull.IndexOutOfBoundsException- ifoffsetis negative,lengthis negative, orlengthis greater thanbytes.length - offset.
-
toString
Converts the buffer's contents into a string decoding bytes using the platform's default character set. The length of the newStringis a function of the character set, and hence may not be equal to the size of the buffer.This method always replaces malformed-input and unmappable-character sequences with the default replacement string for the platform's default character set. The CharsetDecoder class should be used when more control over the decoding process is required.
-
close
Closing aUByteArrayOutputStreamhas no effect. The methods in this class can be called after the stream has been closed without generating anIOException.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException
-
reset
public void reset()Resets thecountfield of thisUByteArrayOutputStreamto zero, so that all currently accumulated output in the output stream is discarded. The output stream can be used again, reusing the already allocated buffer space. -
size
public int size()Returns the current size of the buffer of thisUByteArrayOutputStream.- Returns:
- the value of the
countfield, which is the number of valid bytes in this output stream.
-
getByteArray
public byte[] getByteArray()Returns the backing byte array of thisUByteArrayOutputStream.- Returns:
- the backing byte array of this
UByteArrayOutputStream.
-
toByteArray
public byte[] toByteArray()Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.- Returns:
- the current contents of this output stream, as a byte array.
-
writeBytes
public void writeBytes(byte[] bytes) Writes the complete contents of the specified byte array to thisUByteArrayOutputStream.- Parameters:
bytes- the data.- Throws:
NullPointerException- ifbytesisnull.
-
writeTo
Writes the complete contents of thisUByteArrayOutputStreamto the specified output stream argument, as if by calling the output stream's write method usingoutput.write(buffer, 0, count).- Parameters:
output- the output stream to which to write the data.- Throws:
NullPointerException- ifoutputisnull.IOException- if an I/O error occurs.
-
toString
Converts the buffer's contents into a string by decoding the bytes using the specifiedcharset. The length of the newStringis a function of the charset, and hence may not be equal to the length of the byte array.This method always replaces malformed-input and unmappable-character sequences with the charset's default replacement string. The
CharsetDecoderclass should be used when more control over the decoding process is required.- Parameters:
charset- the charset to be used to decode thebytes- Returns:
- String decoded from the buffer's contents.
-