| 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 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 } | 340 } |
| 341 | 341 |
| 342 external static _directory(String path); | 342 external static _directory(String path); |
| 343 | 343 |
| 344 Directory directorySync() { | 344 Directory directorySync() { |
| 345 var result = _directory(path); | 345 var result = _directory(path); |
| 346 throwIfError(result, "Cannot retrieve directory for file '$_path'"); | 346 throwIfError(result, "Cannot retrieve directory for file '$_path'"); |
| 347 return new Directory(result); | 347 return new Directory(result); |
| 348 } | 348 } |
| 349 | 349 |
| 350 Future<RandomAccessFile> open([FileMode mode = FileMode.READ]) { | 350 Future<RandomAccessFile> open({FileMode mode: FileMode.READ}) { |
| 351 _ensureFileService(); | 351 _ensureFileService(); |
| 352 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 352 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 353 if (mode != FileMode.READ && | 353 if (mode != FileMode.READ && |
| 354 mode != FileMode.WRITE && | 354 mode != FileMode.WRITE && |
| 355 mode != FileMode.APPEND) { | 355 mode != FileMode.APPEND) { |
| 356 Timer.run(() { | 356 Timer.run(() { |
| 357 completer.completeError(new ArgumentError()); | 357 completer.completeError(new ArgumentError()); |
| 358 }); | 358 }); |
| 359 return completer.future; | 359 return completer.future; |
| 360 } | 360 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 external static _lastModified(String path); | 412 external static _lastModified(String path); |
| 413 | 413 |
| 414 DateTime lastModifiedSync() { | 414 DateTime lastModifiedSync() { |
| 415 var ms = _lastModified(path); | 415 var ms = _lastModified(path); |
| 416 throwIfError(ms, "Cannot retrieve modification time for file '$_path'"); | 416 throwIfError(ms, "Cannot retrieve modification time for file '$_path'"); |
| 417 return new DateTime.fromMillisecondsSinceEpoch(ms); | 417 return new DateTime.fromMillisecondsSinceEpoch(ms); |
| 418 } | 418 } |
| 419 | 419 |
| 420 external static _open(String path, int mode); | 420 external static _open(String path, int mode); |
| 421 | 421 |
| 422 RandomAccessFile openSync([FileMode mode = FileMode.READ]) { | 422 RandomAccessFile openSync({FileMode mode: FileMode.READ}) { |
| 423 if (mode != FileMode.READ && | 423 if (mode != FileMode.READ && |
| 424 mode != FileMode.WRITE && | 424 mode != FileMode.WRITE && |
| 425 mode != FileMode.APPEND) { | 425 mode != FileMode.APPEND) { |
| 426 throw new FileIOException("Unknown file mode. Use FileMode.READ, " | 426 throw new FileIOException("Unknown file mode. Use FileMode.READ, " |
| 427 "FileMode.WRITE or FileMode.APPEND."); | 427 "FileMode.WRITE or FileMode.APPEND."); |
| 428 } | 428 } |
| 429 var id = _open(_path, mode._mode); | 429 var id = _open(_path, mode._mode); |
| 430 throwIfError(id, "Cannot open file '$_path'"); | 430 throwIfError(id, "Cannot open file '$_path'"); |
| 431 return new _RandomAccessFile(id, _path); | 431 return new _RandomAccessFile(id, _path); |
| 432 } | 432 } |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 502 var length = opened.lengthSync(); | 502 var length = opened.lengthSync(); |
| 503 var result = new Uint8List(length); | 503 var result = new Uint8List(length); |
| 504 var read = opened.readListSync(result, 0, length); | 504 var read = opened.readListSync(result, 0, length); |
| 505 if (read != length) { | 505 if (read != length) { |
| 506 throw new FileIOException("Failed to read file"); | 506 throw new FileIOException("Failed to read file"); |
| 507 } | 507 } |
| 508 opened.closeSync(); | 508 opened.closeSync(); |
| 509 return result; | 509 return result; |
| 510 } | 510 } |
| 511 | 511 |
| 512 Future<String> readAsString([Encoding encoding = Encoding.UTF_8]) { | 512 Future<String> readAsString({Encoding encoding: Encoding.UTF_8}) { |
| 513 _ensureFileService(); | 513 _ensureFileService(); |
| 514 return readAsBytes().then((bytes) { | 514 return readAsBytes().then((bytes) { |
| 515 return _decodeString(bytes, encoding); | 515 return _decodeString(bytes, encoding); |
| 516 }); | 516 }); |
| 517 } | 517 } |
| 518 | 518 |
| 519 String readAsStringSync([Encoding encoding = Encoding.UTF_8]) { | 519 String readAsStringSync({Encoding encoding: Encoding.UTF_8}) { |
| 520 List<int> bytes = readAsBytesSync(); | 520 List<int> bytes = readAsBytesSync(); |
| 521 return _decodeString(bytes, encoding); | 521 return _decodeString(bytes, encoding); |
| 522 } | 522 } |
| 523 | 523 |
| 524 static List<String> _decodeLines(List<int> bytes, Encoding encoding) { | 524 static List<String> _decodeLines(List<int> bytes, Encoding encoding) { |
| 525 if (bytes.length == 0) return []; | 525 if (bytes.length == 0) return []; |
| 526 var list = []; | 526 var list = []; |
| 527 var controller = new StreamController(); | 527 var controller = new StreamController(); |
| 528 controller.stream | 528 controller.stream |
| 529 .transform(new StringDecoder(encoding)) | 529 .transform(new StringDecoder(encoding)) |
| 530 .transform(new LineTransformer()) | 530 .transform(new LineTransformer()) |
| 531 .listen((line) => list.add(line)); | 531 .listen((line) => list.add(line)); |
| 532 controller.add(bytes); | 532 controller.add(bytes); |
| 533 controller.close(); | 533 controller.close(); |
| 534 return list; | 534 return list; |
| 535 } | 535 } |
| 536 | 536 |
| 537 Future<List<String>> readAsLines([Encoding encoding = Encoding.UTF_8]) { | 537 Future<List<String>> readAsLines({Encoding encoding: Encoding.UTF_8}) { |
| 538 _ensureFileService(); | 538 _ensureFileService(); |
| 539 Completer<List<String>> completer = new Completer<List<String>>(); | 539 Completer<List<String>> completer = new Completer<List<String>>(); |
| 540 return readAsBytes().then((bytes) { | 540 return readAsBytes().then((bytes) { |
| 541 return _decodeLines(bytes, encoding); | 541 return _decodeLines(bytes, encoding); |
| 542 }); | 542 }); |
| 543 } | 543 } |
| 544 | 544 |
| 545 List<String> readAsLinesSync([Encoding encoding = Encoding.UTF_8]) { | 545 List<String> readAsLinesSync({Encoding encoding: Encoding.UTF_8}) { |
| 546 return _decodeLines(readAsBytesSync(), encoding); | 546 return _decodeLines(readAsBytesSync(), encoding); |
| 547 } | 547 } |
| 548 | 548 |
| 549 Future<File> writeAsBytes(List<int> bytes, | 549 Future<File> writeAsBytes(List<int> bytes, |
| 550 [FileMode mode = FileMode.WRITE]) { | 550 {FileMode mode: FileMode.WRITE}) { |
| 551 try { | 551 try { |
| 552 IOSink<File> sink = openWrite(mode: mode); | 552 IOSink<File> sink = openWrite(mode: mode); |
| 553 sink.writeBytes(bytes); | 553 sink.writeBytes(bytes); |
| 554 sink.close(); | 554 sink.close(); |
| 555 return sink.done.then((_) => this);; | 555 return sink.done.then((_) => this);; |
| 556 } catch (e) { | 556 } catch (e) { |
| 557 return new Future.immediateError(e); | 557 return new Future.immediateError(e); |
| 558 } | 558 } |
| 559 } | 559 } |
| 560 | 560 |
| 561 void writeAsBytesSync(List<int> bytes, [FileMode mode = FileMode.WRITE]) { | 561 void writeAsBytesSync(List<int> bytes, {FileMode mode: FileMode.WRITE}) { |
| 562 RandomAccessFile opened = openSync(mode); | 562 RandomAccessFile opened = openSync(mode: mode); |
| 563 opened.writeListSync(bytes, 0, bytes.length); | 563 opened.writeListSync(bytes, 0, bytes.length); |
| 564 opened.closeSync(); | 564 opened.closeSync(); |
| 565 } | 565 } |
| 566 | 566 |
| 567 Future<File> writeAsString(String contents, | 567 Future<File> writeAsString(String contents, |
| 568 {FileMode mode: FileMode.WRITE, | 568 {FileMode mode: FileMode.WRITE, |
| 569 Encoding encoding: Encoding.UTF_8}) { | 569 Encoding encoding: Encoding.UTF_8}) { |
| 570 try { | 570 try { |
| 571 return writeAsBytes(_encodeString(contents, encoding), mode); | 571 return writeAsBytes(_encodeString(contents, encoding), mode: mode); |
| 572 } catch (e) { | 572 } catch (e) { |
| 573 var completer = new Completer(); | 573 var completer = new Completer(); |
| 574 Timer.run(() => completer.completeError(e)); | 574 Timer.run(() => completer.completeError(e)); |
| 575 return completer.future; | 575 return completer.future; |
| 576 } | 576 } |
| 577 } | 577 } |
| 578 | 578 |
| 579 void writeAsStringSync(String contents, | 579 void writeAsStringSync(String contents, |
| 580 {FileMode mode: FileMode.WRITE, | 580 {FileMode mode: FileMode.WRITE, |
| 581 Encoding encoding: Encoding.UTF_8}) { | 581 Encoding encoding: Encoding.UTF_8}) { |
| 582 writeAsBytesSync(_encodeString(contents, encoding), mode); | 582 writeAsBytesSync(_encodeString(contents, encoding), mode: mode); |
| 583 } | 583 } |
| 584 | 584 |
| 585 String get path => _path; | 585 String get path => _path; |
| 586 | 586 |
| 587 String toString() => "File: '$path'"; | 587 String toString() => "File: '$path'"; |
| 588 | 588 |
| 589 void _ensureFileService() { | 589 void _ensureFileService() { |
| 590 if (_fileService == null) { | 590 if (_fileService == null) { |
| 591 _fileService = _FileUtils._newServicePort(); | 591 _fileService = _FileUtils._newServicePort(); |
| 592 } | 592 } |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 856 _ensureFastAndSerializableBuffer(buffer, offset, bytes); | 856 _ensureFastAndSerializableBuffer(buffer, offset, bytes); |
| 857 var result = | 857 var result = |
| 858 _writeList(_id, bufferAndOffset.buffer, bufferAndOffset.offset, bytes); | 858 _writeList(_id, bufferAndOffset.buffer, bufferAndOffset.offset, bytes); |
| 859 if (result is OSError) { | 859 if (result is OSError) { |
| 860 throw new FileIOException("writeList failed for file '$_path'", result); | 860 throw new FileIOException("writeList failed for file '$_path'", result); |
| 861 } | 861 } |
| 862 return result; | 862 return result; |
| 863 } | 863 } |
| 864 | 864 |
| 865 Future<RandomAccessFile> writeString(String string, | 865 Future<RandomAccessFile> writeString(String string, |
| 866 [Encoding encoding = Encoding.UTF_8]) { | 866 {Encoding encoding: Encoding.UTF_8}) { |
| 867 if (encoding is! Encoding) { | 867 if (encoding is! Encoding) { |
| 868 var completer = new Completer(); | 868 var completer = new Completer(); |
| 869 Timer.run(() { | 869 Timer.run(() { |
| 870 completer.completeError(new FileIOException( | 870 completer.completeError(new FileIOException( |
| 871 "Invalid encoding in writeString: $encoding")); | 871 "Invalid encoding in writeString: $encoding")); |
| 872 }); | 872 }); |
| 873 return completer.future; | 873 return completer.future; |
| 874 } | 874 } |
| 875 var data = _encodeString(string, encoding); | 875 var data = _encodeString(string, encoding); |
| 876 return writeList(data, 0, data.length); | 876 return writeList(data, 0, data.length); |
| 877 } | 877 } |
| 878 | 878 |
| 879 int writeStringSync(String string, [Encoding encoding = Encoding.UTF_8]) { | 879 int writeStringSync(String string, {Encoding encoding: Encoding.UTF_8}) { |
| 880 if (encoding is! Encoding) { | 880 if (encoding is! Encoding) { |
| 881 throw new FileIOException( | 881 throw new FileIOException( |
| 882 "Invalid encoding in writeStringSync: $encoding"); | 882 "Invalid encoding in writeStringSync: $encoding"); |
| 883 } | 883 } |
| 884 var data = _encodeString(string, encoding); | 884 var data = _encodeString(string, encoding); |
| 885 return writeListSync(data, 0, data.length); | 885 return writeListSync(data, 0, data.length); |
| 886 } | 886 } |
| 887 | 887 |
| 888 Future<int> position() { | 888 Future<int> position() { |
| 889 _ensureFileService(); | 889 _ensureFileService(); |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1041 new FileIOException("File closed '$_path'")); | 1041 new FileIOException("File closed '$_path'")); |
| 1042 }); | 1042 }); |
| 1043 return completer.future; | 1043 return completer.future; |
| 1044 } | 1044 } |
| 1045 | 1045 |
| 1046 final String _path; | 1046 final String _path; |
| 1047 int _id; | 1047 int _id; |
| 1048 | 1048 |
| 1049 SendPort _fileService; | 1049 SendPort _fileService; |
| 1050 } | 1050 } |
| OLD | NEW |