Rumah > Java > javaTutorial > Java BufferedInputStream

Java BufferedInputStream

PHPz
Lepaskan: 2024-08-30 16:08:54
asal
1028 orang telah melayarinya

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

Syntax

public class BufferedInputStream extends FilterInputStream
Salin selepas log masuk

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.

Constructors

There are two types of constructors that the Java BufferedInputStream Class, namely support:

BufferedInputStream(InputStream in)
Salin selepas log masuk

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)
Salin selepas log masuk

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.

Methods

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()
Salin selepas log masuk

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.

Examples to Implement Java BufferedInputStream

below are some examples are mentioned:

Example #1

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();
}
}
}
Salin selepas log masuk

Output:

Java BufferedInputStream

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()
Salin selepas log masuk

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.

Example #2

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();
}
}
}
Salin selepas log masuk

Output:

Java BufferedInputStream

void mark(int readlimit)
Salin selepas log masuk

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.

Example #3

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();
}
}
}
Salin selepas log masuk

Output:

Java BufferedInputStream

boolean markSupported()
Salin selepas log masuk

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.

Example #4

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();
}
}
}
Salin selepas log masuk

Output:

Java BufferedInputStream

int read()
Salin selepas log masuk

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.

Example #5

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();
}
}
}
Salin selepas log masuk

Output:

Java BufferedInputStream

int read(byte[] b, int off, int len)
Salin selepas log masuk

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.

Example #6

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();
}
}
}
Salin selepas log masuk

Output:

Java BufferedInputStream

void reset()
Salin selepas log masuk

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.

Example #7

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();
}
}
}
Salin selepas log masuk

Output:

Java BufferedInputStream

long skip(long n)
Salin selepas log masuk

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.

Example #8

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();
}
}
}
Salin selepas log masuk

Output:

Java BufferedInputStream

Conclusion

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.

Atas ialah kandungan terperinci Java BufferedInputStream. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan