/**
 *
 */ export interface Serializeable { /**
     * Returns the size the serialized object would consume.
     */ getSerializedSize ( ) : number ; /**
     * Serializes the object.
     */ serialize ( memBuffer ) ; /**
     * Deserializes the object.
     */ deserialize ( memBuffer ) ; } /**
 * The intention for this buffer is to use it for serialization.
 * Use it either for writing or for reading. Mixed write/read access
 * is not intended.
 * At construction time the internal offset is set to 0 and
 * increased on every write (or read).
 * (It is never reset.)
 *
 * When creating for writing use 'createBuffer' with an approximate length that is
 * bigger than the required length.
 * Then, when everything is written, use 'getUint8Array()' to retrieve
 * an array with the correct required length.
 *
 * For reading use 'from()' with an already existing Uint8Array.
 */ export class MemBuffer { public buffer : ArrayBuffer ; protected writeOffset = 0 ; protected readOffset = 0 ; protected dataView : DataView ; param length returns constructor ( length ? ) { if ( length ) { this . buffer = new ArrayBuffer ( length ) ; this . dataView = new DataView ( this . buffer ) ; } } param data returns static from ( data ) { const memBuffer = new MemBuffer ( ) ; memBuffer . buffer = data ; memBuffer . dataView = new DataView ( memBuffer . buffer ) ; return memBuffer ; } /**
     * Returns the current size.
     */ public getSize ( ) { return this . writeOffset ; } /**
     * Writes a value to the next position (offset).
     */ public write8 ( value ) { this . dataView ?. setUint8 ( this . writeOffset , value ) ; this . writeOffset ++ ; } /**
     * Writes a value to the next position (offset).
     */ public write16 ( value ) { this . dataView ?. setUint16 ( this . writeOffset , value ) ; this . writeOffset += 2 ; } /**
     * Writes a value to the next position (offset).
     */ public write32 ( value ) { this . dataView ?. setUint32 ( this . writeOffset , value ) ; this . writeOffset += 4 ; } /**
     * Writes an array to the next position (offset).
     */ public writeArrayBuffer ( buffer ) { const length = buffer . byteLength ; this . write32 ( length ) ; if ( this . dataView ) { const src = new Uint8Array ( buffer ) ; const dst = new Uint8Array ( this . buffer ) ; dst . set ( src , this . writeOffset ) ; } this . writeOffset += length ; } /**
     * Writes a java script number. I.e. a float 64.
     * Size = 8.
     */ public writeNumber ( value ) { this . dataView ?. setFloat64 ( this . writeOffset , value ) ; this . writeOffset += 8 ; } /**
     * Writes a boolean value to the next position (offset).
     */ public writeBoolean ( value ) { this . dataView ?. setUint8 ( this . writeOffset , value ? 1 : 0 ) ; this . writeOffset ++ ; } /**
     * Returns an array of the required length.
     */ public getUint8Array ( ) { const view = new Uint8Array ( this . buffer , 0 , this . writeOffset ) ; return view ; } /**
     * Reads a value from the next position (offset).
     */ public read8 ( ) { const value = this . dataView . getUint8 ( this . readOffset ) ; this . readOffset ++ ; return value ; } /**
     * Reads a value from the next position (offset).
     */ public read16 ( ) { const value = this . dataView . getUint16 ( this . readOffset ) ; this . readOffset += 2 ; return value ; } /**
     * Reads a value from the next position (offset).
     */ public read32 ( ) { const value = this . dataView . getUint32 ( this . readOffset ) ; this . readOffset += 4 ; return value ; } /**
     * Writes an array from the next position (offset).
     */ public readArrayBuffer ( ) { const wholeBuffer = new Uint8Array ( this . buffer ) ; const length = this . read32 ( ) ; const end = this . readOffset + length ; const buffer = wholeBuffer . subarray ( this . readOffset , end ) ; this . readOffset = end ; return buffer ; } /**
     * Reads a java script number from the next position (offset).
     * Size = 8.
     */ public readNumber ( ) { const value = this . dataView . getFloat64 ( this . readOffset ) ; this . readOffset += 8 ; return value ; } /**
     * Reads a boolean value from the next position (offset).
     */ public readBoolean ( ) { const value = ( this . dataView . getUint8 ( this . readOffset ) != 0 ) ; this . readOffset ++ ; return value ; } }