reset method
Repositions this stream to the position at the time the mark method was last called on this input stream.
If the method markSupported returns true
, then:
- If mark has not been called since the stream was created, or the number of bytes read from the stream since mark was last called is larger than the argument to mark at that last call, then an IOException might be thrown.
- If such an IOException is not thrown, then the stream is reset to a state such that all the bytes read since the most recent call to mark will be resupplied to subsequent callers of the read method, followed by any bytes that otherwise would have been the next input data as of the time of the call to reset.
Example
final input = BufferedInputStream(FileInputStream('config.txt'));
if (input.markSupported()) {
input.mark(1024);
// Try to parse as JSON
try {
final jsonData = await input.readAll();
final config = jsonDecode(String.fromCharCodes(jsonData));
return config;
} catch (e) {
// Reset and try parsing as XML
await input.reset();
final xmlData = await input.readAll();
return parseXml(String.fromCharCodes(xmlData));
}
}
Throws IOException if the stream has not been marked or if the mark has been invalidated, or if the stream does not support reset, or if some other I/O error occurs.
Implementation
@override
Future<void> reset() async {
checkClosed();
if (_markPosition < 0) {
throw IOException('Mark not set or invalidated');
}
_position = _markPosition;
}