import DecodeStream from 'src/core/streams/DecodeStream' ; import { StreamType } from 'src/core/streams/Stream' ; class RunLengthStream extends DecodeStream { private stream : StreamType ; constructor ( stream , maybeLength ? ) { super ( maybeLength ) ; this . stream = stream ; } protected readBlock ( ) { const repeatHeader = this . stream . getBytes ( 2 ) ; if ( ! repeatHeader || repeatHeader . length < 2 || repeatHeader [ 0 ] === 128 ) { this . eof = true ; return ; } let buffer ; let bufferLength = this . bufferLength ; let n = repeatHeader [ 0 ] ; if ( n < 128 ) { buffer = this . ensureBuffer ( bufferLength + n + 1 ) ; buffer [ bufferLength ++ ] = repeatHeader [ 1 ] ; if ( n > 0 ) { const source = this . stream . getBytes ( n ) ; buffer . set ( source , bufferLength ) ; bufferLength += n ; } } else { n = 257 - n ; const b = repeatHeader [ 1 ] ; buffer = this . ensureBuffer ( bufferLength + n + 1 ) ; for ( let i = 0 ; i < n ; i ++ ) { buffer [ bufferLength ++ ] = b ; } } this . bufferLength = bufferLength ; } } export default RunLengthStream ;