Like this:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream w = new DataOutputStream(baos);

w.writeInt(100);
w.write(byteArray);

w.flush();

byte[] result = baos.toByteArray();

Actually your second version will not work at all. DataOutputStream requires an actual target stream in which to write the data. You can't do new DataOutputStream(). There isn't actually any constructor like that.

Answer from Mihai Toader on Stack Overflow
🌐
Jenkov
jenkov.com › tutorials › java-io › bytearrayoutputstream.html
Java ByteArrayOutputStream
November 15, 2019 - This tutorial explains how to use the ByteArrayOutputStream in Java IO to write data to an OutputStream and capture that data in a byte array.
🌐
Programiz
programiz.com › java-programming › bytearrayoutputstream
Java ByteArrayOutputStream (With Examples)
In the above example, we have created an array of bytes to store the data returned by the toByteArray() method. We then have used the for loop to access each byte from the array. Here, each byte is converted into the corresponding character using typecasting.
🌐
Tutorialspoint
tutorialspoint.com › java › java_bytearrayoutputstream.htm
Java - ByteArrayOutputStream
import java.io.*; public class ByteStreamTest { public static void main(String args[])throws IOException { ByteArrayOutputStream bOutput = new ByteArrayOutputStream(12); while( bOutput.size()!= 10 ) { // Gets the inputs from the user bOutput.write("hello".getBytes()); } byte b [] = bOutput.toByteArray(); System.out.println("Print the content"); for(int x = 0; x < b.length; x++) { // printing the characters System.out.print((char)b[x] + " "); } System.out.println(" "); int c; ByteArrayInputStream bInput = new ByteArrayInputStream(b); System.out.println("Converting characters to Upper case " ); for(int y = 0 ; y < 1; y++ ) { while(( c = bInput.read())!= -1) { System.out.println(Character.toUpperCase((char)c)); } bInput.reset(); } } }
🌐
TechVidvan
techvidvan.com › tutorials › java-bytearrayoutputstream-class
Java ByteArrayOutputStream Class with Examples - TechVidvan
May 2, 2024 - An array of output data (in bytes) can be written using the Java.io package’s ByteArrayOutputStream class.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › ByteArrayOutputStream.html
ByteArrayOutputStream (Java Platform SE 8 )
March 16, 2026 - Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
🌐
Apache Commons
commons.apache.org › proper › commons-io › javadocs › api-2.5 › org › apache › commons › io › output › ByteArrayOutputStream.html
ByteArrayOutputStream (Apache Commons IO 2.5 API)
This is an alternative implementation of the ByteArrayOutputStream class. The original implementation only allocates 32 bytes at the beginning. As this class is designed for heavy duty it starts at 1024 bytes. In contrast to the original it doesn't reallocate the whole memory block but allocates ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › io-bytearrayoutputstream-class-java
Java.io.ByteArrayOutputStream() Class in Java - GeeksforGeeks
July 23, 2025 - ByteArrayOutputStream(int buffersize) : creates a new ByteArrayOutputStream with buffersize to write bytes.
🌐
ZetCode
zetcode.com › java › io-bytearrayoutputstream
Java ByteArrayOutputStream Class - Complete Tutorial with Examples
This example demonstrates using the reset method to clear buffer content. After reset, the buffer appears empty but retains its capacity. This is useful when you need to reuse the stream for new data without allocating new memory. The underlying byte array isn't shrunk by reset. ByteArrayOutputStream provides toString methods to convert buffer content to strings.
🌐
Apache Commons
commons.apache.org › proper › commons-io › apidocs › org › apache › commons › io › output › ByteArrayOutputStream.html
ByteArrayOutputStream (Apache Commons IO 2.21.0 API)
public ByteArrayOutputStream() Constructs a new byte array output stream. The buffer capacity is initially 1024 bytes, though its size increases if necessary. public ByteArrayOutputStream · (int size) Constructs a new byte array output stream, with a buffer capacity of the specified size, in bytes.
🌐
Baeldung
baeldung.com › home › java › java io › guide to java outputstream
Guide to Java OutputStream | Baeldung
December 3, 2025 - Some of the examples of FilterOutputStream are BufferedOutputStream, CheckedOutputStream, CipherOutputStream, DataOutputStream, DeflaterOutputStream, DigestOutputStream, InflaterOutputStream, PrintStream.
🌐
Android Developers
developer.android.com › api reference › bytearrayoutputstream
ByteArrayOutputStream | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
GitHub
github.com › openjdk-mirror › jdk7u-jdk › blob › master › src › share › classes › java › io › ByteArrayOutputStream.java
jdk7u-jdk/src/share/classes/java/io/ByteArrayOutputStream.java at master · openjdk-mirror/jdk7u-jdk
public ByteArrayOutputStream() { this(32); } · /** * Creates a new byte array output stream, with a buffer capacity of · * the specified size, in bytes.
Author   openjdk-mirror
🌐
Javatpoint
javatpoint.com › java-bytearrayoutputstream-class
Java ByteArrayOutputStream Class - javatpoint
Java ByteArrayOutputStream Class for beginners and professionals with examples on Java IO or Input Output in Java with input stream, output stream, reader and writer class. The java.io package provides api to reading and writing data.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java tutorial › java bytearrayoutputstream
Java ByteArrayOutputStream | Examples of Java ByteArrayOutputStream
April 10, 2023 - void close(): ByteArrayOutputStream will be closed on calling this method. Now, let us see some of the sample examples.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › io › bytearrayoutputstream
Java ByteArrayOutputStream Example - Java Code Geeks
September 16, 2014 - In this example I created an instance of ByteArrayOutputStream and wrote 10 random bytes into it.
🌐
Tutorialspoint
tutorialspoint.com › java › io › java_io_bytearrayoutputstream.htm
Java - ByteArrayOutputStream Class
A ByteArrayOutputStream is created. The first string, "Hello, World!", is written to the stream and printed. The reset() method is called, which clears the buffer and makes the stream empty again.
🌐
Coding Shuttle
codingshuttle.com › home › handbooks › java programming handbook › bytearrayinputstream and bytearrayoutputstream
ByteArrayInputStream and ByteArrayOutputStream | Coding Shuttle
April 9, 2025 - This blog explains Java's ByteArrayInputStream and ByteArrayOutputStream classes with detailed examples, outputs, and key methods. It highlights how to handle byte data in memory for tasks like testing, data conversion, and stream manipulation.