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
ConstructorDescriptionCreates a newUByteArrayOutputStream
.UByteArrayOutputStream
(int size) Creates a newUByteArrayOutputStream
, with a buffer capacity of the specified size, in bytes. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closing aUByteArrayOutputStream
has no effect.byte[]
Returns the backing byte array of thisUByteArrayOutputStream
.void
reset()
Resets thecount
field of thisUByteArrayOutputStream
to zero, so that all currently accumulated output in the output stream is discarded.int
size()
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
.void
write
(byte[] bytes, int offset, int length) Writeslength
bytes from the specified byte array starting at offsetoffset
to thisUByteArrayOutputStream
.void
write
(int value) Writes the specified byte to thisUByteArrayOutputStream
.void
writeBytes
(byte[] bytes) Writes the complete contents of the specified byte array to thisUByteArrayOutputStream
.void
writeTo
(OutputStream output) Writes the complete contents of thisUByteArrayOutputStream
to the specified output stream argument, as if by calling the output stream's write method usingoutput.write(buffer, 0, count)
.Methods inherited from class java.io.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:
write
in classOutputStream
- Parameters:
value
- the byte to be written.
-
write
public void write(byte[] bytes, int offset, int length) Writeslength
bytes from the specified byte array starting at offsetoffset
to thisUByteArrayOutputStream
.- Overrides:
write
in classOutputStream
- Parameters:
bytes
- the data.offset
- the start offset in the data.length
- the number of bytes to write.- Throws:
NullPointerException
- ifbytes
isnull
.IndexOutOfBoundsException
- ifoffset
is negative,length
is negative, orlength
is 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 newString
is 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 aUByteArrayOutputStream
has no effect. The methods in this class can be called after the stream has been closed without generating anIOException
.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
- Throws:
IOException
-
reset
public void reset()Resets thecount
field of thisUByteArrayOutputStream
to 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
count
field, 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
- ifbytes
isnull
.
-
writeTo
Writes the complete contents of thisUByteArrayOutputStream
to 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
- ifoutput
isnull
.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 newString
is 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
CharsetDecoder
class 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.
-