It's often useful to be able to read a few bytes, and then back up and reread them. For example, in the design of a Java compiler you don't know for sure whether you've read <, <<, or <<= until you've read one two many characters. It would be useful to be able to back up and reread the token once you know which token you've read. Compiler design and other parsing problems provide many more examples, but this need occurs elsewhere as well.
Some but not all input streams allow you to mark a particular position in the stream, and then return to it. There are three methods that allow this:
public synchronized void mark(int readlimit)
public synchronized void reset() throws IOException
public boolean markSupported()
markSupported()
method returns
true
if this stream supports marking and
false
if it doesn't.
The
mark()
method places a bookmark in the stream which
you can return to later using the reset()
method.
There can be only one mark()
in the stream at any
given time. Marking a second location erases the first mark.
If
marking is not supported, these methods throw
IOException
s.