forked from antimatter15/bzip2.js
-
Notifications
You must be signed in to change notification settings - Fork 4
/
node-test.js
122 lines (108 loc) · 2.78 KB
/
node-test.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var fs = require('fs');
var bz2 = require('./bzip2.js');
var bufferSize = 1;
var reader = fs.createReadStream('test.txt.bz2', { bufferSize: bufferSize });
// var blockSize;
// reader.on('data', function (data) {
// var bitReader = bz2.array(data);
// if (!blockSize) {
// blockSize = bz2.header(bitReader);
// }
// console.log( bz2.decompress(bitReader, blockSize) );
// });
// var bit = 0, readBytes = 0;
// var BITMASK = [0, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF ];
// var buffer = new Buffer(0)
// var hasBytes = 0;
// var blockOffset = 0;
// var blockSize = 0;
// function bitReader(n){
// var result = 0;
// while(n > 0){
// var left = 8 - bit;
// var currentByte = buffer[readBytes - blockOffset];
// if(n >= left){
// result <<= left;
// result |= (BITMASK[left] & currentByte);
// readBytes++;
// bit = 0;
// n -= left;
// }else{
// result <<= n;
// result |= ((currentByte & (BITMASK[n] << (8 - n - bit))) >> (8 - n - bit));
// bit += n;
// n = 0;
// }
// }
// return result;
// }
// reader.on('data', function (data) {
// var newData = data.length;
// blockOffset = readBytes;
// var newBuffer = new Buffer(hasBytes - readBytes + newData); //create a buffer for all the unread bytes
// buffer.copy(newBuffer, 0, hasBytes - readBytes);
// data.copy(newBuffer, hasBytes - readBytes);
// buffer = newBuffer;
// hasBytes += data.length;
// if(hasBytes - readBytes > blockSize){
// if(!blockSize){
// blockSize = bz2.header(bitReader);
// }else{
// bz2.decompress(bitReader, blockSize)
// }
// }
// })
var bit = 0, readBytes = 0, readOffset = 0;
var BITMASK = [0, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF ];
var bufferQueue = [];
var buffer = [];
function bitReader(n){
var result = 0;
while(n > 0){
var left = 8 - bit;
if(buffer.length == readBytes - readOffset){
readOffset = readBytes;
buffer = bufferQueue.shift();
}
var currentByte = buffer[readBytes - readOffset];
if(n >= left){
result <<= left;
result |= (BITMASK[left] & currentByte);
readBytes++;
bit = 0;
n -= left;
}else{
result <<= n;
result |= ((currentByte & (BITMASK[n] << (8 - n - bit))) >> (8 - n - bit));
bit += n;
n = 0;
}
}
return result;
}
var blockSize = 0;
function decompressBlock(){
if(!blockSize){
blockSize = bz2.header(bitReader);
console.log("got header of", blockSize)
}else{
var chunk = bz2.decompress(bitReader, blockSize);
if(chunk == -1){
console.log('done')
}else{
console.log(chunk)
}
}
}
var hasBytes = 0;
reader.on('data', function (data) {
bufferQueue.push(data);
hasBytes += data.length;
if(hasBytes - readBytes > (100000 * blockSize || 10)){
console.log('decompressing with ', hasBytes)
decompressBlock()
}
})
reader.on('end', function (data) {
decompressBlock()
})