Java Methods of ByteArrayOutputStream
The Java ByteArrayOutputStream
class is a subclass of the OutputStream
class that allows you to write data to an internal byte array buffer. Some of the commonly used methods of the ByteArrayOutputStream
class are:
write(int b)
: This method writes a byte to the byte array buffer.write(byte[] b, int off, int len)
: This method writes len bytes from the byte array b starting at offset off to the byte array buffer.toByteArray()
: This method returns the contents of the byte array buffer as a byte array.toString(String charsetName)
: This method converts the contents of the byte array buffer to a string using the specified character set.reset()
: This method resets the byte array buffer so that it is empty.size()
: This method returns the current size of the byte array buffer.close()
: This method closes theByteArrayOutputStream
and releases any system resources associated with it.flush()
: This method flushes the byte array buffer, writing any buffered output bytes to the underlying output stream.writeTo(OutputStream out)
: This method writes the entire contents of the byte array buffer to the specified output stream.
Note that ByteArrayOutputStream
extends the OutputStream
class, so it also inherits all the methods of the OutputStream
class.