Permalink
Browse files

Start using nsISeekableStream for setting stream positions.

  • Loading branch information...
1 parent 4ea2878 commit 0d3e8cb378e8c0ec23d2b9cb64e867e22f5887b6 @Gozala committed May 12, 2011
Showing with 12 additions and 3 deletions.
  1. +12 −3 lib/fs.js
View
@@ -118,11 +118,14 @@ const ReadStream = Stream.extend({
this.bufferSize = options.bufferSize;
if ('length' in options && options.length)
this.length = options.length;
- if ('position' in options && options.position)
+ if ('position' in options && options.position !== undefined)
this.position = options.position;
let { flags, mode, position, length } = this;
let { input } = _(isString(path) ? openSync(path, flags, mode) : path);
+ // Setting a stream position, unless it's `-1` which means current position.
+ if (position >= 0)
+ input.QueryInterface(Ci.nsISeekableStream).seek(NS_SEEK_SET, position);
// We use `nsIStreamTransportService` service to transform blocking
// file input stream into a fully asynchronous stream that can be written
// without blocking the main thread.
@@ -132,10 +135,10 @@ const ReadStream = Stream.extend({
// count.
let asyncInputStream = transport.openInputStream(null, 0, 0);
_(this).asyncInputStream = asyncInputStream;
- let pump = StreamPump(asyncInputStream, position, length, 0, 0, false);
- _(this).pump = pump;
let binaryInputStream = BinaryInputStream(asyncInputStream);
_(this).binaryInputStream = binaryInputStream;
+ let pump = StreamPump(asyncInputStream, position, length, 0, 0, false);
+ _(this).pump = pump;
this.read();
},
path: null,
@@ -186,6 +189,8 @@ const ReadStream = Stream.extend({
delete _(this).pump;
_(this).binaryInputStream.close();
delete _(this).binaryInputStream;
+ _(this).asyncInputStream.close();
+ delete _(this).asyncInputStream;
} catch (error) {
this.emit("error", error);
}
@@ -725,6 +730,10 @@ exports.read = function read(fd, buffer, offset, length, position, callback) {
callback(null, bytesRead, buffer);
}
});
+ readStream.on("end", function onEnd() {
+ readStream.destroy();
+ console.log('The end!')
+ });
};
/**

0 comments on commit 0d3e8cb

Please sign in to comment.