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.
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.
Videos
02:13
ByteArrayOutputStream in Java: Resetting with reset() Method | ...
28 - Ecrire les octets dans un ByteArrayOutputStream
Buffered Input Output |SequenceInputStream in java ...
04:17
How to write to ByteArrayOutputStream? - YouTube
04:08
Java ByteArrayOutputStream in English | Java Tutorials in English ...
01:42
Java ByteArrayOutputStream: How to Use toByteArray() Method | Java ...
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(); } } }
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › io › ByteArrayOutputStream.html
ByteArrayOutputStream (Java SE 17 & JDK 17)
October 20, 2025 - Creates a new ByteArrayOutputStream, with a buffer capacity of the specified size, in bytes.
Top answer 1 of 6
42
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.
2 of 6
3
Could you make a variable to hold on to the ByteArrayOutputStream and pass it into the DataOutputStream.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(1);
byte[] result = dos.toByteArray();
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 ...
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › io › ByteArrayOutputStream.html
ByteArrayOutputStream (Java SE 11 & JDK 11 )
January 20, 2026 - Creates a new ByteArrayOutputStream, with a buffer capacity of the specified size, in 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.
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.
Call +917738666252
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.