Java Methods of ByteArrayOutputStream

www.i‮ditfig‬ea.com

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:

  1. write(int b): This method writes a byte to the byte array buffer.

  2. 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.

  3. toByteArray(): This method returns the contents of the byte array buffer as a byte array.

  4. toString(String charsetName): This method converts the contents of the byte array buffer to a string using the specified character set.

  5. reset(): This method resets the byte array buffer so that it is empty.

  6. size(): This method returns the current size of the byte array buffer.

  7. close(): This method closes the ByteArrayOutputStream and releases any system resources associated with it.

  8. flush(): This method flushes the byte array buffer, writing any buffered output bytes to the underlying output stream.

  9. 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.