@@ -12,13 +12,13 @@ module.exports.open = function (p, options){
12
12
13
13
var Reader = function ( path , options ) {
14
14
events . EventEmitter . call ( this ) ;
15
-
15
+
16
16
options = options || { } ;
17
17
if ( options . highWaterMark < 1 ) {
18
18
throw new Error ( "Invalid highWaterMark" ) ;
19
19
}
20
20
this . _highWaterMark = options . highWaterMark || 16384 ;
21
-
21
+
22
22
this . _path = path ;
23
23
this . _fd = null ;
24
24
this . _p = 0 ;
@@ -32,9 +32,9 @@ var Reader = function (path, options){
32
32
this . _bufferMode = null ;
33
33
this . _readFile = false ;
34
34
this . _cancelled = false ;
35
-
35
+
36
36
var me = this ;
37
-
37
+
38
38
this . _q = dq ( ) . on ( "error" , function ( error ) {
39
39
if ( ! me . _fd ) {
40
40
me . _q = null ;
@@ -54,15 +54,15 @@ util.inherits (Reader, events.EventEmitter);
54
54
55
55
Reader . prototype . _open = function ( cb ) {
56
56
var me = this ;
57
-
57
+
58
58
var open = function ( ) {
59
59
fs . open ( me . _path , "r" , function ( error , fd ) {
60
60
if ( error ) return cb ( error ) ;
61
61
me . _fd = fd ;
62
62
cb ( ) ;
63
63
} ) ;
64
64
} ;
65
-
65
+
66
66
if ( this . _size === null ) {
67
67
this . _stats ( function ( error ) {
68
68
if ( error ) return cb ( error ) ;
@@ -88,15 +88,15 @@ Reader.prototype._stats = function (cb){
88
88
89
89
Reader . prototype . cancel = function ( err ) {
90
90
if ( ! this . _q ) throw new Error ( "The reader is closed" ) ;
91
-
91
+
92
92
this . _cancelled = true ;
93
93
this . _q . pause ( ) ;
94
-
94
+
95
95
if ( ! this . _fd ) {
96
96
this . _q = null ;
97
97
return err ? this . emit ( "error" , err ) : this . emit ( "close" ) ;
98
98
}
99
-
99
+
100
100
var me = this ;
101
101
fs . close ( this . _fd , function ( error ) {
102
102
me . _fd = null ;
@@ -114,9 +114,9 @@ Reader.prototype.cancel = function (err){
114
114
115
115
Reader . prototype . close = function ( ) {
116
116
if ( ! this . _q ) throw new Error ( "The reader is closed" ) ;
117
-
117
+
118
118
var me = this ;
119
-
119
+
120
120
this . _q . push ( function ( done ) {
121
121
if ( ! me . _fd ) {
122
122
me . _q = null ;
@@ -139,47 +139,47 @@ Reader.prototype.close = function (){
139
139
me . emit ( "close" ) ;
140
140
}
141
141
} ) ;
142
-
142
+
143
143
return this ;
144
144
} ;
145
145
146
146
Reader . prototype . isEOF = function ( ) {
147
147
if ( ! this . _q ) throw new Error ( "The reader is closed" ) ;
148
-
148
+
149
149
return this . _size !== null && this . _p >= this . _size ;
150
150
} ;
151
151
152
152
Reader . prototype . _dump = function ( target , offset , start , end , cb ) {
153
153
var me = this ;
154
-
154
+
155
155
var reads = Math . ceil ( ( end - start ) / this . _highWaterMark ) ;
156
156
var last = ( end - start ) % this . _highWaterMark || this . _highWaterMark ;
157
-
157
+
158
158
( function read ( reads ) {
159
159
if ( reads === 1 ) {
160
160
//Read to the buffer and copy to the target
161
161
fs . read ( me . _fd , me . _b , 0 , me . _highWaterMark , start ,
162
162
function ( error , bytesRead ) {
163
163
if ( error ) return cb ( error ) ;
164
-
164
+
165
165
//Update the buffer limits
166
166
me . _s = start ;
167
167
me . _e = start + bytesRead ;
168
-
168
+
169
169
//Fill the target buffer
170
170
me . _b . copy ( target , offset , 0 , last ) ;
171
-
171
+
172
172
cb ( ) ;
173
173
} ) ;
174
174
} else {
175
175
//Read to the target
176
176
fs . read ( me . _fd , target , offset , me . _highWaterMark , start ,
177
177
function ( error , bytesRead ) {
178
178
if ( error ) return cb ( error ) ;
179
-
179
+
180
180
offset += bytesRead ;
181
181
start += bytesRead ;
182
-
182
+
183
183
read ( reads - 1 ) ;
184
184
} ) ;
185
185
}
@@ -188,24 +188,24 @@ Reader.prototype._dump = function (target, offset, start, end, cb){
188
188
189
189
Reader . prototype . _read = function ( bytes , cb ) {
190
190
var me = this ;
191
-
191
+
192
192
//Trim the number of bytes to read
193
193
if ( this . _p + bytes >= this . _size ) {
194
194
bytes = this . _size - this . _p ;
195
195
}
196
-
196
+
197
197
var target = new Buffer ( bytes ) ;
198
-
198
+
199
199
if ( ! this . _bufferMode ) {
200
200
//File size <= buffer size
201
201
if ( ! this . _b ) this . _b = new Buffer ( this . _size ) ;
202
-
202
+
203
203
var read = function ( ) {
204
204
me . _b . copy ( target , 0 , me . _p , me . _p + bytes ) ;
205
205
me . _p += bytes ;
206
206
cb ( null , bytes , target ) ;
207
207
} ;
208
-
208
+
209
209
if ( ! this . _readFile ) {
210
210
//Read all the file
211
211
fs . read ( this . _fd , this . _b , 0 , this . _size , 0 , function ( error ) {
@@ -220,13 +220,13 @@ Reader.prototype._read = function (bytes, cb){
220
220
} else {
221
221
//File size > buffer size
222
222
if ( ! this . _b ) this . _b = new Buffer ( this . _highWaterMark ) ;
223
-
223
+
224
224
var s = this . _p ;
225
225
var e = this . _p + bytes ;
226
226
//Check whether the limits are inside the buffer
227
227
var is = s >= this . _s && s < this . _e ;
228
228
var ie = e > this . _s && e <= this . _e ;
229
-
229
+
230
230
if ( is && ie ) {
231
231
//Case 1
232
232
//The bytes to read are already in the buffer
@@ -235,7 +235,7 @@ Reader.prototype._read = function (bytes, cb){
235
235
cb ( null , bytes , target ) ;
236
236
} else if ( ! is && ! ie ) {
237
237
if ( this . _s >= s && this . _s < e && this . _e > s && this . _e <= e ) {
238
-
238
+
239
239
//Case 5
240
240
//The buffer is inside the requested bytes
241
241
//Copy the bytes already in the buffer
@@ -287,10 +287,10 @@ Reader.prototype._read = function (bytes, cb){
287
287
288
288
Reader . prototype . read = function ( bytes , cb ) {
289
289
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 ( "Must read 0 or more bytes" ) ;
291
+
292
292
var me = this ;
293
-
293
+
294
294
this . _q . push ( function ( done ) {
295
295
//Fast case
296
296
if ( me . isEOF ( ) ) return done ( null , 0 , new Buffer ( 0 ) ) ;
@@ -317,42 +317,42 @@ Reader.prototype.read = function (bytes, cb){
317
317
}
318
318
}
319
319
} ) ;
320
-
320
+
321
321
return this ;
322
322
} ;
323
323
324
324
Reader . prototype . _seek = function ( offset , whence , cb ) {
325
325
if ( ! whence ) {
326
326
whence = { start : true } ;
327
327
}
328
-
328
+
329
329
if ( whence . start ) {
330
330
this . _p = offset ;
331
331
} else if ( whence . current ) {
332
332
this . _p += offset ;
333
333
} else if ( whence . end ) {
334
334
this . _p = this . _size - 1 - offset ;
335
335
}
336
-
336
+
337
337
//An offset beyond the size - 1 limit will always return 0 bytes read, no need
338
338
//to check and return an error
339
339
if ( this . _p < 0 ) {
340
340
return cb ( new Error ( "The seek pointer must contain a positive value" ) ) ;
341
341
}
342
-
342
+
343
343
cb ( ) ;
344
344
} ;
345
345
346
346
Reader . prototype . seek = function ( offset , whence , cb ) {
347
347
if ( ! this . _q ) throw new Error ( "The reader is closed" ) ;
348
-
348
+
349
349
var me = this ;
350
-
350
+
351
351
if ( arguments . length === 2 && typeof whence === "function" ) {
352
352
cb = whence ;
353
353
whence = null ;
354
354
}
355
-
355
+
356
356
this . _q . push ( function ( done ) {
357
357
if ( me . _size === null ) {
358
358
me . _stats ( function ( error ) {
@@ -377,18 +377,18 @@ Reader.prototype.seek = function (offset, whence, cb){
377
377
}
378
378
}
379
379
} ) ;
380
-
380
+
381
381
return this ;
382
382
} ;
383
383
384
384
Reader . prototype . size = function ( ) {
385
385
if ( ! this . _q ) throw new Error ( "The reader is closed" ) ;
386
-
386
+
387
387
return this . _size ;
388
388
} ;
389
389
390
390
Reader . prototype . tell = function ( ) {
391
391
if ( ! this . _q ) throw new Error ( "The reader is closed" ) ;
392
-
392
+
393
393
return this . _p ;
394
- } ;
394
+ } ;
0 commit comments