ByteArrayOutputStream writes bytes to a byte array in memory. Not to any other destination, such as a file or a network socket. After writing the data, you can get the byte array by calling toByteArray() on it.

BufferedOutputStream wraps another, underlying OutputStream and provides buffering for that underlying stream, to make I/O operations more efficient. The underlying stream can be any kind of OutputStream, for example one that writes to a file or a network socket.

Why you might want to use buffering: Writing a large block of data to the file system is more efficient than writing byte by byte. If your program needs to write many small pieces of data, it's more efficient to first gather these small pieces in a buffer and then write the entire buffer to disk at once. This is what BufferedOutputStream does automatically for you.

Answer from Jesper on Stack Overflow
🌐
Apache Commons
commons.apache.org › proper › commons-io › javadocs › api-2.4 › src-html › org › apache › commons › io › output › ByteArrayOutputStream.html
ByteArrayOutputStream - Apache Commons
039 * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in · 040 * this class can be called after the stream has been closed without · 041 * generating an <tt>IOException</tt>. 042 * <p> 043 * This is an alternative implementation of the {@link java.io.ByteArrayOutputStream} 044 * class.
Discussions

java - Difference between ByteArrayOutputStream and BufferedOutputStream - Stack Overflow
Both ByteArrayOutputStream and BufferedOutputStream do buffering by placing data in an array in memory. So my questions are what are the differences between these two. When to use More on stackoverflow.com
🌐 stackoverflow.com
FileOutputStream v. ByteArrayOutputStream: is there a noticeable difference in memory usage?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
10
8
December 20, 2022
java - How to fill a ByteArrayOutputStream with the content of a file? - Stack Overflow
I have File and need to fill its content to a ByteArrayOutputStream, can someone help me with that? With Google Guava I am able to create an ByteArray but how do I get an ByteArrayOutputStream from... More on stackoverflow.com
🌐 stackoverflow.com
ByteArrayOutputStream to a File - Drive API, Java
I am going to be blunt: this is for crossover fanfiction between Tron: Legacy and The Amazing Digital Circus · I recognize that I do not actually have to put the effort into learning code to write fanfiction about computer programs because the average fanfiction reader is not a programmer, ... More on reddit.com
🌐 r/AskProgramming
January 23, 2020
🌐
Apache
axis.apache.org › axis › java › apiDocs › org › apache › axis › utils › ByteArrayOutputStream.html
ByteArrayOutputStream (Apache Axis)
This is an alternative implementation of the java.io.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 ...
🌐
SourceForge
ulibgcj.sourceforge.net › javadoc › java › io › ByteArrayOutputStream.html
ByteArrayOutputStream
This method initializes a new ByteArrayOutputStream with the default buffer size of 32 bytes. If a different initial buffer size is desired, see the constructor ByteArrayOutputStream(int size).
🌐
Javadoc.io
javadoc.io › static › org.apache.commons › commons-io › 1.3.2 › org › apache › commons › io › output › ByteArrayOutputStream.html
ByteArrayOutputStream (Commons IO 1.3.2 API)
This is an alternative implementation of the java.io.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 ...
🌐
Jenkov
jenkov.com › tutorials › java-io › bytearrayoutputstream.html
Java ByteArrayOutputStream
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.
Find elsewhere
🌐
Bureau of Economic Geology
beg.utexas.edu › lmod › agi.servlet › doc › detail › java › io › ByteArrayOutputStream.html
: Class ByteArrayOutputStream
public ByteArrayOutputStream() Creates a new byte array output stream. The buffer capacity is initially 32 bytes, though its size increases if necessary. public ByteArrayOutputStream(int size) Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
Top answer
1 of 3
13

ByteArrayOutputStream writes bytes to a byte array in memory. Not to any other destination, such as a file or a network socket. After writing the data, you can get the byte array by calling toByteArray() on it.

BufferedOutputStream wraps another, underlying OutputStream and provides buffering for that underlying stream, to make I/O operations more efficient. The underlying stream can be any kind of OutputStream, for example one that writes to a file or a network socket.

