read(byte[]): This method is available in java.io.BufferedInputStream class of Java.
Syntax:
int java.io.FilterInputStream.read(byte[] b) throws IOException
This method takes one argument. This method reads up to b.length bytes of data from this input stream into an array of bytes.
Note This method blocks until some input is available.
Parameters: One parameter is required for this method.
b: the buffer into which the data is read.
Returns: the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
IOException - if an I/O error occurs.
Approach 1: When no exception
Java
import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;public class BufferedInputStreamread2 {public static void main(String[] args) throws IOException {InputStream inputStream = InputStream.nullInputStream();BufferedInputStream bufferedInputStream =new BufferedInputStream(inputStream);byte b[] = { 1, 1, 2, 3 };System.out.println(bufferedInputStream.read(b));}}
Output:
-1
Approach 2: When IOException
Java
import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;public class BufferedInputStreamread2 {public static void main(String[] args) throws IOException {InputStream inputStream = InputStream.nullInputStream();BufferedInputStream bufferedInputStream =new BufferedInputStream(inputStream);byte b[] = { 1, 1, 2, 3 };bufferedInputStream.close();System.out.println(bufferedInputStream.read(b));}}
Output:
Exception in thread "main" java.io.IOException: Stream closed at java.base/java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:168) at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:334) at java.base/java.io.FilterInputStream.read(FilterInputStream.java:106)
No comments:
Post a Comment