Skip to content

Commit e7d4ef0

Browse files
committed
Support read(0)
1 parent e5f2c2a commit e7d4ef0

File tree

3 files changed

+49
-44
lines changed

3 files changed

+49
-44
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

lib/index.js

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ module.exports.open = function (p, options){
1212

1313
var Reader = function (path, options){
1414
events.EventEmitter.call (this);
15-
15+
1616
options = options || {};
1717
if (options.highWaterMark < 1){
1818
throw new Error ("Invalid highWaterMark");
1919
}
2020
this._highWaterMark = options.highWaterMark || 16384;
21-
21+
2222
this._path = path;
2323
this._fd = null;
2424
this._p = 0;
@@ -32,9 +32,9 @@ var Reader = function (path, options){
3232
this._bufferMode = null;
3333
this._readFile = false;
3434
this._cancelled = false;
35-
35+
3636
var me = this;
37-
37+
3838
this._q = dq ().on ("error", function (error){
3939
if (!me._fd){
4040
me._q = null;
@@ -54,15 +54,15 @@ util.inherits (Reader, events.EventEmitter);
5454

5555
Reader.prototype._open = function (cb){
5656
var me = this;
57-
57+
5858
var open = function (){
5959
fs.open (me._path, "r", function (error, fd){
6060
if (error) return cb (error);
6161
me._fd = fd;
6262
cb ();
6363
});
6464
};
65-
65+
6666
if (this._size === null){
6767
this._stats (function (error){
6868
if (error) return cb (error);
@@ -88,15 +88,15 @@ Reader.prototype._stats = function (cb){
8888

8989
Reader.prototype.cancel = function (err){
9090
if (!this._q) throw new Error ("The reader is closed");
91-
91+
9292
this._cancelled = true;
9393
this._q.pause ();
94-
94+
9595
if (!this._fd){
9696
this._q = null;
9797
return err ? this.emit ("error", err) : this.emit ("close");
9898
}
99-
99+
100100
var me = this;
101101
fs.close (this._fd, function (error){
102102
me._fd = null;
@@ -114,9 +114,9 @@ Reader.prototype.cancel = function (err){
114114

115115
Reader.prototype.close = function (){
116116
if (!this._q) throw new Error ("The reader is closed");
117-
117+
118118
var me = this;
119-
119+
120120
this._q.push (function (done){
121121
if (!me._fd){
122122
me._q = null;
@@ -139,47 +139,47 @@ Reader.prototype.close = function (){
139139
me.emit ("close");
140140
}
141141
});
142-
142+
143143
return this;
144144
};
145145

146146
Reader.prototype.isEOF = function (){
147147
if (!this._q) throw new Error ("The reader is closed");
148-
148+
149149
return this._size !== null && this._p >= this._size;
150150
};
151151

152152
Reader.prototype._dump = function (target, offset, start, end, cb){
153153
var me = this;
154-
154+
155155
var reads = Math.ceil ((end - start)/this._highWaterMark);
156156
var last = (end - start)%this._highWaterMark || this._highWaterMark;
157-
157+
158158
(function read (reads){
159159
if (reads === 1){
160160
//Read to the buffer and copy to the target
161161
fs.read (me._fd, me._b, 0, me._highWaterMark, start,
162162
function (error, bytesRead){
163163
if (error) return cb (error);
164-
164+
165165
//Update the buffer limits
166166
me._s = start;
167167
me._e = start + bytesRead;
168-
168+
169169
//Fill the target buffer
170170
me._b.copy (target, offset, 0, last);
171-
171+
172172
cb ();
173173
});
174174
}else{
175175
//Read to the target
176176
fs.read (me._fd, target, offset, me._highWaterMark, start,
177177
function (error, bytesRead){
178178
if (error) return cb (error);
179-
179+
180180
offset += bytesRead;
181181
start += bytesRead;
182-
182+
183183
read (reads - 1);
184184
});
185185
}
@@ -188,24 +188,24 @@ Reader.prototype._dump = function (target, offset, start, end, cb){
188188

189189
Reader.prototype._read = function (bytes, cb){
190190
var me = this;
191-
191+
192192
//Trim the number of bytes to read
193193
if (this._p + bytes >= this._size){
194194
bytes = this._size - this._p;
195195
}
196-
196+
197197
var target = new Buffer (bytes);
198-
198+
199199
if (!this._bufferMode){
200200
//File size <= buffer size
201201
if (!this._b) this._b = new Buffer (this._size);
202-
202+
203203
var read = function (){
204204
me._b.copy (target, 0, me._p, me._p + bytes);
205205
me._p += bytes;
206206
cb (null, bytes, target);
207207
};
208-
208+
209209
if (!this._readFile){
210210
//Read all the file
211211
fs.read (this._fd, this._b, 0, this._size, 0, function (error){
@@ -220,13 +220,13 @@ Reader.prototype._read = function (bytes, cb){
220220
}else{
221221
//File size > buffer size
222222
if (!this._b) this._b = new Buffer (this._highWaterMark);
223-
223+
224224
var s = this._p;
225225
var e = this._p + bytes;
226226
//Check whether the limits are inside the buffer
227227
var is = s >= this._s && s < this._e;
228228
var ie = e > this._s && e <= this._e;
229-
229+
230230
if (is && ie){
231231
//Case 1
232232
//The bytes to read are already in the buffer
@@ -235,7 +235,7 @@ Reader.prototype._read = function (bytes, cb){
235235
cb (null, bytes, target);
236236
}else if (!is && !ie){
237237
if (this._s >= s && this._s < e && this._e > s && this._e <= e){
238-
238+
239239
//Case 5
240240
//The buffer is inside the requested bytes
241241
//Copy the bytes already in the buffer
@@ -287,10 +287,10 @@ Reader.prototype._read = function (bytes, cb){
287287

288288
Reader.prototype.read = function (bytes, cb){
289289
if (!this._q) throw new Error ("The reader is closed");
290-
if (~~bytes < 1) throw new Error ("Must read one or more bytes");
291-
290+
if (~~bytes < 0) throw new Error ("Cannot read a negative number of bytes");
291+
292292
var me = this;
293-
293+
294294
this._q.push (function (done){
295295
//Fast case
296296
if (me.isEOF ()) return done (null, 0, new Buffer (0));
@@ -317,42 +317,42 @@ Reader.prototype.read = function (bytes, cb){
317317
}
318318
}
319319
});
320-
320+
321321
return this;
322322
};
323323

324324
Reader.prototype._seek = function (offset, whence, cb){
325325
if (!whence){
326326
whence = { start: true };
327327
}
328-
328+
329329
if (whence.start){
330330
this._p = offset;
331331
}else if (whence.current){
332332
this._p += offset;
333333
}else if (whence.end){
334334
this._p = this._size - 1 - offset;
335335
}
336-
336+
337337
//An offset beyond the size - 1 limit will always return 0 bytes read, no need
338338
//to check and return an error
339339
if (this._p < 0){
340340
return cb (new Error ("The seek pointer must contain a positive value"));
341341
}
342-
342+
343343
cb ();
344344
};
345345

346346
Reader.prototype.seek = function (offset, whence, cb){
347347
if (!this._q) throw new Error ("The reader is closed");
348-
348+
349349
var me = this;
350-
350+
351351
if (arguments.length === 2 && typeof whence === "function"){
352352
cb = whence;
353353
whence = null;
354354
}
355-
355+
356356
this._q.push (function (done){
357357
if (me._size === null){
358358
me._stats (function (error){
@@ -377,18 +377,18 @@ Reader.prototype.seek = function (offset, whence, cb){
377377
}
378378
}
379379
});
380-
380+
381381
return this;
382382
};
383383

384384
Reader.prototype.size = function (){
385385
if (!this._q) throw new Error ("The reader is closed");
386-
386+
387387
return this._size;
388388
};
389389

390390
Reader.prototype.tell = function (){
391391
if (!this._q) throw new Error ("The reader is closed");
392-
392+
393393
return this._p;
394-
};
394+
};

test/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ var tests = {
4242
assert.strictEqual (bytesRead, 3);
4343
assert.deepEqual (buffer, new Buffer ([0, 1, 2]));
4444
})
45+
.read (0, function (bytesRead, buffer){
46+
assert.strictEqual (bytesRead, 0);
47+
assert.deepEqual (buffer, new Buffer ([]));
48+
})
4549
.close ();
4650
},
4751
"case 2": function (done){
@@ -243,7 +247,7 @@ var tests = {
243247
br.open (file)
244248
.on ("error", function (error){
245249
assert.ok (error);
246-
250+
247251
br.open (file)
248252
.on ("error", function (error){
249253
assert.ok (error);
@@ -326,4 +330,4 @@ var keysLength = keys.length;
326330
again (i + 1);
327331
}
328332
}
329-
})(0);
333+
})(0);

0 commit comments

Comments
 (0)