首页  >  文章  >  Java  >  Java 缓冲输入流

Java 缓冲输入流

PHPz
PHPz原创
2024-08-30 16:08:54968浏览

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

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)

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.

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()

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();
}
}
}

Output:

Java 缓冲输入流

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.

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();
}
}
}

Output:

Java 缓冲输入流

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.

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();
}
}
}

Output:

Java 缓冲输入流

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.

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();
}
}
}

Output:

Java 缓冲输入流

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.

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();
}
}
}

Output:

Java 缓冲输入流

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.

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();
}
}
}

Output:

Java 缓冲输入流

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.

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();
}
}
}

Output:

Java 缓冲输入流

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.

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();
}
}
}

Output:

Java 缓冲输入流

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.

以上是Java 缓冲输入流的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn