`options` can include `start` and `end` values to read a range of bytes from
the file instead of the entire file. Both `start` and `end` are inclusive and
-start at 0. When used, both the limits must be specified always.
+start at 0.
An example to read the last 10 bytes of a file which is 100 bytes long:
if (this.encoding) this.setEncoding(this.encoding);
- if (this.start !== undefined || this.end !== undefined) {
- if (this.start === undefined || this.end === undefined) {
- this.emit('error', new Error('Both start and end are needed ' +
- 'for range streaming.'));
- } else if (this.start > this.end) {
+ if (this.start !== undefined) {
+ if (this.end === undefined) {
+ this.end = Infinity;
+ }
+
+ if (this.start > this.end) {
this.emit('error', new Error('start must be <= end'));
} else {
this._firstRead = true;
assert.equal(contentRead, 'yz');
});
-try {
+var file5 = fs.createReadStream(rangeFile, {bufferSize: 1, start: 1});
+file5.data = '';
+file5.addListener('data', function(data) {
+ file5.data += data.toString('utf-8');
+});
+file5.addListener('end', function() {
+ assert.equal(file5.data, 'yz\n');
+});
+
+
+assert.throws(function() {
fs.createReadStream(rangeFile, {start: 10, end: 2});
- assert.fail('Creating a ReadStream with incorrect range limits must throw.');
-} catch (e) {
- assert.equal(e.message, 'start must be <= end');
-}
-
-try {
- fs.createReadStream(rangeFile, {start: 2});
- assert.fail('Creating a ReadStream with a only one range limits must throw.');
-} catch (e) {
- assert.equal(e.message, 'Both start and end are needed for range streaming.');
-}
+}, /start must be <= end/);
var stream = fs.createReadStream(rangeFile, { start: 0, end: 0 });
stream.data = '';