Topics Index

Input/Output: Exploring > Byte Streams >
Siva Nookala - 15 Apr 2016
ByteArrayOutputStream implements OutputStream. ByteArrayOutputStream contains a buffer in which data is stored in byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() and toString(). Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

public class ByteArrayOutputStream
extends OutputStream
Constructors :
Constructor Description
ByteArrayOutputStream() Creates a new byte array output stream.
ByteArrayOutputStream(int size) Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
Important Methods :
Method Description
public void write(int w) Writes the specified byte to the byte array output stream.
public byte[] toByteArray() Creates a newly allocated byte array.
public String toString() Converts the buffer's contents into a string decoding bytes using the platform's default character set.
public void reset() Resets the count field of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded.

class ByteArrayOutputStreamDemo
    public static void main(String arg[])
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); // LINE A
        String lowerCase = "merit campus";
        byte b[] = lowerCase.getBytes();
        baos.write(b); // LINE B
        System.out.println("Converting to String : ");
        // using toString
        String upperCase = baos.toString().toUpperCase(); // LINE C
        System.out.println("Converting to byte array : ");
        // using toByteArray
        byte b1[] = baos.toByteArray(); // LINE D
        for(int i = 0; i < b1.length; i++)
            System.out.print((char) b1[i]);
        baos.close(); // LINE E
        baos.reset(); // LINE F
        String afterReset = baos.toString();
        System.out.println("After Reset : " + afterReset);    


At LINE A we created a ByteArrayOutputStream object.
At LINE B we are writing the bytes of array b into baos.
At LINE C we are converting the byte array inside baos to string and then to uppercase.
At LINE D we are now changing the stream back to byte array.
At LINE E we are closing the stream which doesn't show any effect.
At LINE F we are clearing the buffer using reset method. After reset there will be no bytes in stream.

  • Take a String I LOVE JAVA convert it to byte array and write it to ByteArrayOutputStream.
  • Now convert it back to string.
Reset of Methods :
Method Description
public void write(byte []b, int of, int len) Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
public void writeTo(OutputStream outSt) Writes the entire content of this Stream to the specified stream argument.

Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.