Java.io - SPLessons

Java.io ByteArrayOutputStream

Chapter 7

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io ByteArrayOutputStream

Java.io ByteArrayOutputStream

shape Introduction

Generally the output stream acts as a destination to the data. The Java.io ByteArrayOutputStream is the class used to write the bytes in the form of an array to stream. Through the byte array output stream the stream is enclose to change into a array bytes.

shape Description

In case of Java.io ByteArrayOutputStream the methods that are called after closing the stream only when no I/O exceptions occurred. The byte array output stream is subclass to the class called output stream.

Class Declaration

shape Declaration

Java.io ByteArrayOutputStream class is declared below:
Public class ByteArrayOutputStream extends OutputStream

shape Fields

Java.io ByteArrayOutputStream class fields are:

  • Protected int Count : The field represents to the index to count input stream buffer after the last character.
  • Protected byte [] bu : The field represents to the internal buffer where actually the data  stored.

Class Constructors

shape Table

Constructor Description
ByteArrayOutputStream () The function of this constructor is to create the byte array output stream.
ByteAyyayInputStream ( int size) The function of this constructor is to create the byte array output stream with represented size of buffer.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to input stream systems resources are released without any impact.
void write (int b) The function of this method is to write the represented byte into output stream.
void write (byte[] b, int off, int n) By using this method it writes the data up to ‘n’ bytes from the output stream to an array of bytes.
void writeTo(Output stream out) The function of this method is to write the entire data from this stream to the specified output stream.

Inherited Methods

shape Description

From the following classes, methods are inherited to the byte array output stream class.

  • Java.io.object
  • Java.io.OutputStream

shape Examples

Usage of void close() method.

import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class ByteArrayOutputStream 
{
   public static void main(String[] args) throws IOException 
   {
      
      byte[] buf = {65, 66, 67, 68};
      ByteArrayOutputStream baos = null;
      
      try{
         // creating a new Byte array output stream as below.
         baos = new ByteArrayOutputStream();
         
         // close method is invoked before baos.
         baos.close();
         
         // To write byte array to the output stream
         baos.write(buf);
         
         // print as in the form string
         System.out.print(baos.toString());
         
      }catch(Exception e){

         // if I/O error occurs
         e.printStackTrace();
      }finally{
         if(baos!=null)
            baos.close();
      }   
   }
}

Output
Following is the result will be displayed.

ABCD

Usage of void writeTo (Outputstream out) method.

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class ByteArrayOutputStream 
{
   public static void main(String[] args) throws IOException 
   {                  
      byte[] bs = {83, 80, 76, 69, 83, 83, 79, 78, 83};
      OutputStream os = null;
      ByteArrayOutputStream baos = null;      
      try{
         // creating a new output stream
         os = new ByteArrayOutputStream();         
         // creating a new ByteArrayOutputStream
         baos = new ByteArrayOutputStream();      
         // writing the buffer to the byte array output stream
         baos.write(bs);         
         // To write the output stream with baos
         baos.writeTo(os);         
         // print the byte as default character set
         System.out.println(os.toString());       
      }catch(Exception e)
      {
         // If any I/O error occurs
         e.printStackTrace();
      }finally
      {
         if(baos!=null)
            baos.close();
         if(os!=null)
            os.close();
      }   
   }
}

Output
Following is the result will be displayed.

SPLESSONS

Summary

shape Key Points

  • Generally the output stream acts as a destination to the data.
  • The byte array output stream is the class used to write the bytes in the form of an array to stream. Through byte array output stream the stream is enclose to change into a array bytes.
  • In case of byte array output stream the methods that are called after closing the stream only when no I/O exceptions occurred.