InputStream reset() in Java

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

Syntax:

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

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

The general contract of reset is:

1. If the method markSupported returns true, then:

a) If the method mark has not been called since the stream was created, or the number of bytes read from the stream since the mark was last called is larger than the argument to mark at that last call, then an IOException might be thrown.

b) 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 the 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.

2) If the method markSupported returns false, then:

a) The call to reset may throw an IOException.

b) 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.

Parameters: NA

Returns: NA

Note:

The method reset for class InputStreamdoes nothing except throw an IOException.

Throws:

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

Approach 1: When no exception

Java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamreset {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\hello2.txt");
        InputStream inputStream = new FileInputStream(file);

        try {
            inputStream.reset();
            System.out.println("Successfully reset the input stream");
        } catch (IOException e) {
            System.out.println("IOException occurs");
        }

        inputStream.close();
    }
}

Output:

Successfully reset the input stream


Approach 2: IOException

Java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamreset {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\hello.txt");
        InputStream inputStream = new FileInputStream(file);

        try {
            inputStream.reset();
            System.out.println("Successfully reset the input stream");
        } catch (IOException e) {
            System.out.println("IOException occurs");
        }

        inputStream.close();
    }
}

Output:

IOException occurs


Some other methods of InputStream

available()This method returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when the end of the stream is detected.

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.

InputStream.nullInputStream()This method returns a new InputStream that reads no bytes. The returned stream is initially open.

read()This method reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255.

read(byte[])This method reads some number of bytes from the input stream and stores them into the buffer array b.

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

readAllBytes()This method reads all remaining bytes from the input stream.

readNBytes(int)This method reads up to a specified number of bytes from the input stream.

readNBytes(byte[], int, int)This method reads the requested number of bytes from the input stream into the given byte array.

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

skipNBytes(long)This method skips over and discards exactly n bytes of data from this input stream.

transferTo(OutputStream)This method reads all bytes from this input stream and writes the bytes to the given output stream in the order that they are read.


No comments:

Post a Comment