Advertisement



< Prev
Next >



ByteArrayOutputStream



ByteArrayOutputStream class is a subclass of OutputStream abstract class. It is used to write content of a byte array to the output stream by performing two steps in sequence -




Constructor :


ByteArrayOutputStream()
This constructor creates a ByteArrayOutputStream object for writing out the contents of a byte array. Example-:
String str = "Learning about Input Streams";
byte[] b= str.getBytes();
ByteArrayOutputStream bais= new ByteArrayOutputStream();
bais.write(b);
We have initialized a byte array, b, to the value of a String str. This byte array is written to the internal buffer of ByteArrayOutputSteam. Data of internal buffer will be written out to an output stream.




Methods for writing bytes using ByteArrayOutputStream


Some methods that we generally use while working with ByteArrayOutputStream are shown in the table below :

Methods Description
flush()
This method flushes any buffered data to be written out of this output stream.
size()
This method returns the current size of the buffer of ByteArrayOutputStream.

write(int b)
This method writes one byte at a time to this output stream.

write(byte[])
This method writes a whole byte array at a time to this input stream.

writeTo(OutputStream out)
This method writes the content of ByteArrayOutputStream to the output stream passed to it in the parameters.

close()
This method closes this output stream and also frees any resources connected with this output stream.




Advertisement




Writing one byte at a time using write(int b) method of ByteArrayInputStream


ByteArrayOutputStream writes the content of a byte array to its own internel buffer. Next, all the bytes in the internal buffer are written to a file using an output stream.
// A program to write a byte at a time using ByteArrayOutputStream. 

import java.io.*;

class A
{
public static void main(String... ar)
{
try
{
String str= new String("HelloJava");
byte b[]=str.getBytes();

FileOutputStream fos=new FileOutputStream("D:/TextBook.txt");
ByteArrayOutputStream baos= new ByteArrayOutputStream();

//Initial buffer size of ByteArrayOutputStream is 0
System.out.println("Initial Size of the buffer"+ baos.size()); 


//Writing one byte at a time to ByteArrayOutputStream's buffer
for(byte byt : b) 
{
baos.write(byt);
}

System.out.println("Content written to ByteArrayOutputStream's buffer = "+baos.toString());
System.out.println("Size of the buffer after writing data to it = "+ baos.size()); 

//writing out the contents of BufferArrayOutputStream's buffer to a file.
baos.writeTo(fos); 

//flusing all the bytes out of output stream
baos.flush();
fos.flush();

fos.close();
}
catch(IOException e)
{
System.out.println(e);
}

}
}


Output is -:


Initial Size of the buffer = 0
Content written to ByteArrayOutputStream's buffer = HelloJava
Size of the buffer after writing data to it = 9



Program Analysis






Note


Closing the output stream of ByteArrayOutputStream has no effect, we can still add more data to the buffer.



Please share this article -





< Prev
Next >
< ByteArrayInputStream
BufferedInputStream >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement