import { MethodNotImplementedError } from 'src/core/errors' ; import Stream , { StreamType } from 'src/core/streams/Stream' ; const emptyBuffer = new Uint8Array ( 0 ) ; /**
 * Super class for the decoding streams
 */ class DecodeStream implements StreamType { protected bufferLength : number ; protected buffer : Uint8Array ; protected eof : boolean ; private pos : number ; private minBufferLength : number ; constructor ( maybeMinBufferLength ? ) { this . pos = 0 ; this . bufferLength = 0 ; this . eof = false ; this . buffer = emptyBuffer ; this . minBufferLength = 512 ; if ( maybeMinBufferLength ) { while ( this . minBufferLength < maybeMinBufferLength ) { this . minBufferLength *= 2 ; } } } get isEmpty ( ) { while ( ! this . eof && this . bufferLength === 0 ) { this . readBlock ( ) ; } return this . bufferLength === 0 ; } getByte ( ) { const pos = this . pos ; while ( this . bufferLength <= pos ) { if ( this . eof ) { return - 1 ; } this . readBlock ( ) ; } return this . buffer [ this . pos ++ ] ; } getUint16 ( ) { const b0 = this . getByte ( ) ; const b1 = this . getByte ( ) ; if ( b0 === - 1 || b1 === - 1 ) { return - 1 ; } return ( b0 << 8 ) + b1 ; } getInt32 ( ) { const b0 = this . getByte ( ) ; const b1 = this . getByte ( ) ; const b2 = this . getByte ( ) ; const b3 = this . getByte ( ) ; return ( b0 << 24 ) + ( b1 << 16 ) + ( b2 << 8 ) + b3 ; } getBytes ( length , forceClamped = false ) { let end ; const pos = this . pos ; if ( length ) { this . ensureBuffer ( pos + length ) ; end = pos + length ; while ( ! this . eof && this . bufferLength < end ) { this . readBlock ( ) ; } const bufEnd = this . bufferLength ; if ( end > bufEnd ) { end = bufEnd ; } } else { while ( ! this . eof ) { this . readBlock ( ) ; } end = this . bufferLength ; } this . pos = end ; const subarray = this . buffer . subarray ( pos , end ) ; return forceClamped && ! ( subarray instanceof Uint8ClampedArray ) ? new Uint8ClampedArray ( subarray ) : subarray ; } peekByte ( ) { const peekedByte = this . getByte ( ) ; this . pos -- ; return peekedByte ; } peekBytes ( length , forceClamped = false ) { const bytes = this . getBytes ( length , forceClamped ) ; this . pos -= bytes . length ; return bytes ; } skip ( n ) { if ( ! n ) { n = 1 ; } this . pos += n ; } reset ( ) { this . pos = 0 ; } makeSubStream ( start , length ) { const end = start + length ; while ( this . bufferLength <= end && ! this . eof ) { this . readBlock ( ) ; } return new Stream ( this . buffer , start , length ) ; } decode ( ) { while ( ! this . eof ) this . readBlock ( ) ; return this . buffer . subarray ( 0 , this . bufferLength ) ; } protected readBlock ( ) { throw new MethodNotImplementedError ( this . constructor . name , 'readBlock' ) ; } protected ensureBuffer ( requested ) { const buffer = this . buffer ; if ( requested <= buffer . byteLength ) { return buffer ; } let size = this . minBufferLength ; while ( size < requested ) { size *= 2 ; } const buffer2 = new Uint8Array ( size ) ; buffer2 . set ( buffer ) ; return ( this . buffer = buffer2 ) ; } } export default DecodeStream ;