| 1 |
#!/usr/bin/env node |
| 2 |
|
| 3 |
var program = require('commander'); |
| 4 |
var Bunzip = require('../'); |
| 5 |
var fs = require('fs'); |
| 6 |
|
| 7 |
program |
| 8 |
.version(Bunzip.version) |
| 9 |
.usage('[infile]') |
| 10 |
.option('-m, --multistream', |
| 11 |
'Read a multistream bzip2 file'); |
| 12 |
program.on('--help', function() { |
| 13 |
console.log(' If <infile> is omitted, reads from stdin.'); |
| 14 |
}); |
| 15 |
program.parse(process.argv); |
| 16 |
|
| 17 |
var makeInStream = function(in_fd) { |
| 18 |
var stat = fs.fstatSync(in_fd); |
| 19 |
var stream = { |
| 20 |
buffer: new Buffer(4096), |
| 21 |
totalPos: 0, |
| 22 |
pos: 0, |
| 23 |
end: 0, |
| 24 |
_fillBuffer: function() { |
| 25 |
this.end = fs.readSync(in_fd, this.buffer, 0, this.buffer.length); |
| 26 |
this.pos = 0; |
| 27 |
}, |
| 28 |
readByte: function() { |
| 29 |
if (this.pos >= this.end) { this._fillBuffer(); } |
| 30 |
if (this.pos < this.end) { |
| 31 |
this.totalPos++; |
| 32 |
return this.buffer[this.pos++]; |
| 33 |
} |
| 34 |
return -1; |
| 35 |
}, |
| 36 |
read: function(buffer, bufOffset, length) { |
| 37 |
if (this.pos >= this.end) { this._fillBuffer(); } |
| 38 |
var bytesRead = 0; |
| 39 |
while (bytesRead < length && this.pos < this.end) { |
| 40 |
buffer[bufOffset++] = this.buffer[this.pos++]; |
| 41 |
bytesRead++; |
| 42 |
} |
| 43 |
this.totalPos += bytesRead; |
| 44 |
return bytesRead; |
| 45 |
}, |
| 46 |
eof: function() { |
| 47 |
if (this.pos >= this.end) { this._fillBuffer(); } |
| 48 |
return !(this.pos < this.end); |
| 49 |
} |
| 50 |
}; |
| 51 |
if (stat.size) { |
| 52 |
stream.size = stat.size; |
| 53 |
} |
| 54 |
return stream; |
| 55 |
}; |
| 56 |
|
| 57 |
var in_fd = 0, close_in = function(){}; |
| 58 |
if (program.args.length > 0) { |
| 59 |
in_fd = fs.openSync(program.args.shift(), 'r'); |
| 60 |
close_in = function() { fs.closeSync(in_fd); }; |
| 61 |
} |
| 62 |
|
| 63 |
var inStream = makeInStream(in_fd); |
| 64 |
|
| 65 |
var report = function(position, blocksize) { |
| 66 |
console.log(position+'\t'+blocksize); |
| 67 |
}; |
| 68 |
|
| 69 |
Bunzip.table(inStream, report, program.multistream); |
| 70 |
close_in(); |
| 71 |
return 0; |