indexOfBytes method
Returns the index of the first match for bytes
in the range of start
inclusive to end
exclusive. This expands the buffer as necessary until bytes
is found. This reads an unbounded number of
bytes into the buffer. Returns -1 if the stream is exhausted before the requested bytes are
found.
var MOVE = utf8.encode("move");
Buffer buffer = new Buffer();
buffer.writeString("Don't move! He can't see us if we don't move.");
expect(6, buffer.indexOfBytes(MOVE));
expect(40, buffer.indexOfBytes(MOVE, 12));
Implementation
@override
Future<int> indexOfBytes(Uint8List bytes, [int start = 0, int? end]) async {
checkState(!_closed, 'closed');
while (true) {
final result = _buffer.indexOfBytes(bytes, start, end);
if (result != -1) return result;
final lastBufferSize = _buffer._length;
if (await _source.read(_buffer, kBlockSize) == 0) return -1;
// Keep searching, picking up from where we left off.
start = max(start, lastBufferSize - bytes.length + 1);
}
}