-
Notifications
You must be signed in to change notification settings - Fork 36
/
file_reader.js
85 lines (70 loc) · 2.09 KB
/
file_reader.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
let path = require("path");
let fs = require("fs");
let readline = require("readline");
let zlib = require("zlib");
class FileReader{
constructor(){
this.filePath_ = "";
this.readStream_ = null;
this.readIF_ = null;
this.fileSize_ = 0;
this.complete_ = false;
}
/**
* Open a file
* @param {string} filePath - a file path
*/
open(filePath){
let stat = fs.statSync(filePath);
if (!stat) {
throw "Failed to fs.statSync(). There seems to be no file.";
}
else{
this.fileSize_ = stat.size;
}
this.filePath_ = filePath;
// GZip の chunk size と合わせて,少し増やすと2割ぐらい速くなる
let rs = fs.createReadStream(filePath, {highWaterMark: 1024*64});
this.readStream_ = rs; // 読み出し量はファイルサイズ基準なので,こっちをセット
if (this.getExtension() == ".gz") {
let gzipRS = rs.pipe(zlib.createGunzip({chunkSize: 1024*32}));
this.readIF_ = readline.createInterface({"input": gzipRS});
}
else {
this.readIF_ = readline.createInterface({"input": rs});
}
}
close(){
if (this.readIF_){
this.readIF_.close();
this.readIF_ = null;
}
if (this.readStream_) {
this.readStream_.destroy();
this.readStream_ = null;
}
}
getPath(){
return this.filePath_;
}
/**
* Open a file
* @param {function(string): void} read - Called when a line is read
* @param {function(string): void} finish - Called when all lines have been read
*/
readlines(read, finish){
this.readIF_.on("line", read);
this.readIF_.on("close", finish);
}
get fileSize(){
return this.fileSize_;
}
get bytesRead(){
return this.readStream_ ? this.readStream_.bytesRead : 0;
}
getExtension(){
let ext = path.extname(this.filePath_);
return ext;
}
}
module.exports.FileReader = FileReader;