import { NextByteAssertionError } from 'src/core/errors' ; import PDFRawStream from 'src/core/objects/PDFRawStream' ; import { decodePDFRawStream } from 'src/core/streams/decode' ; import CharCodes from 'src/core/syntax/CharCodes' ; class ByteStream { static of = ( bytes ) => new ByteStream ( bytes ) ; static fromPDFRawStream = ( rawStream ) => ByteStream . of ( decodePDFRawStream ( rawStream ) . decode ( ) ) ; private readonly bytes : Uint8Array ; private readonly length : number ; private idx = 0 ; private line = 0 ; private column = 0 ; constructor ( bytes ) { this . bytes = bytes ; this . length = this . bytes . length ; } moveTo ( offset ) { this . idx = offset ; } next ( ) { const byte = this . bytes [ this . idx ++ ] ; if ( byte === CharCodes . Newline ) { this . line += 1 ; this . column = 0 ; } else { this . column += 1 ; } return byte ; } assertNext ( expected ) { if ( this . peek ( ) !== expected ) { throw new NextByteAssertionError ( this . position ( ) , expected , this . peek ( ) ) ; } return this . next ( ) ; } peek ( ) { return this . bytes [ this . idx ] ; } peekAhead ( steps ) { return this . bytes [ this . idx + steps ] ; } peekAt ( offset ) { return this . bytes [ offset ] ; } done ( ) { return this . idx >= this . length ; } offset ( ) { return this . idx ; } slice ( start , end ) { return this . bytes . slice ( start , end ) ; } position ( ) { return { line : this . line , column : this . column , offset : this . idx } ; } } export default ByteStream ;