forked from nacholibre/node-readlines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readlines.js
173 lines (123 loc) · 4.01 KB
/
readlines.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict';
var fs = require('fs');
function LineByLine(file, options) {
options = options || {};
if (!options.readChunk) {
options.readChunk = 1024;
}
if (!options.newLineCharacter) {
options.newLineCharacter = 0x0a; //linux line ending
} else {
options.newLineCharacter = options.newLineCharacter.charCodeAt(0);
}
if (typeof file === 'number') {
this.fd = file;
} else {
this.fd = fs.openSync(file, 'r');
}
this.options = options;
this.newLineCharacter = options.newLineCharacter;
this.reset();
}
LineByLine.prototype._searchInBuffer = function(buffer, hexNeedle) {
var found = -1;
for (var i = 0; i <= buffer.length; i++) {
var b_byte = buffer[i];
if (b_byte === hexNeedle) {
found = i;
break;
}
}
return found;
};
LineByLine.prototype.reset = function() {
this.bufferData = null;
this.bytesRead = 0;
this.bufferPosition = 0;
this.eofReached = false;
this.line = '';
this.linesCache = [];
this.lastBytePosition = null;
this.fdPosition = 0;
};
LineByLine.prototype._extractLines = function(buffer) {
var line;
var lines = [];
var bufferPosition = 0;
var lastNewLineBufferPosition = 0;
while (true) {
var bufferPositionValue = buffer[bufferPosition++];
if (bufferPositionValue === this.newLineCharacter) {
line = buffer.slice(lastNewLineBufferPosition, bufferPosition);
lines.push(line);
lastNewLineBufferPosition = bufferPosition;
} else if (!bufferPositionValue) {
break;
}
}
var leftovers = buffer.slice(lastNewLineBufferPosition, bufferPosition);
if (leftovers.length) {
lines.push(leftovers);
}
return lines;
};
LineByLine.prototype._readChunk = function(lineLeftovers) {
var bufferData = new Buffer(this.options.readChunk);
var totalBytesRead = 0;
var bytesRead = fs.readSync(this.fd, bufferData, 0, this.options.readChunk, this.fdPosition);
totalBytesRead = totalBytesRead + bytesRead;
this.fdPosition = this.fdPosition + bytesRead;
var buffers = [];
buffers.push(bufferData);
var lastBuffer = buffers[buffers.length-1];
while(this._searchInBuffer(buffers[buffers.length-1], this.options.newLineCharacter) === -1) {
//new line character doesn't exist in the readed data, so we must read
//again
var newBuffer = new Buffer(this.options.readChunk);
var bytesRead = fs.readSync(this.fd, newBuffer, 0, this.options.readChunk, this.fdPosition);
totalBytesRead = totalBytesRead + bytesRead;
this.fdPosition = this.fdPosition + bytesRead;
buffers.push(newBuffer);
}
bufferData = Buffer.concat(buffers);
if (bytesRead < this.options.readChunk) {
this.eofReached = true;
bufferData = bufferData.slice(0, totalBytesRead);
}
if (bytesRead) {
this.linesCache = this._extractLines(bufferData);
if (lineLeftovers) {
this.linesCache[0] = Buffer.concat([lineLeftovers, this.linesCache[0]]);
}
}
return totalBytesRead;
};
LineByLine.prototype.next = function() {
var line = false;
if (this.eofReached && this.linesCache.length === 0) {
return line;
}
var bytesRead;
if (!this.linesCache.length) {
bytesRead = this._readChunk();
}
if (this.linesCache.length) {
line = this.linesCache.shift();
var lastLineCharacter = line[line.length-1];
if (lastLineCharacter !== 0x0a) {
bytesRead = this._readChunk(line);
if (bytesRead) {
line = this.linesCache.shift();
}
}
}
if (this.eofReached && this.linesCache.length === 0) {
fs.closeSync(this.fd);
this.fd = null;
}
if (line && line[line.length-1] === this.newLineCharacter) {
line = line.slice(0, line.length-1);
}
return line;
};
module.exports = LineByLine;