Thursday, July 4, 2024
HomeLanguagesJavaByteArrayOutputStream toByteArray() method in Java with Examples

ByteArrayOutputStream toByteArray() method in Java with Examples

The toByteArray() method of ByteArrayOutputStream class in Java is used to create a newly allocated byte array. The size of the newly allocated byte array is equal to the current size of this output stream. This method copies valid contents of the buffer into it.

Syntax:

public byte[] toByteArray()

Parameters: This method does not accept any parameter.

Return value: The method returns newly allocated byte array which has the valid contents of this output stream.

Exceptions: This method does not throw any exception.

Below programs illustrate toByteArray() method in ByteArrayOutputStream class in IO package:

Program 1:




// Java program to illustrate
// ByteArrayOutputStream toByteArray() 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 };
  
        // Write byte array
        // to byteArrayOutputStream
        byteArrayOutStr.write(buf);
  
        for (byte b : byteArrayOutStr
                          .toByteArray()) {
  
            // Print the byte
            System.out.println((char)b);
        }
    }
}


Output:

G
E
E
K
S

Program 2:




// Java program to illustrate
// ByteArrayOutputStream toByteArray() 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 };
  
        // Write byte array
        // to byteArrayOutputStream
        byteArrayOutStr.write(buf);
  
        for (byte b : byteArrayOutStr
                          .toByteArray()) {
  
            // Print the byte
            System.out.println((char)b);
        }
    }
}


Output:

G
E
E
K
S
F
O
R
G
E
E
K
S

References:
https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayOutputStream.html#toByteArray()

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments