java.io.BufferedInputStream reset()
Description
On this document we will be showing a java example on how to use the reset() method of BufferedInputStream Class. This method reposition this stream to the position at the time the mark method was last called on this input stream.
The general contract of reset is:
- If the method markSupported returns true, then:
- If the method mark has not been called since the stream was created, or the number of bytes read from the stream since mark was last called is larger than the argument to mark at that last call, then an IOException might be thrown.
- If such an IOException is not thrown, then the stream is reset to a state such that all the bytes read since the most recent call to mark (or since the start of the file, if mark has not been called) will be resupplied to subsequent callers of the read method, followed by any bytes that otherwise would have been the next input data as of the time of the call to reset.
- If the method markSupported returns false, then:
- The call to reset may throw an IOException.
- If an IOException is not thrown, then the stream is reset to a fixed state that depends on the particular type of the input stream and how it was created. The bytes that will be supplied to subsequent callers of the read method depend on the particular type of the input stream.
- The method reset for class InputStream does nothing except throw an IOException.
Override by:
- reset in class FilterInputStream
Throws:
- IOException – if this stream has not been marked or, if the mark has been invalidated, or the stream has been closed by invoking its close() method, or an I/O error occurs.
Method Syntax
public void reset()
throws IOException
Method Argument
Data Type | Parameter | Description |
---|---|---|
N/A | N/A | N/A |
Method Returns
This method returns void.
Compatibility
Requires Java 1.0 and up
Java BufferedInputStream reset() Example
Below is a java code demonstrates the use of reset() method of BufferedInputStream class. The example presented might be simple however it shows the behaviour of the reset().
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 * reset() method of BufferedInputStream class */ public class BufferedInputStreamResetExample { 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(available==3){ // if the remaining stream is 3 // mark the stream buffIs.mark(0); } } // reset the stream buffIs.reset(); // check how many available System.out.println("How many available after reset?:"+buffIs.available()); // 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