writeBackReference method
Append count bytes copied from distance bytes before the current end
of the stream (an LZ77 back-reference, as used by Inflate). Correctly
handles overlapping copies where count > distance (e.g. RLE-style
runs), reproducing the repeating pattern.
This default implementation is expressed in terms of subset and writeBytes; subclasses backed by a contiguous buffer should override it with a direct in-buffer copy to avoid per-iteration view allocations.
Implementation
@override
void writeBackReference(int distance, int count) {
while (length + count > _buffer.length) {
_expandBuffer((length + count) - _buffer.length);
}
final src = length - distance;
if (distance >= count) {
// Non-overlapping copy: bulk move is safe and fast.
_buffer.setRange(length, length + count, _buffer, src);
} else {
// Overlapping copy (LZ77 run): copy forward byte-by-byte so the source
// bytes written earlier in this same call are repeated correctly.
var s = src;
var d = length;
final end = length + count;
while (d < end) {
_buffer[d++] = _buffer[s++];
}
}
length += count;
}