The Wayback Machine - https://web.archive.org/web/20240828080432/https://www.geeksforgeeks.org/bytearrayinputstream-reset-method-in-java-with-examples/
Open In App

ByteArrayInputStream reset() method in Java with Examples

Last Updated : 26 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The reset() method is a built-in method of the Java.io.ByteArrayInputStream is invoked by mark() method. It repositions the input stream to the marked position.

Syntax: 

public void reset()

Parameters: The function does not accepts any parameter. 
Return Value: The function does not returns anything. 

Below is the implementation of the above function:
Program 1: 

Java




// Java program to implement
// the above function
import java.io.*;
 
public class Main {
    public static void main(String[] args) throws Exception
    {
 
        byte[] buf = { 5, 6, 7, 8, 9 };
 
        // Create new byte array input stream
        ByteArrayInputStream exam
            = new ByteArrayInputStream(buf);
 
        // print bytes
        System.out.println(exam.read());
        System.out.println(exam.read());
        System.out.println(exam.read());
 
        // Use of reset() method :
        // repositioning the stream to marked positions.
        exam.reset();
 
        System.out.println("\nreset() invoked");
        System.out.println(exam.read());
        System.out.println(exam.read());
    }
}


Output: 

5
6
7

reset() invoked
5
6

 

Program 2: 

Java




// Java program to implement
// the above function
import java.io.*;
 
public class Main {
    public static void main(String[] args) throws Exception
    {
 
        byte[] buf = { 1, 2, 3, 4 };
 
        // Create new byte array input stream
        ByteArrayInputStream exam
            = new ByteArrayInputStream(buf);
 
        // print bytes
        System.out.println(exam.read());
        System.out.println(exam.read());
        System.out.println(exam.read());
 
        exam.mark(1);
 
        // Use of reset() method :
        // repositioning the stream to marked positions.
        exam.reset();
 
        System.out.println("\nreset() invoked");
        System.out.println(exam.read());
        System.out.println(exam.read());
    }
}


Output: 

1
2
3

reset() invoked
4
-1

 

Reference: https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayInputStream.html#reset()
 



Similar Reads

