Java IO: ByteArrayOutputStream
ByteArrayOutputStream class of the Java IO API allows you to capture data written to a stream in an array.
Here is a simple example:
ByteArrayOutputStream output = new ByteArrayOutputStream(); //write data to output stream byte bytes = output.toByteArray();
ByteArrayOutputStream can be handy in situations where you have a component that outputs its data
OutputStream, but where you need it as a byte array.
Note: The proper exception handling has been skipped here for the sake of clarity. To learn more about correct exception handling, go to Java IO Exception Handling.