Description

On this document we will be showing a java example on how to use the markSupported() method of BufferedInputStream Class. This method tests if this input stream supports the mark and reset methods. The markSupported method of BufferedInputStream returns true.

Override by:

  • markSupported in class FilterInputStream

Method Syntax

public boolean markSupported()

Method Argument

Data Type Parameter Description
N/A N/A N/A

Method Returns

This method returns boolean, which denotes if this stream type supports the mark and reset methods.

Compatibility

Requires Java 1.0 and up

Java BufferedInputStream markSupported() Example

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

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  
 * markSupported() 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?:3