ByteArrayInputStream read() method in Java with Examples - GeeksforGeeks

28 May, 2020
The read() method of ByteArrayInputStream class in Java is used in two ways:
1. The read() method of ByteArrayInputStream class in Java is used to read the next byte of the ByteArrayInputStream. This read() method returns the byte that is read int the form of an integer and if the input stream is ended this method return -1. This method reads one byte at a time from the stream.
Syntax:
public int read()

Specified By: This method is specified by read() method of InputStream class.
Parameters: This method does not accept any parameter.
Return value: This method returns the read byte in the form of an integer. If the stream is ended then it returns -1.
Exceptions: This method does not throw any exception.
Below program illustrates read() method in ByteArrayInputStream class in IO package:
Program:
// Java program to illustrate// ByteArrayInputStream read() method  import java.io.*;  public class GFG {    public static void main(String[] args)        throws Exception    {          // Create byte array        byte[] buf = { 71, 69, 69, 75, 83 };          // Create byteArrayInputStream        ByteArrayInputStream byteArrayInputStr            = new ByteArrayInputStream(buf);          int b = 0;        while ((b = byteArrayInputStr.read()) != -1) {            // Convert byte to character            char ch = (char)b;              // Print the character            System.out.println("Char : " + ch);        }    }}
Char : G
Char : E
Char : E
Char : K
Char : S

2. The read(byte[ ], int, int) method of ByteArrayInputStream class in Java is used to read the given number of bytes into the given byte array from the ByteArrayOutputStream. This method is different from the above read() method as it can read several bytes at a time. It returns the total number of bytes read as the return value.
Syntax:
public void read(byte[ ] b,
                 int offset,
                 int length)

Overrides: This method overrides read() method of InputStream class.
Parameters: This method accepts three parameters:
b – It represents the byte array into which data is read.
offset – It represents the starting index in the byte array b.
length – It represents the number of bytes to be read.
Return value: This method returns total number of bytes read into the buffer. If the input stream is ended, this method returns -1.
Exceptions:
NullPointerException – This method throws NullPointerException if the byte array b is null.
IndexOutOfBoundsException – This method throws IndexOutOfBoundsException if the length is greater than the length of input stream after offset or offset is negative or length is negative.
Below program illustrates read(byte[ ], int, int) method in ByteArrayInputStream class in IO package:
Program:
// Java program to illustrate// ByteArrayInputStream// read(byte[ ], int, int) method  import java.io.*;  public class GFG {    public static void main(String[] args)        throws Exception    {          // Create byte array        byte[] buf = { 71, 69, 69, 75, 83 };          // Create byteArrayInputStream        ByteArrayInputStream byteArrayInputStr            = new ByteArrayInputStream(buf);          // Create buffer        byte[] b = new byte[4];          int total_bytes            = byteArrayInputStr.read(b, 1, 3);          // Total number of bytes read        System.out.println("Total bytes read: "                           + total_bytes);          for (byte ch : b) {              // Print the character            if (ch == 0)                System.out.println("NULL");              else                System.out.println((char)ch);        }    }}
Total bytes read: 3
NULL
G
E
E

References:1. https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayInputStream.html#read()2. https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayInputStream.html#read(byte%5B%5D, int, int)
Java-Functions
Java-IO package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Functional Interfaces in Java
Stream In Java
Constructors in Java
Different ways of Reading a text file in Java
Exceptions in Java
Generics in Java
Comparator Interface in Java with Examples
Strings in Java
Difference between Abstract Class and Interface in Java
How to remove an element from ArrayList in Java?