HudaTutorials.com

Java BufferedInputStream Class

Last updated on

java.io.BufferedInputStream Class

BufferedInputStream Class in Java

BufferedInputStream

A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created, an internal buffer array is created. The internal buffer is refilled as necessary from the contained input stream, many bytes at a time. The mark operation remembers a point in the input stream and the reset operation causes all the bytes read since the most recent mark operation to be reread before new bytes are taken from the contained input stream.

Buffering I/O is a very common performance optimization. Java’s BufferedInputStream class allows you to "wrap" any InputStream into a buffered stream to improve performance.

In this tutorial you can learn about Java BufferedInputStream class. And how to use Java BufferedInputStream class examples.

What is Java BufferedInputStream Class ?

Java BufferedInputStream class is used to read data bytes from input stream. Class BufferedInputStream is since JDK 1.0 it is also work in Java 8 and above.

What are the Constructors of Java BufferedInputStream Class ?

Following are the constructors of BufferedInputStream class.

BufferedInputStream(InputStream in)

Creates a BufferedInputStream and saves its argument, the input stream in, for later use.

BufferedInputStream(InputStream in, int size)

Creates a BufferedInputStream with the specified buffer size, and saves its argument, the input stream in, for later use.

Methods of class BufferedInputStream

  • available() : Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
  • close() : Closes this input stream and releases any system resources associated with the stream.
  • mark(int readlimit) : Marks the current position in this input stream.
  • markSupported() : Tests if this input stream supports the mark and reset methods.
  • read() : Reads the next byte of data from the input stream.
  • read(byte[] b, int off, int len) : Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.
  • reset() : Repositions this stream to the position at the time the mark method was last called on this input stream.
  • skip(long n) : Skips over and discards n bytes of data from this input stream.

java.io.BufferedInputStream class Example

/*	Java BufferedInputStream class Example
	Following example shows how to read the text
	from file using java BufferedInputStream.
	Save with file name BufferedInputStreamExample.java	*/
import java.io.*;
public class BufferedInputStreamExample
{
	public static void main(String args[])
	{
		// BufferedInputStream DECLARATION
		BufferedInputStream bis = null;
		// FileInputStream DECLARATION
		FileInputStream fis = null;
		try
		{
			int ch=0;
			fis = new FileInputStream("BufferedInputStreamExample.java");
			bis = new BufferedInputStream(fis);
			while((ch = bis.read()) != -1)
			{
				// CONVERT byte TO char
				System.out.print( (char) ch);
			}
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : "+e.getMessage());
		}
		finally
		{
			// SHOULD CLOSE STREAMS FINALLY
			try
			{
				if(bis!=null)
					bis.close();
				if(fis!=null)
					fis.close();
			}
			catch(Exception ex)
			{
				System.out.println("Error Occurred : "+ex.getMessage());
			}
		}
	}
}

The above program output may look a little different from what you expected because System.in is line buffered, by default. This means that no input is actually passed to the program until you press ENTER. As you can guess, this does not make read() particularly valuable for interactive console input.

java.io.BufferedInputStream class Example 2

The following program demonstrates BufferedReader and the readLine() method; the program reads and displays lines of text until you enter the word "stop".

/*	Java BufferedInputStream class Example 2
	Following example shows how to read total bytes
	from file into array using java BufferedInputStream.
	Save with file name BufferedInputStreamExample2.java	*/
import java.io.*;
public class BufferedInputStreamExample2
{
	public static void main(String args[])
	{
		// java.io.BufferedInputStream DECLARATION
		java.io.BufferedInputStream bis = null;
		// FileInputStream DECLARATION
		FileInputStream fis = null;
		// ARRAY TO READ BYTES
		byte bytebuff[] = null;
		try
		{
			fis = new FileInputStream("BufferedInputStreamExample2.java");
			bis = new BufferedInputStream(fis);
			// GET THE AVAILABLE BYTES FROM InputReader AND CREATES ARRAY
			bytebuff = new byte[fis.available()];
			// READ TOTAL BYTES FROM FILE
			bis.read(bytebuff, 0,fis.available());
			// PRINT CHARACTERS FROM ARRAY
			for(int j=0;j<bytebuff.length;j++)
			{
				// CONVERT byte TO char
				System.out.print( (char) bytebuff[j]);
			}
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : "+e.getMessage());
		}
		finally
		{
			// SHOULD CLOSE STREAMS FINALLY
			try
			{
				// RELEASE ARRAY MEMORY
				bytebuff = null;
				if(bis!=null)
					bis.close();
				if(fis!=null)
					fis.close();
			}
			catch(Exception ex)
			{
				System.out.println("Error Occurred : "+ex.getMessage());
			}
		}
	}
}

How to read total bytes from file into array using java BufferedInputStream

/*	How to read total bytes from file into array
	using java BufferedInputStream Example
	Save with file name BufferedInputStreamExample3.java	*/
import java.io.*;
public class BufferedInputStreamExample3
{
	public static void main(String args[])
	{
		String str = "This is a © copyright symbol but not © not.\n";
		// java.io.BufferedInputStream DECLARATION
		java.io.BufferedInputStream bis = null;
		// java.io.ByteArrayInputStream DECLARATION
		ByteArrayInputStream bais = null;
		// ARRAY TO READ BYTES
		byte bytebuff[] = null;
		try
		{
			bytebuff = str.getBytes();
			bais = new ByteArrayInputStream(bytebuff);
			bis = new BufferedInputStream(bais);
			while(
			{
				// CONVERT byte TO char
				System.out.print( (char) bytebuff[j]);
			}
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : "+e.getMessage());
		}
		finally
		{
			// SHOULD CLOSE STREAMS FINALLY
			try
			{
				// RELEASE ARRAY MEMORY
				bytebuff = null;
				if(bis!=null)
					bis.close();
				if(fis!=null)
					fis.close();
			}
			catch(Exception ex)
			{
				System.out.println("Error Occurred : "+ex.getMessage());
			}
		}
	}
}