InputStream, mark(), reset()

前端 未结 2 942
予麋鹿
予麋鹿 2021-02-07 18:26

How are mark() and reset() methods working exactly(in code below), step by step ? I tried to write my own example but is starts to throw wrong mark exc

2条回答
  •  青春惊慌失措
    2021-02-07 19:03

    See the API documentation:

    mark(int)

    Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.

    The readlimit argument tells this input stream to allow that many bytes to be read before the mark position gets invalidated.

    This method simply performs in.mark(readlimit).

    reset()

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

    This method simply performs in.reset().

    Stream marks are intended to be used in situations where you need to read ahead a little to see what's in the stream. Often this is most easily done by invoking some general parser. If the stream is of the type handled by the parse, it just chugs along happily. If the stream is not of that type, the parser should toss an exception when it fails. If this happens within readlimit bytes, it allows the outer code to reset the stream and try another parser.

提交回复
热议问题