How to use bytebuffer

Comprehensive bytebuffer code examples:

How to use bytebuffer.atob:

1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
}
var newMetaData = "";
if (payload.Certificate) {
    var certificate = payload.Certificate;
    try {
        var certStr = ByteBuffer.atob(certificate.replace("BEGIN CERTIFICATE--- ", "").replace(" ---END CERTIFICATE", ""));
        newMetaData = certStr;
    } catch (err) {
        console.log(err);
        console.log({ details: "InsertData atob certificate exception", error: err });

How to use bytebuffer.Long:

10
11
12
13
14
15
16
17
18
19
20
21
22
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }


var assert = require('assert');


var _require = require('bytebuffer'),
    Long = _require.Long;


module.exports = {
    ULong: ULong,
    isName: isName,

How to use bytebuffer.fromString:

266
267
268
269
270
271
272
273
274
275
if (/\.$/.test(value) ){
    // remove trailing dot
    value = value.substring(0, value.length - 1);
}
if (value === "") { value = "0"; }
var long_string = Long.fromString(value).toString();
if (long_string !== value.trim()) {
    throw new Error(`overflow ${field_name} ${value}`);
}
return;

How to use bytebuffer.fromNumber:

23
24
25
26
27
28
29
30
31
32
}

_createClass(ObjectId, [{
    key: 'toLong',
    value: function toLong() {
        return Long.fromNumber(this.space).shiftLeft(56).or(Long.fromNumber(this.type).shiftLeft(48).or(this.instance));
    }
}, {
    key: 'appendByteBuffer',
    value: function appendByteBuffer(b) {

How to use bytebuffer.isByteBuffer:

93
94
95
96
97
98
99
100
101
102
if (!responseProtobuf) {
	resolve(payload);
	return;
}

if (payload instanceof Buffer || ByteBuffer.isByteBuffer(payload)) {
	let msg = responseProtobuf.decode(ByteBuffer.isByteBuffer(payload) ? payload.toBuffer() : payload);
	msg = responseProtobuf.toObject(msg, { defaults: true });
	resolve(msg);
} else {

How to use bytebuffer.METRICS_CHARS:

2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
    offset = metrics;
    metrics = undefined;
}
var relative = typeof offset === 'undefined';
if (relative) offset = this.offset;
if (typeof metrics === 'undefined') metrics = ByteBuffer.METRICS_CHARS;
if (!this.noAssert) {
    if (typeof length !== 'number' || length % 1 !== 0)
        throw TypeError("Illegal length: "+length+" (not an integer)");
    length |= 0;

How to use bytebuffer.calculateVarint64:

2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
        throw RangeError("Illegal offset: 0 <= "+offset+" (+"+0+") <= "+this.buffer.byteLength);
}
if (typeof value === 'number')
    value = Long.fromNumber(value, false);
else if (value.unsigned !== false) value = value.toSigned();
var size = ByteBuffer.calculateVarint64(value),
    part0 = value.toInt() >>> 0,
    part1 = value.shiftRightUnsigned(28).toInt() >>> 0,
    part2 = value.shiftRightUnsigned(56).toInt() >>> 0;
offset += size;

How to use bytebuffer.fromUTF8:

1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
case "hex":
    return ByteBuffer.fromHex(buffer, littleEndian);
case "binary":
    return ByteBuffer.fromBinary(buffer, littleEndian);
case "utf8":
    return ByteBuffer.fromUTF8(buffer, littleEndian);
case "debug":
    return ByteBuffer.fromDebug(buffer, littleEndian);
default:
    throw Error("Unsupported encoding: "+encoding);

How to use bytebuffer.fromDebug:

1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
        case "binary":
            return ByteBuffer.fromBinary(buffer, littleEndian);
        case "utf8":
            return ByteBuffer.fromUTF8(buffer, littleEndian);
        case "debug":
            return ByteBuffer.fromDebug(buffer, littleEndian);
        default:
            throw Error("Unsupported encoding: "+encoding);
    }
}

How to use bytebuffer.fromHex:

1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
    encoding = "utf8";
switch (encoding) {
    case "base64":
        return ByteBuffer.fromBase64(buffer, littleEndian);
    case "hex":
        return ByteBuffer.fromHex(buffer, littleEndian);
    case "binary":
        return ByteBuffer.fromBinary(buffer, littleEndian);
    case "utf8":
        return ByteBuffer.fromUTF8(buffer, littleEndian);

How to use bytebuffer.zigZagDecode64:

2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
    ByteBufferPrototype.readVarint64ZigZag = function(offset) {
        var val = this.readVarint64(offset);
        if (val && val['value'] instanceof Long)
            val["value"] = ByteBuffer.zigZagDecode64(val["value"]);
        else
            val = ByteBuffer.zigZagDecode64(val);
        return val;
    };

} // Long

How to use bytebuffer.zigZagDecode32:

2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
ByteBufferPrototype.readVarint32ZigZag = function(offset) {
    var val = this.readVarint32(offset);
    if (typeof val === 'object')
        val["value"] = ByteBuffer.zigZagDecode32(val["value"]);
    else
        val = ByteBuffer.zigZagDecode32(val);
    return val;
};

// types/varints/varint64

How to use bytebuffer.fromBinary:

1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
case "base64":
    return ByteBuffer.fromBase64(buffer, littleEndian);
case "hex":
    return ByteBuffer.fromHex(buffer, littleEndian);
case "binary":
    return ByteBuffer.fromBinary(buffer, littleEndian);
case "utf8":
    return ByteBuffer.fromUTF8(buffer, littleEndian);
case "debug":
    return ByteBuffer.fromDebug(buffer, littleEndian);

How to use bytebuffer.calculateVarint32:

93
94
95
96
97
98
99
100
101
102
        throw TypeError("Illegal offset: " + offset + " (not an integer)");
    offset >>>= 0;
    if (offset < 0 || offset > this.buffer.length)
        throw RangeError("Illegal offset: 0 <= " + offset + " (+" + 0 + ") <= " + this.buffer.length);
}
let size = ByteBuffer.calculateVarint32(value),
    rotate = true,
    b;
offset += size;
let capacity10 = this.buffer.length;

How to use bytebuffer.DEFAULT_NOASSERT:

326
327
328
329
330
331
332
333
334
335
 * @expose
 */
var ByteBuffer = function(capacity, littleEndian, noAssert) {
    if (typeof capacity     === 'undefined') capacity     = ByteBuffer.DEFAULT_CAPACITY;
    if (typeof littleEndian === 'undefined') littleEndian = ByteBuffer.DEFAULT_ENDIAN;
    if (typeof noAssert     === 'undefined') noAssert     = ByteBuffer.DEFAULT_NOASSERT;
    if (!noAssert) {
        capacity = capacity | 0;
        if (capacity < 0)
            throw RangeError("Illegal capacity");

How to use bytebuffer.DEFAULT_ENDIAN:

325
326
327
328
329
330
331
332
333
334
 *  {@link ByteBuffer.DEFAULT_NOASSERT}.
 * @expose
 */
var ByteBuffer = function(capacity, littleEndian, noAssert) {
    if (typeof capacity     === 'undefined') capacity     = ByteBuffer.DEFAULT_CAPACITY;
    if (typeof littleEndian === 'undefined') littleEndian = ByteBuffer.DEFAULT_ENDIAN;
    if (typeof noAssert     === 'undefined') noAssert     = ByteBuffer.DEFAULT_NOASSERT;
    if (!noAssert) {
        capacity = capacity | 0;
        if (capacity < 0)

How to use bytebuffer.DEFAULT_CAPACITY:

324
325
326
327
328
329
330
331
332
333
 * @param {boolean=} noAssert Whether to skip assertions of offsets and values. Defaults to
 *  {@link ByteBuffer.DEFAULT_NOASSERT}.
 * @expose
 */
var ByteBuffer = function(capacity, littleEndian, noAssert) {
    if (typeof capacity     === 'undefined') capacity     = ByteBuffer.DEFAULT_CAPACITY;
    if (typeof littleEndian === 'undefined') littleEndian = ByteBuffer.DEFAULT_ENDIAN;
    if (typeof noAssert     === 'undefined') noAssert     = ByteBuffer.DEFAULT_NOASSERT;
    if (!noAssert) {
        capacity = capacity | 0;

How to use bytebuffer.fromBase64:

81
82
83
84
85
86
87
88
89
90
        } else {
            rawdata_caseflipped += rawdata.charAt(i);
        }
    }
    data_deLZd = LZString.decompressFromEncodedURIComponent(rawdata_caseflipped);
    databuffer = bytebuffer.fromBase64(data_deLZd);
    data = ISpsonpair.decode(databuffer.buffer)
    return data
},
stateEncode: function(rawdata) {

How to use bytebuffer.default:

245
246
247
248
249
250
251
252
253
254
// Diff: 128-159,164-166,168-175
TransactionImpl.prototype.getByteBuffer = function () {
    var size = this.getSize();
    if (this.isTestnet)
        size += 8;
    var buffer = bytebuffer_1.default.allocate(size).order(bytebuffer_1.default.LITTLE_ENDIAN);
    buffer.writeByte(this.type.getType());
    buffer.writeByte((this.version << 4) | this.type.getSubtype());
    buffer.writeInt(this.timestamp);
    buffer.writeShort(this.deadline);

How to use bytebuffer.wrap:

513
514
515
516
517
518
519
520
521
522
	// This is an enqueued connection closure
	this._handleConnectionClose(conn);
	return;
}

let buf = ByteBuffer.wrap(buffer, ByteBuffer.LITTLE_ENDIAN);

let rawEMsg = buf.readUint32();
let eMsg = rawEMsg & ~PROTO_MASK;
let isProtobuf = !!(rawEMsg & PROTO_MASK);

How to use bytebuffer.prototype:

106
107
108
109
110
111
112
113
114
115
},
mapDecode: function(rawdata) {
    bytebuffer.prototype.readBoolean = function() {
        return this.readByte() > 0
    }
    bytebuffer.prototype.readUTF = function() {
        return this.readString(this.readShort())
    }
    bytebuffer.prototype.readUint = bytebuffer.prototype.readUint32
    decodeFromDatabase = function(map) {

How to use bytebuffer.allocate:

843
844
845
846
847
848
849
850
851
852
853
854
855


function createMachineID(val_bb3, val_ff2, val_3b3) {
	// Machine IDs are binary KV objects with root key MessageObject and three hashes named BB3, FF2, and 3B3.
	// I don't feel like writing a proper BinaryKV serializer, so this will work fine.


	let buffer = ByteBuffer.allocate(155, ByteBuffer.LITTLE_ENDIAN);
	buffer.writeByte(0); // 1 byte, total 1
	buffer.writeCString("MessageObject"); // 14 bytes, total 15


	buffer.writeByte(1); // 1 byte, total 16