Java BufferedInputStream is a mechanism where the Input buffer has the capability to assign the buffer some bytes as part of the stream internally. Whenever a BufferedInputStream is invoked or created, an internal array will get created and then it will perform any kind of further functionality of adding bytes into the stream. The buffer mechanism in the BufferedInputStream class provides flexibility and enhances the overall performance of the buffer. If some information gets missed from the stream, it refills and fills the evacuated position by assigning them.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
public class BufferedInputStream extends FilterInputStream
The syntax flow is as follows:
A class named BufferedInputStream will be used to get all the methods and their input parameters related to the class to be extended using the FilterInputStream.
There are two types of constructors that the Java BufferedInputStream Class, namely support:
BufferedInputStream(InputStream in)
This helps in creating a BufferedInputStream and saves its argument into that buffer as the input stream in, and further, this argument is used later at some point of time for input streamflow.
BufferedInputStream(input stream in, int size)
This constructor creates a bufferedInputStream with some specific buffer size and saves its arguments for inserting into the stream later at some point of time with respect to time and size.
These are the methods supported by the Java BufferedInputStream class.
int available() void close() void mark(int readlimit) boolean markSupported() int read() int read(byte[] b, int off, int len) void reset() long skip(long n) int available()
It just provides an estimation for the number of bytes provided to the input stream, and the invocation of the next input stream method should also not hinder the previous method for its insertion of bytes to the input stream. It can also be said that its return type is the estimated number of bytes that can be read or skipped while passing as an input stream.
below are some examples are mentioned:
This program illustrates the int available method of BufferedInputStream Class:
Code:
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class Java_Input_Buffer_Ex { public static void main(String[] args) throws Exception{ BufferedInputStream inpt_smpl = null; FileInputStream sample_input_stream = null; try { sample_input_stream = new FileInputStream("C:\\Users\\adutta\\anu_test.txt"); inpt_smpl = new BufferedInputStream(sample_input_stream); while( inpt_smpl.available() > 0 ) { Integer No_of_bytes = inpt_smpl.available(); System.out.println("Number of Bytes Available to I/O stream = " + No_of_bytes ); char ch = (char)inpt_smpl.read(); System.out.println("Read Each character = " + ch ); } } catch(Exception e) { e.printStackTrace(); } finally { if(sample_input_stream!=null) sample_input_stream.close(); if(inpt_smpl!=null) inpt_smpl.close(); } } }
Output:
Note: Make sure before performing the programs, it is a must to save the text file with some data that the inputBufferStream will invoke later.void close()
Explanation: As its name suggests void close() method as part of the Java BufferedInputStream method is used to close the input stream once the stream and its associated buffer working is finished. It will be used for releasing and freeing the resources once the stream is asked to close. The method will throw Exception once closed and later tried again to resume for the remaining method like reading, available, reset, skip.
This program illustrates the void close() method of the Java BufferedInputStream class.
Code:
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class Java_Input_Buffer_Ex { public static void main(String[] args) throws Exception{ BufferedInputStream inpt_smpl = null; FileInputStream sample_input_stream = null; try { sample_input_stream = new FileInputStream("C:\\Users\\adutta\\anu_test.txt"); inpt_smpl = new BufferedInputStream(sample_input_stream); int byte_num = inpt_smpl.available(); System.out.println(byte_num); inpt_smpl.close(); byte_num = inpt_smpl.available(); System.out.println(byte_num); } catch(Exception e) { e.printStackTrace(); } finally { if(sample_input_stream!=null) sample_input_stream.close(); if(inpt_smpl!=null) inpt_smpl.close(); } } }
Output:
void mark(int readlimit)
Explanation: This method, as part of the BufferedInputStream is used to set up the buffer with some constraint and limit of bytes with some int value that will be used for reading the value before the marked position with the limit set up becomes invalid.
This program illustrates the void mark(int readlimit) method of the Java BufferedInputStream class.
Code:
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class Java_Input_Buffer_Ex { public static void main(String[] args) throws Exception{ BufferedInputStream inpt_smpl = null; FileInputStream sample_input_stream = null; try { sample_input_stream = new FileInputStream("C:\\Users\\adutta\\anu_test.txt"); inpt_smpl = new BufferedInputStream(sample_input_stream); System.out.println("Character_value : "+(char)inpt_smpl.read()); System.out.println("Character_value : "+(char)inpt_smpl.read()); System.out.println("Character_value : "+(char)inpt_smpl.read()); inpt_smpl.mark(0); System.out.println("Character_value : "+(char)inpt_smpl.read()); System.out.println("Invoked the reset() method"); inpt_smpl.reset(); System.out.println("character_value : "+(char)inpt_smpl.read()); System.out.println("character_value : "+(char)inpt_smpl.read()); } catch(Exception e) { e.printStackTrace(); } finally { if(sample_input_stream!=null) sample_input_stream.close(); if(inpt_smpl!=null) inpt_smpl.close(); } } }
Output:
boolean markSupported()
Explanation: This method, as part of the Java BufferedInputStream class, is used for verification purposes whether the reset () and mark () methods support for the class and returns some value as true or false for the boolean markSupported.
This program illustrates the boolean markSupported method of the Java BufferedInputStream class.
Code:
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class Java_Input_Buffer_Ex { public static void main(String[] args) throws Exception{ BufferedInputStream inpt_smpl = null; FileInputStream sample_input_stream = null; boolean bool_val = false; try { sample_input_stream = new FileInputStream("C:\\Users\\adutta\\anu_test.txt"); inpt_smpl = new BufferedInputStream(sample_input_stream); bool_val = inpt_smpl.markSupported(); System.out.println("Support for mark() and reset() : "+bool_val); } catch(Exception e) { e.printStackTrace(); } finally { if(sample_input_stream!=null) sample_input_stream.close(); if(inpt_smpl!=null) inpt_smpl.close(); } } }
Output:
int read()
Explanation: This is a method defined in java BufferedInputStream, which is used for reading the next byte of already present data from the input stream and doesn’t have any return type.
This program illustrates the int read () method of the BufferedInputStream class.
Code:
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class Java_Input_Buffer_Ex { public static void main(String[] args) throws Exception{ BufferedInputStream inpt_smpl = null; FileInputStream sample_input_stream = null; try { sample_input_stream = new FileInputStream("C:\\Users\\adutta\\anu_test.txt"); inpt_smpl = new BufferedInputStream(sample_input_stream); while(inpt_smpl.available()>0) { char chr_a = (char)inpt_smpl.read(); System.out.println("character_val: "+chr_a); } } catch(Exception e) { e.printStackTrace(); } finally { if(sample_input_stream!=null) sample_input_stream.close(); if(inpt_smpl!=null) inpt_smpl.close(); } } }
Output:
int read(byte[] b, int off, int len)
Explanation: Given is an offset based on which one input buffer will get created, and then that input stream will be put as an input for the read () method of the present stream. The read continues until the final value becomes true.
This program illustrates the int read (byte[]b, int off, int len) method of the BufferedInputStream class.
Code:
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class Java_Input_Buffer_Ex { public static void main(String[] args) throws Exception{ BufferedInputStream inpt_smpl = null; FileInputStream sample_input_stream = null; try { sample_input_stream = new FileInputStream("C:\\Users\\adutta\\anu_test.txt"); inpt_smpl = new BufferedInputStream(sample_input_stream); int byte_num = inpt_smpl.available(); byte[] bufr = new byte[byte_num]; inpt_smpl.read(bufr, 4, 8); for (byte z : bufr) { System.out.println((char)z+": " + z); } } catch(Exception e) { e.printStackTrace(); } finally { if(sample_input_stream!=null) sample_input_stream.close(); if(inpt_smpl!=null) inpt_smpl.close(); } } }
Output:
void reset()
Explanation: This method resets the stream to the position where the last input stream with the limit or mark was called lastly on the input stream.
This program illustrates the void reset () method of the BufferedInputStream class.
Code:
import java.io.FileInputStream; import java.io.IOException; import java.io.BufferedInputStream; public class Java_Input_Buffer_Ex { public static void main(String[] args) throws Exception{ BufferedInputStream inpt_smpl = null; FileInputStream sample_input_stream = null; try { sample_input_stream = new FileInputStream("C:\\Users\\adutta\\anu_test.txt"); inpt_smpl = new BufferedInputStream(sample_input_stream); System.out.println("Character_val : "+(char)inpt_smpl.read()); System.out.println("Character_val : "+(char)inpt_smpl.read()); System.out.println("Character_val : "+(char)inpt_smpl.read()); System.out.println("Character_val : "+(char)inpt_smpl.read()); System.out.println("Character_val : "+(char)inpt_smpl.read()); inpt_smpl.mark(0); System.out.println("Character_val : "+(char)inpt_smpl.read()); System.out.println("Invoke the reset_mathod for verifying"); System.out.println("character_val: "+(char)inpt_smpl.read()); System.out.println("character_val : "+(char)inpt_smpl.read()); } catch(Exception e) { e.printStackTrace(); } finally { if(sample_input_stream!=null) sample_input_stream.close(); if(inpt_smpl!=null) inpt_smpl.close(); } } }
Output:
long skip(long n)
Explanation: It is a method that is used for skipping some of the desired values from the BufferedInputStream and then to formulate the entire string and its value.
This program illustrates the long skip(long n) method of the BufferedInputStream class.
Code:
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class Java_Input_Buffer_Ex { public static void main(String[] args) throws Exception{ BufferedInputStream inpt_smpl = null; FileInputStream sample_input_stream = null; try { sample_input_stream = new FileInputStream("C:\\Users\\adutta\\anu_test.txt"); inpt_smpl = new BufferedInputStream(sample_input_stream); while(inpt_smpl.available()>0) { inpt_smpl.skip(3); char p = (char)inpt_smpl.read(); System.out.print(" " + p); } } catch(Exception e) { e.printStackTrace(); } finally { if(sample_input_stream!=null) sample_input_stream.close(); if(inpt_smpl!=null) inpt_smpl.close(); } } }
Output:
Java BufferedInputStream is a class that comprises many constructors and methods that will be used for keeping some useful information without much data loss that too internally by just calling the required functions and methods at the time of execution and compilation, which will be used for retaining and modifying the values.
The above is the detailed content of Java BufferedInputStream. For more information, please follow other related articles on the PHP Chinese website!