BufferedInputStream extends FilterInputStream.
A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods.
When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time.
The mark operation remembers a point in the input stream and the reset operation causes all the bytes read since the most recent mark operation to be reread before new bytes are taken from the contained input stream.
Constructors
![BufferedInputStream in Java](http://javagyansite.com/wp-content/uploads/2020/03/screen-shot-2020-03-28-at-12.46.41-pm.png)
Methods
![Screen Shot 2020-03-28 at 1.25.06 PM](http://box5201.temp.domains/~javagyan/wp-content/uploads/2020/03/screen-shot-2020-03-28-at-1.25.06-pm.png)
Java Code
Reading the below File
![Screen Shot 2020-03-28 at 1.33.27 PM](http://box5201.temp.domains/~javagyan/wp-content/uploads/2020/03/screen-shot-2020-03-28-at-1.33.27-pm.png)
package com.java.io;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class BufferedInputStreamExample {
public static void main(String[] args) {
InputStream inputStream = null;
BufferedInputStream bis = null;
try {
inputStream = new FileInputStream("/Test/TestFolder/test-file.txt");
bis = new BufferedInputStream(inputStream);
while (bis.available() > 0) {
System.out.print((char) bis.read());
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (inputStream != null)
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
if (bis != null)
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Test Data
Using markSupported, mark, skip and reset.
package com.java.io;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class BufferedInputStreamExample {
public static void main(String[] args) {
InputStream inputStream = null;
BufferedInputStream bis = null;
try {
inputStream = new FileInputStream("/Test/TestFolder/test-file.txt");
bis = new BufferedInputStream(inputStream);
System.out.println("Total number of Bytes in the File:" + bis.available());
// Check if markSupported by BufferedInputStream
System.out.println("Is Mark Supported:" + bis.markSupported());
if (bis.markSupported()) {
// marks the current position
bis.mark(bis.available());
}
// Skip 5 bytes of data from this input stream
bis.skip(5);
System.out.print("After Skip File Content:");
while (bis.available() > 0) {
System.out.print((char) bis.read());
}
// Resets to the marked position
bis.reset();
System.out.println("");
System.out.print("After Reset File Content:");
while (bis.available() > 0) {
System.out.print((char) bis.read());
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (inputStream != null)
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
if (bis != null)
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Total number of Bytes in the File:9
Is Mark Supported:true
After Skip File Content:Data
After Reset File Content:Test Data