import { Logger } from "./logger" ; export class ByteWriter { public static writeUTF ( str ) { Logger . logDebug ( `ByteWriter#writeUTF: writing str ${ str }` ) ; const strlen = str . length ; if ( strlen > 65535 ) { Logger . logError ( `ByteWriter#writeUTF: UTF encoding: encoded string too long: ${ strlen } bytes` ) ; throw new Error ( `UTF encoding: encoded string too long: ${ strlen } bytes` ) ; } Logger . logDebug ( `ByteWriter#writeUTF: UTF length is ${ strlen }` ) ; const byteArrLength = new Buffer ( 2 ) ; byteArrLength [ 0 ] = ( ( strlen >> 8 ) & 0xFF ) ; byteArrLength [ 1 ] = ( ( strlen >> 0 ) & 0xFF ) ; return Buffer . concat ( [ byteArrLength , Buffer . from ( str ) ] ) ; } public static writeByte ( code ) { const buffer = new Buffer ( 1 ) ; buffer [ 0 ] = code & 0xff , code / 256 >>> 0 ; return buffer ; } param longInput return public static longToByteArray ( longInput ) { const buffer = new Buffer ( 8 ) ; for ( let index = 0 ; index < buffer . length ; index ++ ) { const byte = longInput & 0xff ; buffer [ buffer . length - index - 1 ] = byte ; longInput = ( longInput - byte ) / 256 ; } return buffer ; } public static byteArrayToLong ( buffer ) { if ( ! buffer ) { return undefined ; } let result = 0 ; let mul = 0 ; for ( let index = 0 ; index < buffer . length ; index ++ ) { result += buffer [ buffer . length - index - 1 ] * Math . pow ( 256 , mul ++ ) ; } return result ; } }