java.io.BufferedInputStream read(byte[] b, int off, int len)
Description
This method implements the general contract of the corresponding read method of the InputStream class. As an additional convenience, it attempts to read as many bytes as possible by repeatedly invoking the read method of the underlying stream. This iterated read continues until one of the following conditions becomes true:
- The specified number of bytes have been read,
- The read method of the underlying stream returns -1, indicating end-of-file, or
- The available method of the underlying stream returns zero, indicating that further input requests would block.
If the first read on the underlying stream returns -1 to indicate end-of-file then this method returns -1. Otherwise this method returns the number of bytes actually read.
Subclasses of this class are encouraged, but not required, to attempt to read as many bytes as possible in the same fashion.
Override by:
- read in class FilterInputStream
Throws:
- IOException – if this input stream has been closed by invoking its close() method, or an I/O error occurs.
Method Syntax
public int read(byte[] b, int off, int len)
throws IOException
Method Argument
| Data Type | Parameter | Description |
|---|---|---|
| byte[] | b | destination buffer. |
| int | off | offset at which to start storing bytes. |
| int | len | maximum number of bytes to read. |
Method Returns
This method returns the number of bytes read, or -1 if the end of the stream has been reached.
Compatibility
Requires Java 1.0 and up
Java BufferedInputStream read(byte[] b, int off, int len) Example
Below is a java code demonstrates the use of read(byte[] b, int off, int len) method of BufferedInputStream class. The example presented might be simple however it shows the behaviour of the read(byte[] b, int off, int len).
package com.javatutorialhq.java.examples;
import java.io.BufferedInputStream;
import java.io.IOException;
/*
* This example source code demonstrates the use of
* read(byte[] b, int off, int len) method
* of BufferedInputStream class
*/
public class BufferedInputStreamReadCbufOffsetExample {
// make sure to mark this as static
static BufferedInputStream buff=null;
public static void main(String[] args) {
// ask for user first name as console input
System.out.print("Enter FirstName:");
// get the console input as String variable
String firstName=getValue();
// ask for user last name as console input
System.out.print("Enter LastName:");
// get the console input as String variable
String lastName=getValue();
// display a welcome message
System.out.println("Welcome "+firstName+" "+lastName);
try {
// closing the Stream
buff.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getValue(){
// initialize a InputStreamReader
buff = new BufferedInputStream(System.in);
// initialize placeholder variable
byte[] buffer=new byte[100];
// initialize return string value
String value="";
try {
/* read console input
the reader starts getting character at index 0
and the length of which is defined by the index size
of the buffer
*/
buff.read(buffer, 0, buffer.length); // be careful for exception
// since the buffer contains blank spaces
// we have to filter it out
for(byte b:buffer){
char charBuffer = (char)b;
// removes white spaces
if((int)charBuffer!=0 && (int)charBuffer!=10 && (int)charBuffer!=13){
// accumulate the characters into a string buffer
value=value + charBuffer;
}
}
} catch (IOException e) {
System.out.println("IOException occured");
}
return value;
}
}
![java BufferedInputStream read(byte[] b, int off, int len) example output](https://javatutorialhq.com/wp-content/uploads/2016/02/java-BufferedInputStream-readbyte-b-int-off-int-len-example-output.gif)