Package java.io
Class ByteArrayOutputStream
java.lang.Object
java.io.OutputStream
java.io.ByteArrayOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
public class ByteArrayOutputStream extends OutputStream
A specialized
OutputStream for class for writing content to an
(internal) byte array. As bytes are written to this stream, the byte array
may be expanded to hold more bytes. When the writing is considered to be
finished, a copy of the byte array can be requested from the class.- See Also:
ByteArrayInputStream
-
Field Summary
-
Constructor Summary
Constructors Constructor Description ByteArrayOutputStream()Constructs a new ByteArrayOutputStream with a default size of 32 bytes.ByteArrayOutputStream(int size)Constructs a newByteArrayOutputStreamwith a default size ofsizebytes. -
Method Summary
Modifier and Type Method Description voidclose()Closes this stream.voidreset()Resets this stream to the beginning of the underlying byte array.intsize()Returns the total number of bytes written to this stream so far.byte[]toByteArray()Returns the contents of this ByteArrayOutputStream as a byte array.StringtoString()Returns the contents of this ByteArrayOutputStream as a string.StringtoString(int hibyte)Deprecated.StringtoString(String charsetName)Returns the contents of this ByteArrayOutputStream as a string converted according to the encoding declared incharsetName.voidwrite(byte[] buffer, int offset, int len)Writescountbytes from the byte arraybufferstarting at offsetindexto this stream.voidwrite(int oneByte)Writes the specified byteoneByteto the OutputStream.voidwriteTo(OutputStream out)Takes the contents of this stream and writes it to the output streamout.Methods inherited from class java.io.OutputStream
flush, write
-
Field Details
-
buf
protected byte[] bufThe byte array containing the bytes written. -
count
protected int countThe number of bytes written.
-
-
Constructor Details
-
ByteArrayOutputStream
public ByteArrayOutputStream()Constructs a new ByteArrayOutputStream with a default size of 32 bytes. If more than 32 bytes are written to this instance, the underlying byte array will expand. -
ByteArrayOutputStream
public ByteArrayOutputStream(int size)Constructs a newByteArrayOutputStreamwith a default size ofsizebytes. If more thansizebytes are written to this instance, the underlying byte array will expand.- Parameters:
size- initial size for the underlying byte array, must be non-negative.- Throws:
IllegalArgumentException- ifsize< 0.
-
-
Method Details
-
close
Closes this stream. This releases system resources used for this stream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException- if an error occurs while attempting to close this stream.
-
reset
public void reset()Resets this stream to the beginning of the underlying byte array. All subsequent writes will overwrite any bytes previously stored in this stream. -
size
public int size()Returns the total number of bytes written to this stream so far.- Returns:
- the number of bytes written to this stream.
-
toByteArray
public byte[] toByteArray()Returns the contents of this ByteArrayOutputStream as a byte array. Any changes made to the receiver after returning will not be reflected in the byte array returned to the caller.- Returns:
- this stream's current contents as a byte array.
-
toString
Returns the contents of this ByteArrayOutputStream as a string. Any changes made to the receiver after returning will not be reflected in the string returned to the caller. -
toString
Deprecated.UsetoString()instead.Returns the contents of this ByteArrayOutputStream as a string. Each bytebin this stream is converted to a charactercusing the following function:c == (char)(((hibyte & 0xff) << 8) | (b & 0xff)). This method is deprecated and eithertoString()ortoString(String)should be used.- Parameters:
hibyte- the high byte of each resulting Unicode character.- Returns:
- this stream's current contents as a string with the high byte set
to
hibyte.
-
toString
Returns the contents of this ByteArrayOutputStream as a string converted according to the encoding declared incharsetName.- Parameters:
charsetName- a string representing the encoding to use when translating this stream to a string.- Returns:
- this stream's current contents as an encoded string.
- Throws:
UnsupportedEncodingException- if the provided encoding is not supported.
-
write
public void write(byte[] buffer, int offset, int len)Writescountbytes from the byte arraybufferstarting at offsetindexto this stream.- Overrides:
writein classOutputStream- Parameters:
buffer- the buffer to be written.offset- the initial position inbufferto retrieve bytes.len- the number of bytes ofbufferto write.- Throws:
NullPointerException- ifbufferisnull.IndexOutOfBoundsException- ifoffset < 0orlen < 0, or ifoffset + lenis greater than the length ofbuffer.
-
write
public void write(int oneByte)Writes the specified byteoneByteto the OutputStream. Only the low order byte ofoneByteis written.- Specified by:
writein classOutputStream- Parameters:
oneByte- the byte to be written.
-
writeTo
Takes the contents of this stream and writes it to the output streamout.- Parameters:
out- an OutputStream on which to write the contents of this stream.- Throws:
IOException- if an error occurs while writing toout.
-
toString()instead.