| 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. |
| 8 const int _BLOCK_SIZE = 64 * 1024; |
| 9 |
| 7 | 10 |
| 8 class _FileStream extends Stream<List<int>> { | 11 class _FileStream extends Stream<List<int>> { |
| 9 // Stream controller. | 12 // Stream controller. |
| 10 StreamController<List<int>> _controller; | 13 StreamController<List<int>> _controller; |
| 11 | 14 |
| 12 // Read the file in blocks of size 64k. | |
| 13 final int _blockSize = 64 * 1024; | |
| 14 | |
| 15 // Information about the underlying file. | 15 // Information about the underlying file. |
| 16 String _path; | 16 String _path; |
| 17 RandomAccessFile _openedFile; | 17 RandomAccessFile _openedFile; |
| 18 int _position; | 18 int _position; |
| 19 | 19 |
| 20 // Has the stream been paused or unsubscribed? | 20 // Has the stream been paused or unsubscribed? |
| 21 bool _paused = false; | 21 bool _paused = false; |
| 22 bool _unsubscribed = false; | 22 bool _unsubscribed = false; |
| 23 | 23 |
| 24 // Is there a read currently in progress? | 24 // Is there a read currently in progress? |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 _openedFile.length() | 69 _openedFile.length() |
| 70 .then((length) { | 70 .then((length) { |
| 71 if (_position >= length) { | 71 if (_position >= length) { |
| 72 _readInProgress = false; | 72 _readInProgress = false; |
| 73 if (!_unsubscribed) { | 73 if (!_unsubscribed) { |
| 74 _closeFile().then((_) { _controller.close(); }); | 74 _closeFile().then((_) { _controller.close(); }); |
| 75 _unsubscribed = true; | 75 _unsubscribed = true; |
| 76 } | 76 } |
| 77 return null; | 77 return null; |
| 78 } else { | 78 } else { |
| 79 return _openedFile.read(_blockSize); | 79 return _openedFile.read(_BLOCK_SIZE); |
| 80 } | 80 } |
| 81 }) | 81 }) |
| 82 .then((block) { | 82 .then((block) { |
| 83 _readInProgress = false; | 83 _readInProgress = false; |
| 84 if (block == null || _unsubscribed) { | 84 if (block == null || _unsubscribed) { |
| 85 return; | 85 return; |
| 86 } | 86 } |
| 87 _position += block.length; | 87 _position += block.length; |
| 88 if (_paused) { | 88 if (_paused) { |
| 89 _currentBlock = block; | 89 _currentBlock = block; |
| (...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 }, | 496 }, |
| 497 onError: (e) { | 497 onError: (e) { |
| 498 completer.completeError(e); | 498 completer.completeError(e); |
| 499 }, | 499 }, |
| 500 unsubscribeOnError: true); | 500 unsubscribeOnError: true); |
| 501 return completer.future; | 501 return completer.future; |
| 502 } | 502 } |
| 503 | 503 |
| 504 List<int> readAsBytesSync() { | 504 List<int> readAsBytesSync() { |
| 505 var opened = openSync(); | 505 var opened = openSync(); |
| 506 var length = opened.lengthSync(); | 506 var chunks = new _BufferList(); |
| 507 var result = new Uint8List(length); | 507 var data; |
| 508 var read = opened.readListSync(result, 0, length); | 508 while ((data = opened.readSync(_BLOCK_SIZE)).length > 0) { |
| 509 if (read != length) { | 509 chunks.add(data); |
| 510 throw new FileIOException("Failed to read file"); | |
| 511 } | 510 } |
| 512 opened.closeSync(); | 511 opened.closeSync(); |
| 513 return result; | 512 return chunks.readBytes(); |
| 514 } | 513 } |
| 515 | 514 |
| 516 Future<String> readAsString({Encoding encoding: Encoding.UTF_8}) { | 515 Future<String> readAsString({Encoding encoding: Encoding.UTF_8}) { |
| 517 _ensureFileService(); | 516 _ensureFileService(); |
| 518 return readAsBytes().then((bytes) { | 517 return readAsBytes().then((bytes) { |
| 519 return _decodeString(bytes, encoding); | 518 return _decodeString(bytes, encoding); |
| 520 }); | 519 }); |
| 521 } | 520 } |
| 522 | 521 |
| 523 String readAsStringSync({Encoding encoding: Encoding.UTF_8}) { | 522 String readAsStringSync({Encoding encoding: Encoding.UTF_8}) { |
| (...skipping 521 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1045 new FileIOException("File closed '$_path'")); | 1044 new FileIOException("File closed '$_path'")); |
| 1046 }); | 1045 }); |
| 1047 return completer.future; | 1046 return completer.future; |
| 1048 } | 1047 } |
| 1049 | 1048 |
| 1050 final String _path; | 1049 final String _path; |
| 1051 int _id; | 1050 int _id; |
| 1052 | 1051 |
| 1053 SendPort _fileService; | 1052 SendPort _fileService; |
| 1054 } | 1053 } |
| OLD | NEW |