import { WMFJSError } from "./Helper" ; export class Blob { public pos : number ; private blob : Blob | ArrayBuffer ; private data : Uint8Array ; constructor ( blob , offset ? ) { if ( blob instanceof Blob ) { this . blob = blob . blob ; this . data = blob . data ; this . pos = offset || blob . pos ; } else { this . blob = blob ; this . data = new Uint8Array ( blob ) ; this . pos = offset || 0 ; } } public eof ( ) { return this . pos >= this . data . length ; } public seek ( newpos ) { if ( newpos < 0 || newpos > this . data . length ) { throw new WMFJSError ( "Invalid seek position" ) ; } this . pos = newpos ; } public skip ( cnt ) { const newPos = this . pos + cnt ; if ( newPos > this . data . length ) { throw new WMFJSError ( "Unexpected end of file" ) ; } this . pos = newPos ; } public readBinary ( cnt ) { const end = this . pos + cnt ; if ( end > this . data . length ) { throw new WMFJSError ( "Unexpected end of file" ) ; } let ret = "" ; while ( cnt -- > 0 ) { ret += String . fromCharCode ( this . data [ this . pos ++ ] ) ; } return ret ; } public readInt8 ( ) { if ( this . pos + 1 > this . data . length ) { throw new WMFJSError ( "Unexpected end of file" ) ; } return this . data [ this . pos ++ ] ; } public readUint8 ( ) { return this . readInt8 ( ) >>> 0 ; } public readInt32 ( ) { if ( this . pos + 4 > this . data . length ) { throw new WMFJSError ( "Unexpected end of file" ) ; } let val = this . data [ this . pos ++ ] ; val |= this . data [ this . pos ++ ] << 8 ; val |= this . data [ this . pos ++ ] << 16 ; val |= this . data [ this . pos ++ ] << 24 ; return val ; } public readUint32 ( ) { return this . readInt32 ( ) >>> 0 ; } public readUint16 ( ) { if ( this . pos + 2 > this . data . length ) { throw new WMFJSError ( "Unexpected end of file" ) ; } let val = this . data [ this . pos ++ ] ; val |= this . data [ this . pos ++ ] << 8 ; return val ; } public readInt16 ( ) { let val = this . readUint16 ( ) ; if ( val > 32767 ) { val -= 65536 ; } return val ; } public readString ( length ) { if ( this . pos + length > this . data . length ) { throw new WMFJSError ( "Unexpected end of file" ) ; } let ret = "" ; for ( let i = 0 ; i < length ; i ++ ) { ret += String . fromCharCode ( this . data [ this . pos ++ ] >>> 0 ) ; } return ret ; } public readNullTermString ( maxSize ) { let ret = "" ; if ( maxSize > 0 ) { maxSize -- ; for ( let i = 0 ; i < maxSize ; i ++ ) { if ( this . pos + i + 1 > this . data . length ) { throw new WMFJSError ( "Unexpected end of file" ) ; } const byte = this . data [ this . pos + i ] >>> 0 ; if ( byte === 0 ) { break ; } ret += String . fromCharCode ( byte ) ; } } return ret ; } }