Table of Contents
Syntax
Constructors
Methods
Examples to Implement Java BufferedInputStream
Example #8
Conclusion
Home Java javaTutorial Java BufferedInputStream

Java BufferedInputStream

Aug 30, 2024 pm 04:08 PM
java

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
Copy after login

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)
Copy after login

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)
Copy after login

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()
Copy after login

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

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()
Copy after login

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

Output:

Java BufferedInputStream

void mark(int readlimit)
Copy after login

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

Output:

Java BufferedInputStream

boolean markSupported()
Copy after login

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

Output:

Java BufferedInputStream

int read()
Copy after login

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

Output:

Java BufferedInputStream

int read(byte[] b, int off, int len)
Copy after login

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

Output:

Java BufferedInputStream

void reset()
Copy after login

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

Output:

Java BufferedInputStream

long skip(long n)
Copy after login

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

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.

The above is the detailed content of Java BufferedInputStream. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

See all articles