packageDocumentation module import { assert } from "./Assert" ; import { Id64 , Id64String } from "./Id" ; public export class ByteStream { private readonly _view : DataView ; private readonly _byteOffset : number ; private _curPos : number = 0 ; param buffer param subView deprecated public constructor ( buffer , subView ? ) { if ( undefined !== subView ) { this . _view = new DataView ( buffer , subView . byteOffset , subView . byteLength ) ; this . _byteOffset = subView . byteOffset ; } else { this . _view = new DataView ( buffer ) ; this . _byteOffset = 0 ; } } /** Construct a new ByteStream for the range of bytes represented by `bytes`, which may be a subset of the range of bytes
     * represented by the underlying ArrayBuffer. The read position will be set to the beginning of the range of bytes.
     */ public static fromUint8Array ( bytes ) { const { byteOffset , byteLength } = bytes ; return new ByteStream ( bytes . buffer , { byteOffset , byteLength } ) ; } param buffer param subView public static fromArrayBuffer ( buffer , subView ? ) { return new ByteStream ( buffer , subView ) ; } /** The number of bytes in this stream */ public get length ( ) : number { return this . _view . byteLength ; } see public get isPastTheEnd ( ) : boolean { return this . curPos > this . length ; } see public get isAtTheEnd ( ) : boolean { return this . curPos === this . length ; } /** The current read position as an index into the stream of bytes. */ public get curPos ( ) : number { return this . _curPos ; } public set curPos ( pos ) { this . _curPos = pos ; assert ( ! this . isPastTheEnd ) ; } /** Adds the specified number of bytes to the current read position */ public advance ( numBytes ) { this . curPos = ( this . curPos + numBytes ) ; return ! this . isPastTheEnd ; } /** Subtracts the specified number of bytes from the current read position */ public rewind ( numBytes ) { if ( this . curPos - numBytes < 0 ) return false ; this . curPos = this . curPos - numBytes ; return true ; } /** Resets the current read position to the beginning of the stream */ public reset ( ) { this . curPos = 0 ; } /** Read a unsigned 8-bit integer from the current read position and advance by 1 byte. */ public get nextUint8 ( ) : number { return this . read ( 1 , ( view ) => view . getUint8 ( this . curPos ) ) ; } /** Read an unsigned 16-bit integer from the current read position and advance by 2 bytes. */ public get nextUint16 ( ) : number { return this . read ( 2 , ( view ) => view . getUint16 ( this . curPos , true ) ) ; } /** Read an unsigned 32-bit integer from the current read position and advance by 4 bytes. */ public get nextUint32 ( ) : number { return this . read ( 4 , ( view ) => view . getUint32 ( this . curPos , true ) ) ; } /** Read a signed 32-bit integer from the current read position and advance by 4 bytes. */ public get nextInt32 ( ) : number { return this . read ( 4 , ( view ) => view . getInt32 ( this . curPos , true ) ) ; } /** Read a 32-bit floating point number from the current read position and advance by 4 bytes. */ public get nextFloat32 ( ) : number { return this . read ( 4 , ( view ) => view . getFloat32 ( this . curPos , true ) ) ; } /** Read a 64-bit floating point number from the current read position and advance by 8 bytes. */ public get nextFloat64 ( ) : number { return this . read ( 8 , ( view ) => view . getFloat64 ( this . curPos , true ) ) ; } /** Read an unsigned 64-bit integer from the current read position, advance by 8 bytes, and return the 64-bit value as an Id64String. */ public get nextId64 ( ) : Id64String { return Id64 . fromUint32Pair ( this . nextUint32 , this . nextUint32 ) ; } /** Read an unsigned 24-bit integer from the current read position and advance by 3 bytes. */ public get nextUint24 ( ) : number { return this . nextUint8 | ( this . nextUint8 << 8 ) | ( this . nextUint8 << 16 ) ; } param numBytes public nextBytes ( numBytes ) { const bytes = new Uint8Array ( this . arrayBuffer , this . curPos + this . _byteOffset , numBytes ) ; this . advance ( numBytes ) ; return bytes ; } /** Read the specified number of bytes at the specified offset without changing the read position. */ public readBytes ( readPos , numBytes ) { return new Uint8Array ( this . arrayBuffer , readPos + this . _byteOffset , numBytes ) ; } /** Read the specified number of unsigned 32-bit integers from the current read position and advance the read position. */ public nextUint32s ( numUint32s ) { const numBytes = numUint32s * 4 ; const uint32s = new Uint32Array ( this . arrayBuffer , this . curPos + this . _byteOffset , numUint32s ) ; this . advance ( numBytes ) ; return uint32s ; } /** Returns the underlying array buffer */ public get arrayBuffer ( ) : ArrayBuffer | SharedArrayBuffer { return this . _view . buffer ; } private read ( numBytes , read ) { const result = read ( this . _view ) ; this . advance ( numBytes ) ; return result ; } }