UNPKG

3.53 kBJavaScriptView Raw
1'use strict';
2
3var bufferUtil = require('../util/buffer');
4var assert = require('assert');
5
6var BufferWriter = function BufferWriter(obj) {
7 if (!(this instanceof BufferWriter))
8 return new BufferWriter(obj);
9 this.bufLen = 0;
10 if (obj)
11 this.set(obj);
12 else
13 this.bufs = [];
14};
15
16BufferWriter.prototype.set = function(obj) {
17 this.bufs = obj.bufs || this.bufs || [];
18 this.bufLen = this.bufs.reduce(function(prev, buf){ return prev + buf.length; }, 0);
19 return this;
20};
21
22BufferWriter.prototype.toBuffer = function() {
23 return this.concat();
24};
25
26BufferWriter.prototype.concat = function() {
27 return Buffer.concat(this.bufs, this.bufLen);
28};
29
30BufferWriter.prototype.write = function(buf) {
31 assert(bufferUtil.isBuffer(buf));
32 this.bufs.push(buf);
33 this.bufLen += buf.length;
34 return this;
35};
36
37BufferWriter.prototype.writeReverse = function(buf) {
38 assert(bufferUtil.isBuffer(buf));
39 this.bufs.push(bufferUtil.reverse(buf));
40 this.bufLen += buf.length;
41 return this;
42};
43
44BufferWriter.prototype.writeUInt8 = function(n) {
45 var buf = Buffer.alloc(1);
46 buf.writeUInt8(n, 0);
47 this.write(buf);
48 return this;
49};
50
51BufferWriter.prototype.writeUInt16BE = function(n) {
52 var buf = Buffer.alloc(2);
53 buf.writeUInt16BE(n, 0);
54 this.write(buf);
55 return this;
56};
57
58BufferWriter.prototype.writeUInt16LE = function(n) {
59 var buf = Buffer.alloc(2);
60 buf.writeUInt16LE(n, 0);
61 this.write(buf);
62 return this;
63};
64
65BufferWriter.prototype.writeUInt32BE = function(n) {
66 var buf = Buffer.alloc(4);
67 buf.writeUInt32BE(n, 0);
68 this.write(buf);
69 return this;
70};
71
72BufferWriter.prototype.writeInt32LE = function(n) {
73 var buf = Buffer.alloc(4);
74 buf.writeInt32LE(n, 0);
75 this.write(buf);
76 return this;
77};
78
79BufferWriter.prototype.writeUInt32LE = function(n) {
80 var buf = Buffer.alloc(4);
81 buf.writeUInt32LE(n, 0);
82 this.write(buf);
83 return this;
84};
85
86BufferWriter.prototype.writeUInt64BEBN = function(bn) {
87 var buf = bn.toBuffer({size: 8});
88 this.write(buf);
89 return this;
90};
91
92BufferWriter.prototype.writeUInt64LEBN = function(bn) {
93 var buf = bn.toBuffer({size: 8});
94 this.writeReverse(buf);
95 return this;
96};
97
98BufferWriter.prototype.writeVarintNum = function(n) {
99 var buf = BufferWriter.varintBufNum(n);
100 this.write(buf);
101 return this;
102};
103
104BufferWriter.prototype.writeVarintBN = function(bn) {
105 var buf = BufferWriter.varintBufBN(bn);
106 this.write(buf);
107 return this;
108};
109
110BufferWriter.varintBufNum = function(n) {
111 var buf = undefined;
112 if (n < 253) {
113 buf = Buffer.alloc(1);
114 buf.writeUInt8(n, 0);
115 } else if (n < 0x10000) {
116 buf = Buffer.alloc(1 + 2);
117 buf.writeUInt8(253, 0);
118 buf.writeUInt16LE(n, 1);
119 } else if (n < 0x100000000) {
120 buf = Buffer.alloc(1 + 4);
121 buf.writeUInt8(254, 0);
122 buf.writeUInt32LE(n, 1);
123 } else {
124 buf = Buffer.alloc(1 + 8);
125 buf.writeUInt8(255, 0);
126 buf.writeInt32LE(n & -1, 1);
127 buf.writeUInt32LE(Math.floor(n / 0x100000000), 5);
128 }
129 return buf;
130};
131
132BufferWriter.varintBufBN = function(bn) {
133 var buf = undefined;
134 var n = bn.toNumber();
135 if (n < 253) {
136 buf = Buffer.alloc(1);
137 buf.writeUInt8(n, 0);
138 } else if (n < 0x10000) {
139 buf = Buffer.alloc(1 + 2);
140 buf.writeUInt8(253, 0);
141 buf.writeUInt16LE(n, 1);
142 } else if (n < 0x100000000) {
143 buf = Buffer.alloc(1 + 4);
144 buf.writeUInt8(254, 0);
145 buf.writeUInt32LE(n, 1);
146 } else {
147 var bw = new BufferWriter();
148 bw.writeUInt8(255);
149 bw.writeUInt64LEBN(bn);
150 var buf = bw.concat();
151 }
152 return buf;
153};
154
155module.exports = BufferWriter;