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 into 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
import java.io.*;
public class GFG {
public static void main(String[] args)
throws Exception
{
byte [] buf = { 71 , 69 , 69 , 75 , 83 };
ByteArrayInputStream byteArrayInputStr
= new ByteArrayInputStream(buf);
int b = 0 ;
while ((b = byteArrayInputStr.read()) != - 1 ) {
char ch = ( char )b;
System.out.println("Char : " + ch);
}
}
}
|
Output: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
import java.io.*;
public class GFG {
public static void main(String[] args)
throws Exception
{
byte [] buf = { 71 , 69 , 69 , 75 , 83 };
ByteArrayInputStream byteArrayInputStr
= new ByteArrayInputStream(buf);
byte [] b = new byte [ 4 ];
int total_bytes
= byteArrayInputStr.read(b, 1 , 3 );
System.out.println("Total bytes read: "
+ total_bytes);
for ( byte ch : b) {
if (ch == 0 )
System.out.println("NULL");
else
System.out.println(( char )ch);
}
}
}
|
Output: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)