Java Programming Handbook

    Java I/O Streams: ByteArrayInputStream and ByteArrayOutputStream

    In this blog, we will explore ByteArrayInputStream and ByteArrayOutputStream, two memory-based I/O stream classes in Java. These classes are useful when working with byte arrays instead of files or external data sources.

    1. What Are ByteArrayInputStream and ByteArrayOutputStream?#

    • ByteArrayInputStream allows an application to read data from a byte array as an input stream.
    • ByteArrayOutputStream allows writing data to a byte array, which grows automatically.

    These streams are often used in testing, conversions, or working with binary data in memory without accessing the filesystem.

    2. ByteArrayInputStream Methods (With Examples)#

    int read()#

    Reads the next byte of data from the input stream.

    import java.io.*; public class ByteArrayInputStreamExample { public static void main(String[] args) { byte[] data = {65, 66, 67}; // A, B, C try (ByteArrayInputStream bais = new ByteArrayInputStream(data)) { int byteData; while ((byteData = bais.read()) != -1) { System.out.print((char) byteData); } } } }

    Output:#

    ABC

    int read(byte[] b, int off, int len)#

    Reads up to len bytes into b starting from offset off.

    import java.io.*; public class ByteArrayInputStreamReadArray { public static void main(String[] args) { byte[] input = "Hello World".getBytes(); byte[] buffer = new byte[5]; try (ByteArrayInputStream bais = new ByteArrayInputStream(input)) { int bytesRead = bais.read(buffer, 0, buffer.length); System.out.println(new String(buffer, 0, bytesRead)); } } }

    Output:#

    Hello

    int available()#

    Returns the number of remaining bytes that can be read.

    import java.io.*; public class ByteArrayInputStreamAvailable { public static void main(String[] args) { byte[] data = "Test".getBytes(); try (ByteArrayInputStream bais = new ByteArrayInputStream(data)) { System.out.println("Bytes available: " + bais.available()); } } }

    Output:#

    Bytes available: 4

    void reset()#

    Resets the stream to the beginning.

    import java.io.*; public class ByteArrayInputStreamReset { public static void main(String[] args) { byte[] data = "ABCD".getBytes(); try (ByteArrayInputStream bais = new ByteArrayInputStream(data)) { System.out.print((char) bais.read()); // A System.out.print((char) bais.read()); // B bais.reset(); System.out.print((char) bais.read()); // A again } } }

    Output:#

    ABA

    3. ByteArrayOutputStream Methods (With Examples)#

    void write(int b)#

    Writes a single byte to the output stream.

    import java.io.*; public class ByteArrayOutputStreamWriteSingle { public static void main(String[] args) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { baos.write(65); // 'A' System.out.println(baos.toString()); } } }

    Output:#

    A

    void write(byte[] b)#

    Writes a byte array to the stream.

    import java.io.*; public class ByteArrayOutputStreamWriteArray { public static void main(String[] args) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { String msg = "Hello Java"; baos.write(msg.getBytes()); System.out.println(baos.toString()); } } }

    Output:#

    Hello Java

    byte[] toByteArray()#

    Returns the current contents as a byte array.

    import java.io.*; public class ByteArrayOutputStreamToByteArray { public static void main(String[] args) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { baos.write("Data".getBytes()); byte[] result = baos.toByteArray(); for (byte b : result) { System.out.print((char) b); } } } }

    Output:#

    Data

    void reset()#

    Clears the current buffer.

    import java.io.*; public class ByteArrayOutputStreamReset { public static void main(String[] args) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { baos.write("Before Reset".getBytes()); baos.reset(); baos.write("After Reset".getBytes()); System.out.println(baos.toString()); } } }

    Output:#

    After Reset

    4. When to Use ByteArrayInputStream and ByteArrayOutputStream?#

    • Use ByteArrayInputStream when reading from an in-memory byte array instead of from an external file.
    • Use ByteArrayOutputStream when you want to collect bytes in memory and optionally convert them to a byte array or string.
    • These are great for data conversion, unit testing, and mocking file I/O in memory.

    Conclusion#

    In this blog, we covered ByteArrayInputStream and ByteArrayOutputStream, their commonly used methods, and practical examples with outputs. These memory-based streams are powerful tools for manipulating byte data directly in RAM, making them very useful in a variety of situations such as testing or data transformation.

    In the next blog, we'll cover ObjectInputStream and ObjectOutputStream, which are used to serialize and deserialize Java objects.

    Last updated on Apr 09, 2025