| 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 | 7 |
| 8 class _FileStream extends Stream<List<int>> { | 8 class _FileStream extends Stream<List<int>> { |
| 9 // Stream controller. | 9 // Stream controller. |
| 10 StreamController<List<int>> _controller; | 10 StreamController<List<int>> _controller; |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 _controller.addError(e); | 97 _controller.addError(e); |
| 98 _closeFile().then((_) { _controller.close(); }); | 98 _closeFile().then((_) { _controller.close(); }); |
| 99 _unsubscribed = true; | 99 _unsubscribed = true; |
| 100 } | 100 } |
| 101 }); | 101 }); |
| 102 } | 102 } |
| 103 | 103 |
| 104 void _start() { | 104 void _start() { |
| 105 Future<RandomAccessFile> openFuture; | 105 Future<RandomAccessFile> openFuture; |
| 106 if (_path != null) { | 106 if (_path != null) { |
| 107 openFuture = new File(_path).open(FileMode.READ); | 107 openFuture = new File(_path).open(mode: FileMode.READ); |
| 108 } else { | 108 } else { |
| 109 openFuture = new Future.immediate(_File._openStdioSync(0)); | 109 openFuture = new Future.immediate(_File._openStdioSync(0)); |
| 110 } | 110 } |
| 111 openFuture | 111 openFuture |
| 112 .then((RandomAccessFile opened) { | 112 .then((RandomAccessFile opened) { |
| 113 _openedFile = opened; | 113 _openedFile = opened; |
| 114 _readBlock(); | 114 _readBlock(); |
| 115 }) | 115 }) |
| 116 .catchError((e) { | 116 .catchError((e) { |
| 117 _controller.addError(e); | 117 _controller.addError(e); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 146 } | 146 } |
| 147 } | 147 } |
| 148 } | 148 } |
| 149 | 149 |
| 150 class _FileStreamConsumer extends StreamConsumer<List<int>, File> { | 150 class _FileStreamConsumer extends StreamConsumer<List<int>, File> { |
| 151 File _file; | 151 File _file; |
| 152 Future<RandomAccessFile> _openFuture; | 152 Future<RandomAccessFile> _openFuture; |
| 153 StreamSubscription _subscription; | 153 StreamSubscription _subscription; |
| 154 | 154 |
| 155 _FileStreamConsumer(File this._file, FileMode mode) { | 155 _FileStreamConsumer(File this._file, FileMode mode) { |
| 156 _openFuture = _file.open(mode); | 156 _openFuture = _file.open(mode: mode); |
| 157 } | 157 } |
| 158 | 158 |
| 159 _FileStreamConsumer.fromStdio(int fd) { | 159 _FileStreamConsumer.fromStdio(int fd) { |
| 160 assert(1 <= fd && fd <= 2); | 160 assert(1 <= fd && fd <= 2); |
| 161 _openFuture = new Future.immediate(_File._openStdioSync(fd)); | 161 _openFuture = new Future.immediate(_File._openStdioSync(fd)); |
| 162 } | 162 } |
| 163 | 163 |
| 164 Future<File> consume(Stream<List<int>> stream) { | 164 Future<File> consume(Stream<List<int>> stream) { |
| 165 Completer<File> completer = new Completer<File>(); | 165 Completer<File> completer = new Completer<File>(); |
| 166 _openFuture | 166 _openFuture |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 } | 342 } |
| 343 | 343 |
| 344 external static _directory(String path); | 344 external static _directory(String path); |
| 345 | 345 |
| 346 Directory directorySync() { | 346 Directory directorySync() { |
| 347 var result = _directory(path); | 347 var result = _directory(path); |
| 348 throwIfError(result, "Cannot retrieve directory for file '$_path'"); | 348 throwIfError(result, "Cannot retrieve directory for file '$_path'"); |
| 349 return new Directory(result); | 349 return new Directory(result); |
| 350 } | 350 } |
| 351 | 351 |
| 352 Future<RandomAccessFile> open([FileMode mode = FileMode.READ]) { | 352 Future<RandomAccessFile> open({FileMode mode: FileMode.READ}) { |
| 353 _ensureFileService(); | 353 _ensureFileService(); |
| 354 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 354 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 355 if (mode != FileMode.READ && | 355 if (mode != FileMode.READ && |
| 356 mode != FileMode.WRITE && | 356 mode != FileMode.WRITE && |
| 357 mode != FileMode.APPEND) { | 357 mode != FileMode.APPEND) { |
| 358 Timer.run(() { | 358 Timer.run(() { |
| 359 completer.completeError(new ArgumentError()); | 359 completer.completeError(new ArgumentError()); |
| 360 }); | 360 }); |
| 361 return completer.future; | 361 return completer.future; |
| 362 } | 362 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 external static _lastModified(String path); | 414 external static _lastModified(String path); |
| 415 | 415 |
| 416 DateTime lastModifiedSync() { | 416 DateTime lastModifiedSync() { |
| 417 var ms = _lastModified(path); | 417 var ms = _lastModified(path); |
| 418 throwIfError(ms, "Cannot retrieve modification time for file '$_path'"); | 418 throwIfError(ms, "Cannot retrieve modification time for file '$_path'"); |
| 419 return new DateTime.fromMillisecondsSinceEpoch(ms); | 419 return new DateTime.fromMillisecondsSinceEpoch(ms); |
| 420 } | 420 } |
| 421 | 421 |
| 422 external static _open(String path, int mode); | 422 external static _open(String path, int mode); |
| 423 | 423 |
| 424 RandomAccessFile openSync([FileMode mode = FileMode.READ]) { | 424 RandomAccessFile openSync({FileMode mode: FileMode.READ}) { |
| 425 if (mode != FileMode.READ && | 425 if (mode != FileMode.READ && |
| 426 mode != FileMode.WRITE && | 426 mode != FileMode.WRITE && |
| 427 mode != FileMode.APPEND) { | 427 mode != FileMode.APPEND) { |
| 428 throw new FileIOException("Unknown file mode. Use FileMode.READ, " | 428 throw new FileIOException("Unknown file mode. Use FileMode.READ, " |
| 429 "FileMode.WRITE or FileMode.APPEND."); | 429 "FileMode.WRITE or FileMode.APPEND."); |
| 430 } | 430 } |
| 431 var id = _open(_path, mode._mode); | 431 var id = _open(_path, mode._mode); |
| 432 throwIfError(id, "Cannot open file '$_path'"); | 432 throwIfError(id, "Cannot open file '$_path'"); |
| 433 return new _RandomAccessFile(id, _path); | 433 return new _RandomAccessFile(id, _path); |
| 434 } | 434 } |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 var length = opened.lengthSync(); | 504 var length = opened.lengthSync(); |
| 505 var result = new Uint8List(length); | 505 var result = new Uint8List(length); |
| 506 var read = opened.readListSync(result, 0, length); | 506 var read = opened.readListSync(result, 0, length); |
| 507 if (read != length) { | 507 if (read != length) { |
| 508 throw new FileIOException("Failed to read file"); | 508 throw new FileIOException("Failed to read file"); |
| 509 } | 509 } |
| 510 opened.closeSync(); | 510 opened.closeSync(); |
| 511 return result; | 511 return result; |
| 512 } | 512 } |
| 513 | 513 |
| 514 Future<String> readAsString([Encoding encoding = Encoding.UTF_8]) { | 514 Future<String> readAsString({Encoding encoding: Encoding.UTF_8}) { |
| 515 _ensureFileService(); | 515 _ensureFileService(); |
| 516 return readAsBytes().then((bytes) { | 516 return readAsBytes().then((bytes) { |
| 517 return _decodeString(bytes, encoding); | 517 return _decodeString(bytes, encoding); |
| 518 }); | 518 }); |
| 519 } | 519 } |
| 520 | 520 |
| 521 String readAsStringSync([Encoding encoding = Encoding.UTF_8]) { | 521 String readAsStringSync({Encoding encoding: Encoding.UTF_8}) { |
| 522 List<int> bytes = readAsBytesSync(); | 522 List<int> bytes = readAsBytesSync(); |
| 523 return _decodeString(bytes, encoding); | 523 return _decodeString(bytes, encoding); |
| 524 } | 524 } |
| 525 | 525 |
| 526 static List<String> _decodeLines(List<int> bytes, Encoding encoding) { | 526 static List<String> _decodeLines(List<int> bytes, Encoding encoding) { |
| 527 if (bytes.length == 0) return []; | 527 if (bytes.length == 0) return []; |
| 528 var list = []; | 528 var list = []; |
| 529 var controller = new StreamController(); | 529 var controller = new StreamController(); |
| 530 controller.stream | 530 controller.stream |
| 531 .transform(new StringDecoder(encoding)) | 531 .transform(new StringDecoder(encoding)) |
| 532 .transform(new LineTransformer()) | 532 .transform(new LineTransformer()) |
| 533 .listen((line) => list.add(line)); | 533 .listen((line) => list.add(line)); |
| 534 controller.add(bytes); | 534 controller.add(bytes); |
| 535 controller.close(); | 535 controller.close(); |
| 536 return list; | 536 return list; |
| 537 } | 537 } |
| 538 | 538 |
| 539 Future<List<String>> readAsLines([Encoding encoding = Encoding.UTF_8]) { | 539 Future<List<String>> readAsLines({Encoding encoding: Encoding.UTF_8}) { |
| 540 _ensureFileService(); | 540 _ensureFileService(); |
| 541 Completer<List<String>> completer = new Completer<List<String>>(); | 541 Completer<List<String>> completer = new Completer<List<String>>(); |
| 542 return readAsBytes().then((bytes) { | 542 return readAsBytes().then((bytes) { |
| 543 return _decodeLines(bytes, encoding); | 543 return _decodeLines(bytes, encoding); |
| 544 }); | 544 }); |
| 545 } | 545 } |
| 546 | 546 |
| 547 List<String> readAsLinesSync([Encoding encoding = Encoding.UTF_8]) { | 547 List<String> readAsLinesSync({Encoding encoding: Encoding.UTF_8}) { |
| 548 return _decodeLines(readAsBytesSync(), encoding); | 548 return _decodeLines(readAsBytesSync(), encoding); |
| 549 } | 549 } |
| 550 | 550 |
| 551 Future<File> writeAsBytes(List<int> bytes, | 551 Future<File> writeAsBytes(List<int> bytes, |
| 552 [FileMode mode = FileMode.WRITE]) { | 552 {FileMode mode: FileMode.WRITE}) { |
| 553 try { | 553 try { |
| 554 IOSink<File> sink = openWrite(mode: mode); | 554 IOSink<File> sink = openWrite(mode: mode); |
| 555 sink.writeBytes(bytes); | 555 sink.writeBytes(bytes); |
| 556 sink.close(); | 556 sink.close(); |
| 557 return sink.done.then((_) => this);; | 557 return sink.done.then((_) => this);; |
| 558 } catch (e) { | 558 } catch (e) { |
| 559 return new Future.immediateError(e); | 559 return new Future.immediateError(e); |
| 560 } | 560 } |
| 561 } | 561 } |
| 562 | 562 |
| 563 void writeAsBytesSync(List<int> bytes, [FileMode mode = FileMode.WRITE]) { | 563 void writeAsBytesSync(List<int> bytes, {FileMode mode: FileMode.WRITE}) { |
| 564 RandomAccessFile opened = openSync(mode); | 564 RandomAccessFile opened = openSync(mode: mode); |
| 565 opened.writeListSync(bytes, 0, bytes.length); | 565 opened.writeListSync(bytes, 0, bytes.length); |
| 566 opened.closeSync(); | 566 opened.closeSync(); |
| 567 } | 567 } |
| 568 | 568 |
| 569 Future<File> writeAsString(String contents, | 569 Future<File> writeAsString(String contents, |
| 570 {FileMode mode: FileMode.WRITE, | 570 {FileMode mode: FileMode.WRITE, |
| 571 Encoding encoding: Encoding.UTF_8}) { | 571 Encoding encoding: Encoding.UTF_8}) { |
| 572 try { | 572 try { |
| 573 return writeAsBytes(_encodeString(contents, encoding), mode); | 573 return writeAsBytes(_encodeString(contents, encoding), mode: mode); |
| 574 } catch (e) { | 574 } catch (e) { |
| 575 var completer = new Completer(); | 575 var completer = new Completer(); |
| 576 Timer.run(() => completer.completeError(e)); | 576 Timer.run(() => completer.completeError(e)); |
| 577 return completer.future; | 577 return completer.future; |
| 578 } | 578 } |
| 579 } | 579 } |
| 580 | 580 |
| 581 void writeAsStringSync(String contents, | 581 void writeAsStringSync(String contents, |
| 582 {FileMode mode: FileMode.WRITE, | 582 {FileMode mode: FileMode.WRITE, |
| 583 Encoding encoding: Encoding.UTF_8}) { | 583 Encoding encoding: Encoding.UTF_8}) { |
| 584 writeAsBytesSync(_encodeString(contents, encoding), mode); | 584 writeAsBytesSync(_encodeString(contents, encoding), mode: mode); |
| 585 } | 585 } |
| 586 | 586 |
| 587 String get path => _path; | 587 String get path => _path; |
| 588 | 588 |
| 589 String toString() => "File: '$path'"; | 589 String toString() => "File: '$path'"; |
| 590 | 590 |
| 591 void _ensureFileService() { | 591 void _ensureFileService() { |
| 592 if (_fileService == null) { | 592 if (_fileService == null) { |
| 593 _fileService = _FileUtils._newServicePort(); | 593 _fileService = _FileUtils._newServicePort(); |
| 594 } | 594 } |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 858 _ensureFastAndSerializableBuffer(buffer, offset, bytes); | 858 _ensureFastAndSerializableBuffer(buffer, offset, bytes); |
| 859 var result = | 859 var result = |
| 860 _writeList(_id, bufferAndOffset.buffer, bufferAndOffset.offset, bytes); | 860 _writeList(_id, bufferAndOffset.buffer, bufferAndOffset.offset, bytes); |
| 861 if (result is OSError) { | 861 if (result is OSError) { |
| 862 throw new FileIOException("writeList failed for file '$_path'", result); | 862 throw new FileIOException("writeList failed for file '$_path'", result); |
| 863 } | 863 } |
| 864 return result; | 864 return result; |
| 865 } | 865 } |
| 866 | 866 |
| 867 Future<RandomAccessFile> writeString(String string, | 867 Future<RandomAccessFile> writeString(String string, |
| 868 [Encoding encoding = Encoding.UTF_8]) { | 868 {Encoding encoding: Encoding.UTF_8}) { |
| 869 if (encoding is! Encoding) { | 869 if (encoding is! Encoding) { |
| 870 var completer = new Completer(); | 870 var completer = new Completer(); |
| 871 Timer.run(() { | 871 Timer.run(() { |
| 872 completer.completeError(new FileIOException( | 872 completer.completeError(new FileIOException( |
| 873 "Invalid encoding in writeString: $encoding")); | 873 "Invalid encoding in writeString: $encoding")); |
| 874 }); | 874 }); |
| 875 return completer.future; | 875 return completer.future; |
| 876 } | 876 } |
| 877 var data = _encodeString(string, encoding); | 877 var data = _encodeString(string, encoding); |
| 878 return writeList(data, 0, data.length); | 878 return writeList(data, 0, data.length); |
| 879 } | 879 } |
| 880 | 880 |
| 881 int writeStringSync(String string, [Encoding encoding = Encoding.UTF_8]) { | 881 int writeStringSync(String string, {Encoding encoding: Encoding.UTF_8}) { |
| 882 if (encoding is! Encoding) { | 882 if (encoding is! Encoding) { |
| 883 throw new FileIOException( | 883 throw new FileIOException( |
| 884 "Invalid encoding in writeStringSync: $encoding"); | 884 "Invalid encoding in writeStringSync: $encoding"); |
| 885 } | 885 } |
| 886 var data = _encodeString(string, encoding); | 886 var data = _encodeString(string, encoding); |
| 887 return writeListSync(data, 0, data.length); | 887 return writeListSync(data, 0, data.length); |
| 888 } | 888 } |
| 889 | 889 |
| 890 Future<int> position() { | 890 Future<int> position() { |
| 891 _ensureFileService(); | 891 _ensureFileService(); |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1043 new FileIOException("File closed '$_path'")); | 1043 new FileIOException("File closed '$_path'")); |
| 1044 }); | 1044 }); |
| 1045 return completer.future; | 1045 return completer.future; |
| 1046 } | 1046 } |
| 1047 | 1047 |
| 1048 final String _path; | 1048 final String _path; |
| 1049 int _id; | 1049 int _id; |
| 1050 | 1050 |
| 1051 SendPort _fileService; | 1051 SendPort _fileService; |
| 1052 } | 1052 } |
| OLD | NEW |