flocore/lib/bufferwriter.js
Ryan X. Charles 9b8ce05b15 BufferWriter().toBuffer convenience method
It does the same thing as .concat(), but may be easier to remember, since the
rest of the library uses the ".toBuffer()" convention
2014-09-22 17:09:53 -07:00

136 lines
2.9 KiB
JavaScript

var BN = require('./bn');
var BufferWriter = function BufferWriter(obj) {
if (!(this instanceof BufferWriter))
return new BufferWriter(obj);
if (obj)
this.set(obj);
else
this.bufs = [];
};
BufferWriter.prototype.set = function(obj) {
this.bufs = obj.bufs || this.bufs || [];
return this;
};
BufferWriter.prototype.toBuffer = function() {
return this.concat();
};
BufferWriter.prototype.concat = function() {
return Buffer.concat(this.bufs);
};
BufferWriter.prototype.write = function(buf) {
this.bufs.push(buf);
return this;
};
BufferWriter.prototype.writeUInt8 = function(n) {
var buf = new Buffer(1);
buf.writeUInt8(n, 0);
this.write(buf);
return this;
};
BufferWriter.prototype.writeUInt16BE = function(n) {
var buf = new Buffer(2);
buf.writeUInt16BE(n, 0);
this.write(buf);
return this;
};
BufferWriter.prototype.writeUInt16LE = function(n) {
var buf = new Buffer(2);
buf.writeUInt16LE(n, 0);
this.write(buf);
return this;
};
BufferWriter.prototype.writeUInt32BE = function(n) {
var buf = new Buffer(4);
buf.writeUInt32BE(n, 0);
this.write(buf);
return this;
};
BufferWriter.prototype.writeUInt32LE = function(n) {
var buf = new Buffer(4);
buf.writeUInt32LE(n, 0);
this.write(buf);
return this;
};
BufferWriter.prototype.writeUInt64BEBN = function(bn) {
var buf = bn.toBuffer({size: 8});
this.write(buf);
return this;
};
BufferWriter.prototype.writeUInt64LEBN = function(bn) {
var buf = bn.toBuffer({size: 8});
var reversebuf = new Buffer(Array.apply(new Array(), buf).reverse());
this.write(reversebuf);
return this;
};
BufferWriter.prototype.writeVarintNum = function(n) {
var buf = BufferWriter.varintBufNum(n);
this.write(buf);
return this;
};
BufferWriter.prototype.writeVarintBN = function(bn) {
var buf = BufferWriter.varintBufBN(bn);
this.write(buf);
return this;
};
BufferWriter.varintBufNum = function(n) {
var buf = undefined;
if (n < 253) {
buf = new Buffer(1);
buf.writeUInt8(n, 0);
} else if (n < 0x10000) {
buf = new Buffer(1 + 2);
buf.writeUInt8(253, 0);
buf.writeUInt16LE(n, 1);
} else if (n < 0x100000000) {
buf = new Buffer(1 + 4);
buf.writeUInt8(254, 0);
buf.writeUInt32LE(n, 1);
} else {
buf = new Buffer(1 + 8);
buf.writeUInt8(255, 0);
buf.writeInt32LE(n & -1, 1);
buf.writeUInt32LE(Math.floor(n / 0x100000000), 5);
}
return buf;
};
BufferWriter.varintBufBN = function(bn) {
var buf = undefined;
var n = bn.toNumber();
if (n < 253) {
buf = new Buffer(1);
buf.writeUInt8(n, 0);
} else if (n < 0x10000) {
buf = new Buffer(1 + 2);
buf.writeUInt8(253, 0);
buf.writeUInt16LE(n, 1);
} else if (n < 0x100000000) {
buf = new Buffer(1 + 4);
buf.writeUInt8(254, 0);
buf.writeUInt32LE(n, 1);
} else {
var bw = new BufferWriter();
bw.writeUInt8(255);
bw.writeUInt64LEBN(bn);
var buf = bw.concat();
}
return buf;
};
module.exports = BufferWriter;