Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Unified Diff: sdk/lib/io/file_impl.dart

Issue 15547003: Add 'start' and 'end' optional arguments to File.openRead(). This makes it possible to stream a sub… (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: aFix comments. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/io/file.dart ('k') | tests/standalone/io/file_input_stream_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/io/file_impl.dart
diff --git a/sdk/lib/io/file_impl.dart b/sdk/lib/io/file_impl.dart
index 15fbded0b3e2c4d7e876ec0243a07a9c534dfdbb..2057234ac7af8da9ca6804232adac3834fb75a92 100644
--- a/sdk/lib/io/file_impl.dart
+++ b/sdk/lib/io/file_impl.dart
@@ -16,6 +16,7 @@ class _FileStream extends Stream<List<int>> {
String _path;
RandomAccessFile _openedFile;
int _position;
+ int _end;
// Has the stream been paused or unsubscribed?
bool _paused = false;
@@ -27,7 +28,7 @@ class _FileStream extends Stream<List<int>> {
// Block read but not yet send because stream is paused.
List<int> _currentBlock;
- _FileStream(String this._path) : _position = 0 {
+ _FileStream(String this._path, this._position, this._end) {
_setupController();
}
@@ -71,7 +72,14 @@ class _FileStream extends Stream<List<int>> {
// Don't start a new read if one is already in progress.
if (_readInProgress) return;
_readInProgress = true;
- _openedFile.read(_BLOCK_SIZE)
+ int readBytes = _BLOCK_SIZE;
+ if (_end != null) {
+ readBytes = min(readBytes, _end - _position);
+ if (readBytes < 0) {
+ throw new RangeError("Bad end position: $_end");
+ }
+ }
+ _openedFile.read(readBytes)
.then((block) {
_readInProgress = false;
if (block.length == 0) {
@@ -108,8 +116,16 @@ class _FileStream extends Stream<List<int>> {
openFuture
.then((RandomAccessFile opened) {
_openedFile = opened;
- _readBlock();
+ if (_position == null) {
+ _position = 0;
+ }
+ if (_position > 0) {
+ return opened.setPosition(_position);
+ } else if (_position < 0) {
+ throw new RangeError("Bad start position: $_position");
+ }
})
+ .then((_) => _readBlock())
.catchError((e) {
_controller.addError(e);
_controller.close();
@@ -430,8 +446,8 @@ class _File implements File {
return result;
}
- Stream<List<int>> openRead() {
- return new _FileStream(_path);
+ Stream<List<int>> openRead([int start, int end]) {
+ return new _FileStream(_path, start, end);
}
IOSink openWrite({FileMode mode: FileMode.WRITE,
« no previous file with comments | « sdk/lib/io/file.dart ('k') | tests/standalone/io/file_input_stream_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698