ByteArrayOutputStream size() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// ByteArrayOutputStream size() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws Exception
    {
  
        // Create byteArrayOutputStream
        ByteArrayOutputStream byteArrayOutStr
            = new ByteArrayOutputStream();
  
        // Create byte array
        byte[] buf = { 71, 69, 69, 75, 83 };
  
        for (byte b : buf) {
            // Write byte
            // to byteArrayOutputStream
            byteArrayOutStr.write(b);
  
            // Print the byteArrayOutputStream
            // as String and size as integer
            System.out.println(
                byteArrayOutStr.toString() + " "
                + byteArrayOutStr.size());
        }
    }
}

chevron_right


Output:

G 1
GE 2
GEE 3
GEEK 4
GEEKS 5

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// ByteArrayOutputStream size() method
  
import java.io.*;
  
public class GFG {
    public static void main(String[] args)
        throws Exception
    {
  
        // Create byteArrayOutputStream
        ByteArrayOutputStream byteArrayOutStr
            = new ByteArrayOutputStream();
  
        // Create byte array
        byte[] buf = { 71, 69, 69, 75, 83,
                       70, 79, 82, 71, 69,
                       69, 75, 83 };
  
        for (byte b : buf) {
            // Write byte
            // to byteArrayOutputStream
            byteArrayOutStr.write(b);
  
            // Convert byteArrayOutputStream
            // into String
            String s
                = byteArrayOutStr.toString();
  
            int buffsize
                = byteArrayOutStr.size();
  
            // Print string and size
            System.out.println(
                s + " " + buffsize);
        }
    }
}

chevron_right


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()

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.