The size() method of ByteArrayOutputStream class in Java is used to obtain the current size of the buffer. This buffer is accumulated inside the ByteArrayOutputStream. This method returns the size of the current buffer as an integer type.
Syntax:
public int size()
Parameters: This method does not accept any parameter.
Return value: This method returns the size of the current buffer as an integer.
Exceptions: This method does not throw any exception.
Below programs illustrate size() method in ByteArrayOutputStream class in IO package:
Program 1:
import java.io.*;
public class GFG {
public static void main(String[] args)
throws Exception
{
ByteArrayOutputStream byteArrayOutStr
= new ByteArrayOutputStream();
byte [] buf = { 71 , 69 , 69 , 75 , 83 };
for ( byte b : buf) {
byteArrayOutStr.write(b);
System.out.println(
byteArrayOutStr.toString() + " "
+ byteArrayOutStr.size());
}
}
}
|
Output:
G 1
GE 2
GEE 3
GEEK 4
GEEKS 5
Program 2:
import java.io.*;
public class GFG {
public static void main(String[] args)
throws Exception
{
ByteArrayOutputStream byteArrayOutStr
= new ByteArrayOutputStream();
byte [] buf = { 71 , 69 , 69 , 75 , 83 ,
70 , 79 , 82 , 71 , 69 ,
69 , 75 , 83 };
for ( byte b : buf) {
byteArrayOutStr.write(b);
String s
= byteArrayOutStr.toString();
int buffsize
= byteArrayOutStr.size();
System.out.println(
s + " " + buffsize);
}
}
}
|
Output:
G 1
GE 2
GEE 3
GEEK 4
GEEKS 5
GEEKSF 6
GEEKSFO 7
GEEKSFOR 8
GEEKSFORG 9
GEEKSFORGE 10
GEEKSFORGEE 11
GEEKSFORGEEK 12
GEEKSFORGEEKS 13
References:
https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayOutputStream.html#size()
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 May, 2020
Like Article
Save Article