export interface StreamType { isEmpty : boolean ; getByte ( ) : number ; getUint16 ( ) : number ; getInt32 ( ) : number ; getBytes ( length , forceClamped ? ) : Uint8Array | Uint8ClampedArray ; peekByte ( ) : number ; peekBytes ( length , forceClamped ? ) : Uint8Array | Uint8ClampedArray ; skip ( n ) : void ; reset ( ) : void ; makeSubStream ( start , length ) : StreamType ; decode ( ) : Uint8Array ; } class Stream implements StreamType { private bytes : Uint8Array ; private start : number ; private pos : number ; private end : number ; constructor ( buffer , start ? , length ? ) { this . bytes = buffer ; this . start = start || 0 ; this . pos = this . start ; this . end = ! ! start && ! ! length ? start + length : this . bytes . length ; } get length ( ) { return this . end - this . start ; } get isEmpty ( ) { return this . length === 0 ; } getByte ( ) { if ( this . pos >= this . end ) { return - 1 ; } return this . bytes [ 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 ) { const bytes = this . bytes ; const pos = this . pos ; const strEnd = this . end ; if ( ! length ) { const subarray = bytes . subarray ( pos , strEnd ) ; return forceClamped ? new Uint8ClampedArray ( subarray ) : subarray ; } else { let end = pos + length ; if ( end > strEnd ) { end = strEnd ; } this . pos = end ; const subarray = bytes . subarray ( pos , end ) ; return forceClamped ? 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 = this . start ; } moveStart ( ) { this . start = this . pos ; } makeSubStream ( start , length ) { return new Stream ( this . bytes , start , length ) ; } decode ( ) { return this . bytes ; } } export default Stream ;