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 12 matching lines...) Expand all Loading... |
23 bool _paused = false; | 23 bool _paused = false; |
24 bool _unsubscribed = false; | 24 bool _unsubscribed = false; |
25 | 25 |
26 // Is there a read currently in progress? | 26 // Is there a read currently in progress? |
27 bool _readInProgress = false; | 27 bool _readInProgress = false; |
28 bool _closed = false; | 28 bool _closed = false; |
29 | 29 |
30 // Block read but not yet send because stream is paused. | 30 // Block read but not yet send because stream is paused. |
31 List<int> _currentBlock; | 31 List<int> _currentBlock; |
32 | 32 |
33 _FileStream(String this._path, this._position, this._end) { | 33 _FileStream(this._path, this._position, this._end) { |
34 _setupController(); | 34 _setupController(); |
35 } | 35 } |
36 | 36 |
37 _FileStream.forStdin() : _position = 0 { | 37 _FileStream.forStdin() : _position = 0 { |
38 _setupController(); | 38 _setupController(); |
39 } | 39 } |
40 | 40 |
41 StreamSubscription<List<int>> listen(void onData(List<int> event), | 41 StreamSubscription<List<int>> listen(void onData(List<int> event), |
42 {Function onError, | 42 {Function onError, |
43 void onDone(), | 43 void onDone(), |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 completer.completeError(e, stackTrace); | 208 completer.completeError(e, stackTrace); |
209 }, | 209 }, |
210 cancelOnError: true); | 210 cancelOnError: true); |
211 }) | 211 }) |
212 .catchError((e) { | 212 .catchError((e) { |
213 completer.completeError(e); | 213 completer.completeError(e); |
214 }); | 214 }); |
215 return completer.future; | 215 return completer.future; |
216 } | 216 } |
217 | 217 |
218 Future<File> close() { | 218 Future<File> close() => |
219 return _openFuture.then((openedFile) => openedFile.close()); | 219 _openFuture.then((openedFile) => openedFile.close()); |
220 } | |
221 } | 220 } |
222 | 221 |
223 | 222 |
224 // Class for encapsulating the native implementation of files. | 223 // Class for encapsulating the native implementation of files. |
225 class _File extends FileSystemEntity implements File { | 224 class _File extends FileSystemEntity implements File { |
226 final String path; | 225 final String path; |
227 | 226 |
228 // Constructor for file. | 227 // Constructor for file. |
229 _File(String this.path) { | 228 _File(this.path) { |
230 if (path is! String) { | 229 if (path is! String) { |
231 throw new ArgumentError('${Error.safeToString(path)} ' | 230 throw new ArgumentError('${Error.safeToString(path)} ' |
232 'is not a String'); | 231 'is not a String'); |
233 } | 232 } |
234 } | 233 } |
235 | 234 |
236 Future<bool> exists() { | 235 Future<bool> exists() { |
237 return _IOService.dispatch(_FILE_EXISTS, [path]).then((response) { | 236 return _IOService.dispatch(_FILE_EXISTS, [path]).then((response) { |
238 if (_isErrorResponse(response)) { | 237 if (_isErrorResponse(response)) { |
239 throw _exceptionFromResponse(response, "Cannot check existence", path); | 238 throw _exceptionFromResponse(response, "Cannot check existence", path); |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 | 465 |
467 String _tryDecode(List<int> bytes, Encoding encoding) { | 466 String _tryDecode(List<int> bytes, Encoding encoding) { |
468 try { | 467 try { |
469 return encoding.decode(bytes); | 468 return encoding.decode(bytes); |
470 } catch (_) { | 469 } catch (_) { |
471 throw new FileSystemException( | 470 throw new FileSystemException( |
472 "Failed to decode data using encoding '${encoding.name}'", path); | 471 "Failed to decode data using encoding '${encoding.name}'", path); |
473 } | 472 } |
474 } | 473 } |
475 | 474 |
476 Future<String> readAsString({Encoding encoding: UTF8}) { | 475 Future<String> readAsString({Encoding encoding: UTF8}) => |
477 return readAsBytes().then((bytes) { | 476 readAsBytes().then((bytes) => _tryDecode(bytes, encoding)); |
478 return _tryDecode(bytes, encoding); | |
479 }); | |
480 } | |
481 | 477 |
482 String readAsStringSync({Encoding encoding: UTF8}) { | 478 String readAsStringSync({Encoding encoding: UTF8}) { |
483 List<int> bytes = readAsBytesSync(); | 479 List<int> bytes = readAsBytesSync(); |
484 return _tryDecode(bytes, encoding); | 480 return _tryDecode(bytes, encoding); |
485 } | 481 } |
486 | 482 |
487 List<String> _decodeLines(List<int> bytes, Encoding encoding) { | 483 List<String> _decodeLines(List<int> bytes, Encoding encoding) { |
488 if (bytes.length == 0) return []; | 484 if (bytes.length == 0) return []; |
489 var list = []; | 485 var list = []; |
490 var controller = new StreamController(sync: true); | 486 var controller = new StreamController(sync: true); |
(...skipping 10 matching lines...) Expand all Loading... |
501 } | 497 } |
502 return list; | 498 return list; |
503 } | 499 } |
504 | 500 |
505 Future<List<String>> readAsLines({Encoding encoding: UTF8}) { | 501 Future<List<String>> readAsLines({Encoding encoding: UTF8}) { |
506 return readAsBytes().then((bytes) { | 502 return readAsBytes().then((bytes) { |
507 return _decodeLines(bytes, encoding); | 503 return _decodeLines(bytes, encoding); |
508 }); | 504 }); |
509 } | 505 } |
510 | 506 |
511 List<String> readAsLinesSync({Encoding encoding: UTF8}) { | 507 List<String> readAsLinesSync({Encoding encoding: UTF8}) => |
512 return _decodeLines(readAsBytesSync(), encoding); | 508 _decodeLines(readAsBytesSync(), encoding); |
513 } | |
514 | 509 |
515 Future<File> writeAsBytes(List<int> bytes, | 510 Future<File> writeAsBytes(List<int> bytes, |
516 {FileMode mode: FileMode.WRITE, | 511 {FileMode mode: FileMode.WRITE, |
517 bool flush: false}) { | 512 bool flush: false}) { |
518 try { | 513 try { |
519 IOSink sink = openWrite(mode: mode); | 514 IOSink sink = openWrite(mode: mode); |
520 sink.add(bytes); | 515 sink.add(bytes); |
521 if (flush) { | 516 if (flush) { |
522 sink.flush().then((_) => sink.close()); | 517 sink.flush().then((_) => sink.close()); |
523 } else { | 518 } else { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
565 } | 560 } |
566 } | 561 } |
567 | 562 |
568 | 563 |
569 class _RandomAccessFile implements RandomAccessFile { | 564 class _RandomAccessFile implements RandomAccessFile { |
570 final String path; | 565 final String path; |
571 int _id; | 566 int _id; |
572 bool _asyncDispatched = false; | 567 bool _asyncDispatched = false; |
573 SendPort _fileService; | 568 SendPort _fileService; |
574 | 569 |
575 _RandomAccessFile(int this._id, String this.path); | 570 _RandomAccessFile(this._id, this.path); |
576 | 571 |
577 Future<RandomAccessFile> close() { | 572 Future<RandomAccessFile> close() { |
578 return _dispatch(_FILE_CLOSE, [_id], markClosed: true).then((result) { | 573 return _dispatch(_FILE_CLOSE, [_id], markClosed: true).then((result) { |
579 if (result != -1) { | 574 if (result != -1) { |
580 _id = result; | 575 _id = result; |
581 return this; | 576 return this; |
582 } else { | 577 } else { |
583 throw new FileSystemException("Cannot close file", path); | 578 throw new FileSystemException("Cannot close file", path); |
584 } | 579 } |
585 }); | 580 }); |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
908 | 903 |
909 void _checkAvailable() { | 904 void _checkAvailable() { |
910 if (_asyncDispatched) { | 905 if (_asyncDispatched) { |
911 throw new FileSystemException("An async operation is currently pending", p
ath); | 906 throw new FileSystemException("An async operation is currently pending", p
ath); |
912 } | 907 } |
913 if (closed) { | 908 if (closed) { |
914 throw new FileSystemException("File closed", path); | 909 throw new FileSystemException("File closed", path); |
915 } | 910 } |
916 } | 911 } |
917 } | 912 } |
OLD | NEW |