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 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 String fullPathSync() { | 461 String fullPathSync() { |
462 var result = _fullPath(_path); | 462 var result = _fullPath(_path); |
463 throwIfError(result, "Cannot retrieve full path for file '$_path'"); | 463 throwIfError(result, "Cannot retrieve full path for file '$_path'"); |
464 return result; | 464 return result; |
465 } | 465 } |
466 | 466 |
467 Stream<List<int>> openRead() { | 467 Stream<List<int>> openRead() { |
468 return new _FileStream(_path); | 468 return new _FileStream(_path); |
469 } | 469 } |
470 | 470 |
471 IOSink<File> openWrite([FileMode mode = FileMode.WRITE]) { | 471 IOSink<File> openWrite({FileMode mode: FileMode.WRITE, |
| 472 Encoding encoding: Encoding.UTF_8}) { |
472 if (mode != FileMode.WRITE && | 473 if (mode != FileMode.WRITE && |
473 mode != FileMode.APPEND) { | 474 mode != FileMode.APPEND) { |
474 throw new FileIOException( | 475 throw new FileIOException( |
475 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND"); | 476 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND"); |
476 } | 477 } |
477 var consumer = new _FileStreamConsumer(this, mode); | 478 var consumer = new _FileStreamConsumer(this, mode); |
478 return new IOSink<File>(consumer); | 479 return new IOSink<File>(consumer, encoding: encoding); |
479 } | 480 } |
480 | 481 |
481 Future<List<int>> readAsBytes() { | 482 Future<List<int>> readAsBytes() { |
482 _ensureFileService(); | 483 _ensureFileService(); |
483 Completer<List<int>> completer = new Completer<List<int>>(); | 484 Completer<List<int>> completer = new Completer<List<int>>(); |
484 var chunks = new _BufferList(); | 485 var chunks = new _BufferList(); |
485 openRead().listen( | 486 openRead().listen( |
486 (d) => chunks.add(d), | 487 (d) => chunks.add(d), |
487 onDone: () { | 488 onDone: () { |
488 var result = chunks.readBytes(chunks.length); | 489 var result = chunks.readBytes(chunks.length); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
540 return _decodeLines(bytes, encoding); | 541 return _decodeLines(bytes, encoding); |
541 }); | 542 }); |
542 } | 543 } |
543 | 544 |
544 List<String> readAsLinesSync([Encoding encoding = Encoding.UTF_8]) { | 545 List<String> readAsLinesSync([Encoding encoding = Encoding.UTF_8]) { |
545 return _decodeLines(readAsBytesSync(), encoding); | 546 return _decodeLines(readAsBytesSync(), encoding); |
546 } | 547 } |
547 | 548 |
548 Future<File> writeAsBytes(List<int> bytes, | 549 Future<File> writeAsBytes(List<int> bytes, |
549 [FileMode mode = FileMode.WRITE]) { | 550 [FileMode mode = FileMode.WRITE]) { |
550 Completer<File> completer = new Completer<File>(); | |
551 try { | 551 try { |
552 var stream = openWrite(mode); | 552 IOSink<File> sink = openWrite(mode: mode); |
553 stream.add(bytes); | 553 sink.writeBytes(bytes); |
554 stream.close(); | 554 sink.close(); |
555 stream.done | 555 return sink.done.then((_) => this);; |
556 .then((_) { | |
557 completer.complete(this); | |
558 }) | |
559 .catchError((e) { | |
560 completer.completeError(e); | |
561 }); | |
562 } catch (e) { | 556 } catch (e) { |
563 Timer.run(() => completer.completeError(e)); | 557 return new Future.immediateError(e); |
564 return completer.future; | |
565 } | 558 } |
566 return completer.future; | |
567 } | 559 } |
568 | 560 |
569 void writeAsBytesSync(List<int> bytes, [FileMode mode = FileMode.WRITE]) { | 561 void writeAsBytesSync(List<int> bytes, [FileMode mode = FileMode.WRITE]) { |
570 RandomAccessFile opened = openSync(mode); | 562 RandomAccessFile opened = openSync(mode); |
571 opened.writeListSync(bytes, 0, bytes.length); | 563 opened.writeListSync(bytes, 0, bytes.length); |
572 opened.closeSync(); | 564 opened.closeSync(); |
573 } | 565 } |
574 | 566 |
575 Future<File> writeAsString(String contents, | 567 Future<File> writeAsString(String contents, |
576 {FileMode mode: FileMode.WRITE, | 568 {FileMode mode: FileMode.WRITE, |
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1049 new FileIOException("File closed '$_path'")); | 1041 new FileIOException("File closed '$_path'")); |
1050 }); | 1042 }); |
1051 return completer.future; | 1043 return completer.future; |
1052 } | 1044 } |
1053 | 1045 |
1054 final String _path; | 1046 final String _path; |
1055 int _id; | 1047 int _id; |
1056 | 1048 |
1057 SendPort _fileService; | 1049 SendPort _fileService; |
1058 } | 1050 } |
OLD | NEW |