Home » Java programming language

Java ByteArrayOutputStream size() Method with Example

ByteArrayOutputStream Class size() method: Here, we are going to learn about the size() method of ByteArrayOutputStream Class with its syntax and example.
Submitted by Preeti Jain, on March 28, 2020

ByteArrayOutputStream Class size() method

  • size() method is available in java.io package.
  • size() method is used to return the current size of the buffer exists.
  • size() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • size() method does not throw an exception at the time of returning the size.

Syntax:

    public int size();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is int, it returns the current size of the buffer exists in this stream.

Example:

// Java program to demonstrate the example 
// of int size() method of ByteArrayInputStream

import java.io.*;

public class SizeOfBAOS {
 public static void main(String[] args) throws Exception {
  byte[] b_arr = {
   97,
   98,
   99,
   100
  };
  ByteArrayOutputStream BAOS = null;

  try {
   // Instantiates ByteArrayOutputStream
   BAOS = new ByteArrayOutputStream();

   // By using write() method is to
   // write b_arr to the BAOS
   BAOS.write(b_arr);

   // By using toString() method is 
   // to represent the BAOS as a string
   System.out.println("BAOS.toString(): " + BAOS.toString());

   // By using size() method is
   // to return the size of the
   // stream
   int s_size = BAOS.size();
   System.out.println("BAOS.size(): " + s_size);

  } catch (Exception ex) {
   System.out.println(ex.toString());

  } finally {
   if (BAOS != null)
    BAOS.close();
  }
 }
}

Output

BAOS.toString(): abcd
BAOS.size(): 4



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.