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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 | 154 |
155 _FileStreamConsumer(File this._file, FileMode mode) { | 155 _FileStreamConsumer(File this._file, FileMode mode) { |
156 _openFuture = _file.open(mode: 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) { | |
165 return addStream(stream).then((_) => close()); | |
166 } | |
167 | |
168 Future<File> addStream(Stream<List<int>> stream) { | 164 Future<File> addStream(Stream<List<int>> stream) { |
169 Completer<File> completer = new Completer<File>(); | 165 Completer<File> completer = new Completer<File>(); |
170 _openFuture | 166 _openFuture |
171 .then((openedFile) { | 167 .then((openedFile) { |
172 _subscription = stream.listen( | 168 _subscription = stream.listen( |
173 (d) { | 169 (d) { |
174 _subscription.pause(); | 170 _subscription.pause(); |
175 openedFile.writeList(d, 0, d.length) | 171 openedFile.writeList(d, 0, d.length) |
176 .then((_) => _subscription.resume()) | 172 .then((_) => _subscription.resume()) |
177 .catchError((e) { | 173 .catchError((e) { |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 String fullPathSync() { | 465 String fullPathSync() { |
470 var result = _fullPath(_path); | 466 var result = _fullPath(_path); |
471 throwIfError(result, "Cannot retrieve full path for file '$_path'"); | 467 throwIfError(result, "Cannot retrieve full path for file '$_path'"); |
472 return result; | 468 return result; |
473 } | 469 } |
474 | 470 |
475 Stream<List<int>> openRead() { | 471 Stream<List<int>> openRead() { |
476 return new _FileStream(_path); | 472 return new _FileStream(_path); |
477 } | 473 } |
478 | 474 |
479 IOSink<File> openWrite({FileMode mode: FileMode.WRITE, | 475 IOSink openWrite({FileMode mode: FileMode.WRITE, |
480 Encoding encoding: Encoding.UTF_8}) { | 476 Encoding encoding: Encoding.UTF_8}) { |
481 if (mode != FileMode.WRITE && | 477 if (mode != FileMode.WRITE && |
482 mode != FileMode.APPEND) { | 478 mode != FileMode.APPEND) { |
483 throw new FileIOException( | 479 throw new FileIOException( |
484 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND"); | 480 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND"); |
485 } | 481 } |
486 var consumer = new _FileStreamConsumer(this, mode); | 482 var consumer = new _FileStreamConsumer(this, mode); |
487 return new IOSink<File>(consumer, encoding: encoding); | 483 return new IOSink(consumer, encoding: encoding); |
488 } | 484 } |
489 | 485 |
490 Future<List<int>> readAsBytes() { | 486 Future<List<int>> readAsBytes() { |
491 _ensureFileService(); | 487 _ensureFileService(); |
492 Completer<List<int>> completer = new Completer<List<int>>(); | 488 Completer<List<int>> completer = new Completer<List<int>>(); |
493 var chunks = new _BufferList(); | 489 var chunks = new _BufferList(); |
494 openRead().listen( | 490 openRead().listen( |
495 (d) => chunks.add(d), | 491 (d) => chunks.add(d), |
496 onDone: () { | 492 onDone: () { |
497 var result = chunks.readBytes(chunks.length); | 493 var result = chunks.readBytes(chunks.length); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
549 }); | 545 }); |
550 } | 546 } |
551 | 547 |
552 List<String> readAsLinesSync({Encoding encoding: Encoding.UTF_8}) { | 548 List<String> readAsLinesSync({Encoding encoding: Encoding.UTF_8}) { |
553 return _decodeLines(readAsBytesSync(), encoding); | 549 return _decodeLines(readAsBytesSync(), encoding); |
554 } | 550 } |
555 | 551 |
556 Future<File> writeAsBytes(List<int> bytes, | 552 Future<File> writeAsBytes(List<int> bytes, |
557 {FileMode mode: FileMode.WRITE}) { | 553 {FileMode mode: FileMode.WRITE}) { |
558 try { | 554 try { |
559 IOSink<File> sink = openWrite(mode: mode); | 555 IOSink sink = openWrite(mode: mode); |
560 sink.add(bytes); | 556 sink.add(bytes); |
561 sink.close(); | 557 sink.close(); |
562 return sink.done.then((_) => this);; | 558 return sink.done.then((_) => this);; |
563 } catch (e) { | 559 } catch (e) { |
564 return new Future.immediateError(e); | 560 return new Future.immediateError(e); |
565 } | 561 } |
566 } | 562 } |
567 | 563 |
568 void writeAsBytesSync(List<int> bytes, {FileMode mode: FileMode.WRITE}) { | 564 void writeAsBytesSync(List<int> bytes, {FileMode mode: FileMode.WRITE}) { |
569 RandomAccessFile opened = openSync(mode: mode); | 565 RandomAccessFile opened = openSync(mode: mode); |
(...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1054 new FileIOException("File closed '$_path'")); | 1050 new FileIOException("File closed '$_path'")); |
1055 }); | 1051 }); |
1056 return completer.future; | 1052 return completer.future; |
1057 } | 1053 } |
1058 | 1054 |
1059 final String _path; | 1055 final String _path; |
1060 int _id; | 1056 int _id; |
1061 | 1057 |
1062 SendPort _fileService; | 1058 SendPort _fileService; |
1063 } | 1059 } |
OLD | NEW |