ByteArrayInputStream available() method in Java with Examples
The available() method is a built-in method of the Java.io.ByteArrayInputStream returns the number of remaining bytes that can be read (or skipped over) from this input stream. It tells the total no. of bytes from the Input Stream to be read. Syntax: public int available() Parameters: The function does not accept any parameter.Return Value: The fun
2 min read
ByteArrayInputStream close() method in Java with Examples
The close() method is a built-in method of the Java.io.ByteArrayInputStream closes the input stream and releases system resources associated with this stream to Garbage Collector. Syntax: public void close() Parameters: The function does not accepts any parameter. Return Value: The function returns nothing. Below is the implementation of the above
2 min read
ByteArrayInputStream mark() method in Java with Examples
The mark() method is a built-in method of the Java.io.ByteArrayInputStream marks the current position of the input stream. It sets readlimit i.e. maximum number of bytes that can be read before mark position becomes invalid. Syntax: public void mark(int arg) Parameters: The function accepts a single mandatory parameter arg which specifies the maxim
2 min read
ByteArrayInputStream markSupported() method in Java with Examples
The markSupported() method is a built-in method of the Java.io.ByteArrayInputStream method tests if this input stream supports the mark and reset methods. The markSupported method of ByteArrayInputStreamInputStream returns true always Syntax: public boolean markSupported() Parameters: The function does not accepts any parameter. Return Value: The f
2 min read
ByteArrayInputStream skip() method in Java with Examples
The skip() method is a built-in method of the Java.io.ByteArrayInputStream which skips arg bytes in the input stream. A fewer bytes is skipped if the stream has reached towards the end. Syntax: public long skip(long args) Parameters: The function accepts a single mandatory parameter args which specifies the number of bytes to be skipped Return Valu
2 min read
ByteArrayInputStream read() method in Java with Examples
The read() method of ByteArrayInputStream class in Java is used in two ways: 1. The read() method of ByteArrayInputStream class in Java is used to read the next byte of the ByteArrayInputStream. This read() method returns the byte that is read into the form of an integer and if the input stream is ended this method return -1. This method reads one
3 min read
Java.io.ByteArrayInputStream class in Java
ByteArrayInputStream class of java.io package contains all the buffers, containing bytes to be read from the Input Stream. There is no IO exception in case of ByteArrayInputStream class methods. Methods of this class can be called even after closing the Stream, there is no effect of it on the class methods. The class view is as follows: --> java
3 min read
Scanner reset() method in Java with Examples
The reset() method of java.util.Scanner class resets this scanner. On resetting a scanner, it discards all of its explicit state information which may have been changed by invocations of useDelimiter(java.util.regex.Pattern), useLocale(java.util.Locale), or useRadix(int). Syntax: public Scanner reset() Return Value: This function returns this scann
2 min read
Matcher reset() Method in Java with Examples
The reset() method of Matcher Class is used to reset this matcher, in order to understand it better it is recommended to have prior knowledge of Pattern and Matcher class in java regex sub-package. Here we will be illustrating it with help of Java programs. Syntax: public Matcher reset() Parameters: This method does not take any parameters. Return
2 min read
Matcher reset(CharSequence) method in Java with Examples
The reset(CharSequence input) method of Matcher Class is used to reset this matcher and insert the input String passed as the parameter to this matcher. Syntax: public Matcher reset(CharSequence input) Parameters: This method takes the parameter input which is the String to be inserted into matcher after getting reset. Return Value: This method ret
2 min read
LongAdder reset() method in Java with Examples
LongAdder class in Java creates a new adder with an initial sum of zero. The Java.LongAdder.reset() is an inbuilt method in Java that resets the sum to zero. When the object of the class is created its initial value is zero. Syntax: public void reset() Parameters: The function does not accepts any parameter. Return Value: This method does not retur
2 min read
DoubleAdder reset() method in Java with Examples
The java.DoubleAdder.reset() is an inbuilt method in java that resets variables maintaining the sum to zero. When the object of the class is created its initial value is zero. Syntax: public void reset() Parameters: This method does not accepts any parameter. Return Value: The method returns the reset value. Below programs illustrate the above meth
1 min read
LongAccumulator reset() method in Java with examples
The java.LongAccumulator.reset() is an inbuilt method in java that resets variables maintaining updates to the identity value. Syntax: public void reset() Parameters: The method does not accepts any parameter. Return Value: The method returns the reset value. Below programs illustrate the above method: Program 1: // Program to demonstrate the reset
1 min read
DoubleAccumulator reset() method in Java with Examples
The Java.DoubleAccumulator.reset() is an inbuilt method in java that resets variables maintaining updates to the identity value. It means it only returns the reset value and does not takes any parameter. The return type is int. Syntax: public void reset() Parameters: The method does not accepts any parameter. Return Value: The method does not retur
2 min read
Reader reset() method in Java with Examples
The reset() method of Reader Class in Java is used to reset the stream. After reset, if the stream has been marked, then this method attempts to reposition it at the mark, else it will try to position it to the starting. Syntax: public void reset() Parameters: This method do not accepts any parameter. Return Value: This method do not returns any va
3 min read
CharArrayReader reset() method in Java with Examples
The reset() method of CharArrayReader Class in Java is used to reset the stream. After reset, if the stream has been marked, then this method attempts to reposition it at the mark, else it will try to position it to the starting. Syntax: public void reset() Parameters: This method do not accepts any parameter. Return Value: This method do not retur
3 min read
StringReader reset() method in Java with Examples
The reset() method of StringReader Class in Java is used to reset the stream. After reset, if the stream has been marked, then this method attempts to reposition it at the mark, else it will try to position it to the starting. Syntax: public void reset() Parameters: This method do not accepts any parameter. Return Value: This method do not returns
3 min read
CharsetEncoder reset() method in Java with Examples
The reset() method is a built-in method of the java.nio.charset.CharsetEncoder resets this encoder, and clears all the internal states if there are any. It also resets charset-independent state and also invokes the implReset method in order to perform any charset-specific reset actions. Syntax: public final CharsetEncoder reset() Parameters: The fu
2 min read
PushbackReader reset() method in Java with Examples
The reset() method of PushbackReader Class in Java is used to reset the Stream. In the case of PushbackReader, this method always throws an exception as this method isn't supported by the PushbackReader. Syntax: public void reset() Parameters: This method do not accepts any parameter. Return Value: This method do not returns any value. Exception: T
2 min read
CharArrayWriter reset() method in Java with examples
The reset() method of the CharArrayWriter class in Java is used to reset the buffer so that it can be used again without throwing away the already allocated buffer.Syntax: public void reset() Parameters: This method does not accept any parameter.Return Value: This method does not returns anything. Below program illustrate the above method: Program
2 min read
CharsetDecoder reset() method in Java with Examples
The reset() method is a built-in method of the java.nio.charset.CharsetDecoder class which resets this CharsetDecoder and clears its internal state. Syntax: public final CharsetDecoder reset() Parameters: The function does not accepts any parameter. Return Value: The function returns this CharsetDecoder after resetting it. Below is the implementati
1 min read
LongBuffer reset() method in Java with Examples
The reset() method of java.nio.LongBuffer Class is used to reset this buffer's position to the previously-marked position. The mark's value remain unchanged during this process. Syntax: public final LongBuffer reset() Parameter: This method do not accept any parameter. Return Value: This method returns this buffer. Below are the examples to illustr
2 min read
ShortBuffer reset() method in Java with Examples
The reset() method of java.nio.ShortBuffer Class is used to reset this buffer's position to the previously-marked position. The mark's value remain unchanged during this process. Syntax: public final ShortBuffer reset() Parameter: This method do not accept any parameter. Return Value: This method returns this buffer. Below are the examples to illus
2 min read
MessageDigest reset() method in Java with Examples
The reset() method of java.security.MessageDigest class is used to reset current message digest value to default message digest value of this MessageDisgest object.Syntax: public void reset() Return Value: This method has nothing to return.Below are the examples to illustrate the reset() method:Example 1: Java Code // Java program to demonstrate //
2 min read
LogManager reset() method in Java with Examples
The reset() method of java.util.logging.LogManager is used to reset the logging configuration. This method throws SecurityException if the exception condition occurs, as given below Syntax: public void reset() throws SecurityException Parameters: This method does not accepts any parameter. Return Value: This method does not return anything. It just
1 min read
CollationElementIterator reset() method in Java with Examples
The reset() method of java.text.CollationElementIterator class is used to reset the cursor of the iterator to the beginning of the input string. Syntax: public void reset() Parameter: This method does not accept any parameter.Return Value: This method has nothing to return. Below is the examples to illustrate the reset() method: Example 1: Java Cod
2 min read
BufferedReader reset() method in Java with Examples
The reset() method of BufferedReader class in Java is used to fix or mark the position at the last marked position so that the same byte can be read again. Syntax: public void reset() throws IOException Overrides: It overrides the reset() method of Reader class. Parameters: The method does not accept any parameter. Return value: The method does not
3 min read
BufferedInputStream reset() method in Java with Examples
The reset() method of BufferedInputStream class in Java is used to reset the position of the stream to the position at the time the mark method was last called. It is used with the combination of mark() method of the same class. General Contract: There are two cases: If mark() and reset() are supported i.e., if markSupported returns true, An IOExce
3 min read
ByteArrayOutputStream reset() method in Java with Examples
The reset() method of ByteArrayOutputStream class in Java is used to reset the ByteArrayOutputStream and make the count field of this ByteArrayOutputStream to zero. As a result of this all currently accumulated output in this ByteArrayOutputStream is discarded. This ByteArrayOutputStream can be used again by reusing the already allocated buffer spa
2 min read
PushbackInputStream reset() method in Java with Examples
The reset() method of PushbackInputStream class in Java is used to reset the steam to the position where mark() method was called. This method does nothing for PushbackInputStream. Syntax: public void reset() throws IOException Overrides: This method overrides the reset() method of FilterInputStream class. Parameters: This method does not accept an
2 min read