ensureAvailableLength method
- int length
Ensures that the buffer has space for N more bytes.
If buffer does not have enough space and isExpanding is false, will throw RawWriterException.
Implementation
void ensureAvailableLength(int length) {
// See whether old buffer has enough capacity.
final oldByteData = this._byteData;
final minCapacity = this._length + length;
if (oldByteData.lengthInBytes >= minCapacity) {
return;
}
if (!isExpanding) {
throw RawWriterException(
"ensureAvailaleLength($length) was called, but only ${oldByteData.lengthInBytes - this._length} bytes is available");
}
// Choose a new capacity that's a power of 2.
var newCapacity = 64;
while (newCapacity < minCapacity) {
newCapacity *= 2;
}
// Switch to the new buffer.
_byteData = new ByteData(newCapacity);
final oldIndex = this._length;
this._length = 0;
// Write contents of the old buffer.
//
// We write the whole buffer so we eliminate complex bugs when index is
// non-monotonic or data is written directly.
writeByteData(oldByteData);
this._length = oldIndex;
}