Why you might want to use buffering: Writing a large block of data to the file system is more efficient than writing byte by byte. If your program needs to write many small pieces of data, it's more efficient to first gather these small pieces in a buffer and then write the entire buffer to disk at once. This is what BufferedOutputStream does automatically for you.

2 of 3
11

Just look at the javadoc:

ByteArrayOutputStream:

This class implements an output stream in which the data is written into a byte array.

BufferedOutputStream:

The class implements a buffered output stream. By setting up such an output stream, an application can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written.

So, those are really two very different things:

  • the first one you use when you know that you have some data that in the end you need as array of bytes
  • the second one is just a wrapper around any other kind of output stream - which adds buffering.

That is all there is to this!

And if you want to experience a different behavior: create a buffered one that writes to a file, and an array one. Then just keep pushing bytes into each one. The array one will cause a memory problem at some point, the other one might not stop until all of your disk space is used up.

🌐
Baeldung
baeldung.com › home › java › java io › convert an outputstream to a byte array in java
Convert an OutputStream to a Byte Array in Java | Baeldung
March 3, 2025 - public class DrainableOutputStream extends FilterOutputStream { private final ByteArrayOutputStream buffer; public DrainableOutputStream(OutputStream out) { super(out); this.buffer = new ByteArrayOutputStream(); } @Override public void write(byte b[]) throws IOException { buffer.write(b); super.write(b); } public byte[] toByteArray() { return buffer.toByteArray(); } }
🌐
Tutorialspoint
tutorialspoint.com › java › pdf › java_bytearrayoutputstream.pdf pdf
Java - ByteArrayOutputStream
Below given is the list of the constructors to provided by ByteArrayOutputStream class.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › io › OutputStream.html
OutputStream (Java SE 21 & JDK 21)
January 20, 2026 - ByteArrayOutputStream, FileOutputStream, FilterOutputStream, ObjectOutputStream, PipedOutputStream · public abstract class OutputStream extends Object implements Closeable, Flushable · This abstract class is the superclass of all classes representing an output stream of bytes.
🌐
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 we will discuss about ByteArrayOutputStream class and its usage. This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it.
🌐
Javaplanet
javaplanet.io › home › additional java se packages › bytearrayoutputstream
ByteArrayOutputStream -
September 9, 2025 - ByteArrayOutputStream is a memory-based stream that collects output data in an internal byte array. It's useful when you want to build a byte array
🌐
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.
🌐
Reddit
reddit.com › r/javahelp › fileoutputstream v. bytearrayoutputstream: is there a noticeable difference in memory usage?
r/javahelp on Reddit: FileOutputStream v. ByteArrayOutputStream: is there a noticeable difference in memory usage?
December 20, 2022 -

Scenario: I have a REST API endpoint built with Spring Boot. The endpoint is used to dynamically generate an excel file based off input parameters. When the file is done being generated, it returns the file as an InputStreamResource. The main goal is minimum memory usage.

I'm using fastexcel to create the excel file, and I'm flushing it to the OutputStream after every row is written. Right now, I am using a FileOutputStream to write to disk. When the excel file is done being generated, I read it back in using InputStreamResource and stream the response. My thought process is that a ByteArrayOutputStream keeps everything in memory even if I'm flushing the excel file after every row, so I used the FileOutputStream. Does my logic track here? Or am I unnecessarily slowing things down with expensive filesystem IO?

🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › ByteArrayInputStream.html
ByteArrayInputStream (Java Platform SE 8 )
March 16, 2026 - Closing a ByteArrayInputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException · If no mark has been set, then the value of mark is the offset passed to the constructor (or 0 if the offset was not supplied)
🌐
Oracle
docs.oracle.com › javase › 9 › docs › api › java › io › OutputStream.html
OutputStream (Java SE 9 & JDK 9 )
BufferedOutputStream, ByteArrayOutputStream, DataOutputStream, FilterOutputStream, InputStream, write(int)