Marking and resetting

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 too 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; then return to it. Three methods support this:

 public void    mark(int readLimit)
 public void    reset() throws IOException
 public boolean markSupported()

The boolean markSupported() method returns true if this stream supports marking and false if it doesn't.

Assuming the stream does support marking, 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 IOExceptions.


Previous | Next | Top | Cafe au Lait

Copyright 1997 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified May 22, 1997