With JDK/11, you can make use of the writeBytes(byte b[])
API which eventually calls the write(b, 0, b.length)
as suggested in the answer by Josh.
/**
* Writes the complete contents of the specified byte array
* to this {@code ByteArrayOutputStream}.
*
* @apiNote
* This method is equivalent to {@link #write(byte[],int,int)
* write(b, 0, b.length)}.
*
* @param b the data.
* @throws NullPointerException if {@code b} is {@code null}.
* @since 11
*/
public void writeBytes(byte b[]) {
write(b, 0, b.length);
}
The sample code would simply transform into --
byte[] bytes = new byte[100];
ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length);
baos.writeBytes(bytes);