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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 Completer<File> completer = new Completer<File>(); |
551 try { | 552 try { |
552 var stream = openWrite(mode); | 553 var sink = openWrite(mode: mode); |
553 stream.add(bytes); | 554 sink.writeBytes(bytes); |
554 stream.close(); | 555 sink.close(); |
555 stream.done | 556 sink.done |
Anders Johnsen
2013/03/07 16:53:49
Maybe do:
- 'return sink.done ...' here
- Remove
Søren Gjesse
2013/03/08 09:47:46
Thanks, that is mich nicer. Had to do
return si
| |
556 .then((_) { | 557 .then((_) { |
557 completer.complete(this); | 558 completer.complete(this); |
558 }) | 559 }) |
559 .catchError((e) { | 560 .catchError((e) { |
560 completer.completeError(e); | 561 completer.completeError(e); |
561 }); | 562 }); |
562 } catch (e) { | 563 } catch (e) { |
563 Timer.run(() => completer.completeError(e)); | 564 Timer.run(() => completer.completeError(e)); |
564 return completer.future; | 565 return completer.future; |
565 } | 566 } |
(...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1049 new FileIOException("File closed '$_path'")); | 1050 new FileIOException("File closed '$_path'")); |
1050 }); | 1051 }); |
1051 return completer.future; | 1052 return completer.future; |
1052 } | 1053 } |
1053 | 1054 |
1054 final String _path; | 1055 final String _path; |
1055 int _id; | 1056 int _id; |
1056 | 1057 |
1057 SendPort _fileService; | 1058 SendPort _fileService; |
1058 } | 1059 } |
OLD | NEW |