Java BufferedInputStream

PHPz
リリース: 2024-08-30 16:08:54
オリジナル
968 人が閲覧しました

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 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()
ログイン後にコピー

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 BufferedInputStream

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 BufferedInputStream

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 BufferedInputStream

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 BufferedInputStream

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 BufferedInputStream

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 BufferedInputStream

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 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.

以上がJava BufferedInputStreamの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!