mark(int readlimit)


On this document we will be showing a java example on how to use the mark(int readlimit) method of BufferedInputStream Class. This method marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.

The readlimit arguments tells this input stream to allow that many bytes to be read before the mark position gets invalidated.

The general contract of mark is that, if the method markSupported returns true, the stream somehow remembers all the bytes read after the call to mark and stands ready to supply those same bytes again if and whenever the method reset is called. However, the stream is not required to remember any data at all if more than readlimit bytes are read from the stream before reset is called.

Marking a closed stream should not have any effect on the stream.

Override by:

  • mark in class FilterInputStream

Method Syntax

public void mark(int readlimit)

Method Argument

Data Type Parameter Description
int readlimit the maximum limit of bytes that can be read before the mark position becomes invalid.

Method Returns

This method returns void.


Requires Java 1.0 and up

Java BufferedInputStream mark(int readlimit) Example

Below is a java code demonstrates the use of mark(int readlimit) method of BufferedInputStream class. The example presented might be simple however it shows the behaviour of the mark(int readlimit) method.



 * This example source code demonstrates the use of  
 * mark() method of BufferedInputStream class

public class BufferedInputStreamMarkSupportedExample {

	public static void main(String[] args) {		
		try {
			// initialize an input stream which in this case
			// we are intended to read a file thus 
			// FileInputStream object suits it best
			FileInputStream fis = new FileInputStream("C:javatutorialhq"
					+ "inputtest_file.txt");
			// initialize BufferedInputStream object
			BufferedInputStream buffIs = new BufferedInputStream(fis);
			// initialize variables
			int val; //character place holder
				// convert the value to character
				char result = (char)val;
				System.out.println("Character read:"+result);
				// check how many available bytes on the stream
				int available = buffIs.available();
				System.out.println("How many available?:"+available);
				// if the remaining stream is 3
				// mark the stream		
					// check first if mark is supported by this stream
				// reset the stream
				// check how many available
				System.out.println("How many available after reset?:"+buffIs.available());
				System.out.println("Mark is not supported by this stream");
			// reset the stream
		} catch (FileNotFoundException e) {
			System.out.println("File does not exists");
		} catch (IOException e) {
			System.out.println("IOException occured");



Sample Output

Below is the sample output when you run the above example.

Character read:A
How many available?:9
Character read:B
How many available?:8
Character read:C
How many available?:7
Character read:D
How many available?:6
Character read:E
How many available?:5
Character read:a
How many available?:4
Character read:b
How many available?:3
Character read:c
How many available?:2
Character read:d
How many available?:1
Character read:e
How many available?:0
How many available after reset?10