-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriter.js
More file actions
34 lines (29 loc) · 715 Bytes
/
writer.js
File metadata and controls
34 lines (29 loc) · 715 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* [Writer description]
*/
function BufferWriter(){
this.buffer = [];
};
/**
* [write description]
* @param {[type]} d [description]
* @param {[type]} size [description]
* @return {[type]} [description]
*/
BufferWriter.prototype.write = function(d, size){
for(var i = 0; i < size; i++)
this.buffer.push((d & Math.pow(2, size - i - 1 )) ? 1 : 0);
};
/**
* [toBuffer description]
* @return {[type]} [description]
*/
BufferWriter.prototype.toBuffer = function(){
var arr = [];
for(var i = 0; i < this.buffer.length; i += 8){
var chunk = this.buffer.slice(i, i + 8);
arr.push(parseInt(chunk.join(''), 2));
}
return Buffer.from(arr);
};
module.exports = BufferWriter;