According to the documentation,
void mark(int readlimit): Marks the current position in this input stream. The mark method of PushbackInputStream does nothing.
void reset(): Repositions this stream to the position at the time the mark method was last called on this input stream. The method reset for class PushbackInputStream does nothing except throw an IOException.
You can check above 'DOES NOTHING'. So, If this is the case, Why and Where this is useful ? In which situation I can use above both the methods ?
and below is the example :
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.PushbackInputStream;
public class PushbackInputStreamDemo
{
public static void main(String arg[]) throws Exception
{
PrintWriter pw = new PrintWriter(System.out, true);
String str = "GeeksforGeeks a computer science portal ";
byte b[] = str.getBytes();
ByteArrayInputStream bout = new ByteArrayInputStream(b);
PushbackInputStream push = new PushbackInputStream(bout);
int c;
while((c=push.read())!=-1)
{
pw.print((char)c);
}
pw.println();
// marking the position
push.mark(5);
// reseting is not supported throw exception
push.reset();
pw.close();
}
}
Above is the sample, But not getting what exactly both the methods does. Please guide.
The mark
and reset
methods are optional operations that not every InputStream needs to support. You can call markSupported
to find out if it does.
PushbackInputStream does not support these methods.
The methods are still there, because they are defined in the InputStream
interface. Maybe a bad design decision (could have been added to a separate interface), but that is how it is.