| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of dart.io; | 5 part of dart.io; |
| 6 | 6 |
| 7 // Read the file in blocks of size 64k. | 7 // Read the file in blocks of size 64k. |
| 8 const int _BLOCK_SIZE = 64 * 1024; | 8 const int _BLOCK_SIZE = 64 * 1024; |
| 9 | 9 |
| 10 | 10 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 _FileStream(String this._path, this._position, this._end) { | 32 _FileStream(String this._path, this._position, this._end) { |
| 33 _setupController(); | 33 _setupController(); |
| 34 } | 34 } |
| 35 | 35 |
| 36 _FileStream.forStdin() : _position = 0 { | 36 _FileStream.forStdin() : _position = 0 { |
| 37 _setupController(); | 37 _setupController(); |
| 38 } | 38 } |
| 39 | 39 |
| 40 StreamSubscription<List<int>> listen(void onData(List<int> event), | 40 StreamSubscription<List<int>> listen(void onData(List<int> event), |
| 41 {void onError(error), | 41 {Function onError, |
| 42 void onDone(), | 42 void onDone(), |
| 43 bool cancelOnError}) { | 43 bool cancelOnError}) { |
| 44 return _controller.stream.listen(onData, | 44 return _controller.stream.listen(onData, |
| 45 onError: onError, | 45 onError: onError, |
| 46 onDone: onDone, | 46 onDone: onDone, |
| 47 cancelOnError: cancelOnError); | 47 cancelOnError: cancelOnError); |
| 48 } | 48 } |
| 49 | 49 |
| 50 void _setupController() { | 50 void _setupController() { |
| 51 _controller = new StreamController<List<int>>(sync: true, | 51 _controller = new StreamController<List<int>>(sync: true, |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 openedFile.writeFrom(d, 0, d.length) | 184 openedFile.writeFrom(d, 0, d.length) |
| 185 .then((_) => _subscription.resume()) | 185 .then((_) => _subscription.resume()) |
| 186 .catchError((e) { | 186 .catchError((e) { |
| 187 openedFile.close(); | 187 openedFile.close(); |
| 188 completer.completeError(e); | 188 completer.completeError(e); |
| 189 }); | 189 }); |
| 190 }, | 190 }, |
| 191 onDone: () { | 191 onDone: () { |
| 192 completer.complete(_file); | 192 completer.complete(_file); |
| 193 }, | 193 }, |
| 194 onError: (e) { | 194 onError: (e, [StackTrace stackTrace]) { |
| 195 openedFile.close(); | 195 openedFile.close(); |
| 196 completer.completeError(e); | 196 completer.completeError(e, stackTrace); |
| 197 }, | 197 }, |
| 198 cancelOnError: true); | 198 cancelOnError: true); |
| 199 }) | 199 }) |
| 200 .catchError((e) { | 200 .catchError((e) { |
| 201 completer.completeError(e); | 201 completer.completeError(e); |
| 202 }); | 202 }); |
| 203 return completer.future; | 203 return completer.future; |
| 204 } | 204 } |
| 205 | 205 |
| 206 Future<File> close() { | 206 Future<File> close() { |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 } | 411 } |
| 412 | 412 |
| 413 Future<List<int>> readAsBytes() { | 413 Future<List<int>> readAsBytes() { |
| 414 Completer<List<int>> completer = new Completer<List<int>>(); | 414 Completer<List<int>> completer = new Completer<List<int>>(); |
| 415 var builder = new BytesBuilder(); | 415 var builder = new BytesBuilder(); |
| 416 openRead().listen( | 416 openRead().listen( |
| 417 (d) => builder.add(d), | 417 (d) => builder.add(d), |
| 418 onDone: () { | 418 onDone: () { |
| 419 completer.complete(builder.takeBytes()); | 419 completer.complete(builder.takeBytes()); |
| 420 }, | 420 }, |
| 421 onError: (e) { | 421 onError: (e, StackTrace stackTrace) { |
| 422 completer.completeError(e); | 422 completer.completeError(e, stackTrace); |
| 423 }, | 423 }, |
| 424 cancelOnError: true); | 424 cancelOnError: true); |
| 425 return completer.future; | 425 return completer.future; |
| 426 } | 426 } |
| 427 | 427 |
| 428 List<int> readAsBytesSync() { | 428 List<int> readAsBytesSync() { |
| 429 var opened = openSync(); | 429 var opened = openSync(); |
| 430 var builder = new BytesBuilder(); | 430 var builder = new BytesBuilder(); |
| 431 var data; | 431 var data; |
| 432 while ((data = opened.readSync(_BLOCK_SIZE)).length > 0) { | 432 while ((data = opened.readSync(_BLOCK_SIZE)).length > 0) { |
| (...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 870 | 870 |
| 871 void _checkAvailable() { | 871 void _checkAvailable() { |
| 872 if (_asyncDispatched) { | 872 if (_asyncDispatched) { |
| 873 throw new FileException("An async operation is currently pending", path); | 873 throw new FileException("An async operation is currently pending", path); |
| 874 } | 874 } |
| 875 if (closed) { | 875 if (closed) { |
| 876 throw new FileException("File closed", path); | 876 throw new FileException("File closed", path); |
| 877 } | 877 } |
| 878 } | 878 } |
| 879 } | 879 } |
| OLD | NEW |