FilterInputStream reset() in Java

reset(): This method is available in the java.io.FilterInputStream class of Java.

Syntax:

void java.io.FilterInputStream.reset() throws IOException

This method repositions this stream to the position at the time the mark method was last called on this input stream.

Stream marks are intended to be used in situations where you need to read ahead a little to see what's in the stream. Often this is most easily done by invoking some general parser. If the stream is of the type handled by the parse, it just chugs along happily.

If the stream is not of that type, the parser should toss an exception when it fails.If this happens within the reading limit bytes, it allows the outer code to reset the stream and try another parser.

Parameters: NA

Returns: NA

Throws:

IOException - if the stream has not been marked or if the mark has been invalidated.

Approach 1: When no exception

Java

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;

public class FilterInputStreamreset {
    public static void main(String[] args) throws IOException {

        File data = new File("D:\\hello.txt");
        FileInputStream file = new FileInputStream(data);

        FilterInputStream filter =
new BufferedInputStream(file);

        System.err.println("Resetting the filterinputstream");
        filter.reset();

        filter.close();
    }
}

Output:

Resetting the filterinputstream


Approach 2: IOException

Java

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;

public class FilterInputStreamreset {
    public static void main(String[] args) throws IOException {

        File data = new File("D:\\hello.txt");
        FileInputStream file = new FileInputStream(data);

        FilterInputStream filter =
new BufferedInputStream(file);

        filter.reset();

        filter.close();
    }
}

Output:

Exception in thread "main" java.io.IOException: Resetting to invalid mark at java.base/java.io.BufferedInputStream.reset(BufferedInputStream.java:446)


Other methods of FilterInputStream

available()This method returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking the next caller of a method for this input stream.

close()This method closes this input stream and releases any system resources associated with the stream.

mark(int)This method marks the current position in this input stream.

markSupported()This method tests if this input stream supports the mark and reset methods.

read()This method reads the next byte of data from this input stream.

read(byte[])This method reads up to b.length bytes of data from this input stream into an array of bytes.

read(byte[], int, int)This method reads up to len bytes of data from this input stream into an array of bytes.

skip(long)This method skips over and discards n bytes of data from the input stream.


No comments:

Post a Comment