java.io.BufferedInputStream mark(int readlimit)
Description
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.
Compatibility
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.
package com.javatutorialhq.java.examples;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/*
* 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
while((val=buffIs.read())!=-1){
// 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
if(available==3){
// check first if mark is supported by this stream
if(buffIs.markSupported()){
buffIs.mark(0);
}
}
}
if(buffIs.markSupported()){
// reset the stream
buffIs.reset();
// check how many available
System.out.println("How many available after reset?:"+buffIs.available());
}
else{
System.out.println("Mark is not supported by this stream");
}
// reset the stream
buffIs.close();
fis.close();
} 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