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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 onPauseStateChange: _onPauseStateChange); | 51 onPauseStateChange: _onPauseStateChange); |
52 } | 52 } |
53 | 53 |
54 Future _closeFile() { | 54 Future _closeFile() { |
55 Future closeFuture; | 55 Future closeFuture; |
56 if (_openedFile != null) { | 56 if (_openedFile != null) { |
57 Future closeFuture = _openedFile.close(); | 57 Future closeFuture = _openedFile.close(); |
58 _openedFile = null; | 58 _openedFile = null; |
59 return closeFuture; | 59 return closeFuture; |
60 } else { | 60 } else { |
61 return new Future.immediate(null); | 61 return new Future.value(); |
62 } | 62 } |
63 } | 63 } |
64 | 64 |
65 void _readBlock() { | 65 void _readBlock() { |
66 // Don't start a new read if one is already in progress. | 66 // Don't start a new read if one is already in progress. |
67 if (_readInProgress) return; | 67 if (_readInProgress) return; |
68 _readInProgress = true; | 68 _readInProgress = true; |
69 _openedFile.length() | 69 _openedFile.length() |
70 .then((length) { | 70 .then((length) { |
71 if (_position >= length) { | 71 if (_position >= length) { |
(...skipping 27 matching lines...) Expand all Loading... |
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(mode: 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.value(_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); |
118 _controller.close(); | 118 _controller.close(); |
119 }); | 119 }); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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: 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.value(_File._openStdioSync(fd)); |
162 } | 162 } |
163 | 163 |
164 Future<File> consume(Stream<List<int>> stream) { | 164 Future<File> consume(Stream<List<int>> stream) { |
165 return addStream(stream).then((_) => close()); | 165 return addStream(stream).then((_) => close()); |
166 } | 166 } |
167 | 167 |
168 Future<File> addStream(Stream<List<int>> stream) { | 168 Future<File> addStream(Stream<List<int>> stream) { |
169 Completer<File> completer = new Completer<File>(); | 169 Completer<File> completer = new Completer<File>(); |
170 _openFuture | 170 _openFuture |
171 .then((openedFile) { | 171 .then((openedFile) { |
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 } | 554 } |
555 | 555 |
556 Future<File> writeAsBytes(List<int> bytes, | 556 Future<File> writeAsBytes(List<int> bytes, |
557 {FileMode mode: FileMode.WRITE}) { | 557 {FileMode mode: FileMode.WRITE}) { |
558 try { | 558 try { |
559 IOSink<File> sink = openWrite(mode: mode); | 559 IOSink<File> sink = openWrite(mode: mode); |
560 sink.add(bytes); | 560 sink.add(bytes); |
561 sink.close(); | 561 sink.close(); |
562 return sink.done.then((_) => this);; | 562 return sink.done.then((_) => this);; |
563 } catch (e) { | 563 } catch (e) { |
564 return new Future.immediateError(e); | 564 return new Future.error(e); |
565 } | 565 } |
566 } | 566 } |
567 | 567 |
568 void writeAsBytesSync(List<int> bytes, {FileMode mode: FileMode.WRITE}) { | 568 void writeAsBytesSync(List<int> bytes, {FileMode mode: FileMode.WRITE}) { |
569 RandomAccessFile opened = openSync(mode: mode); | 569 RandomAccessFile opened = openSync(mode: mode); |
570 opened.writeListSync(bytes, 0, bytes.length); | 570 opened.writeListSync(bytes, 0, bytes.length); |
571 opened.closeSync(); | 571 opened.closeSync(); |
572 } | 572 } |
573 | 573 |
574 Future<File> writeAsString(String contents, | 574 Future<File> writeAsString(String contents, |
(...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1054 new FileIOException("File closed '$_path'")); | 1054 new FileIOException("File closed '$_path'")); |
1055 }); | 1055 }); |
1056 return completer.future; | 1056 return completer.future; |
1057 } | 1057 } |
1058 | 1058 |
1059 final String _path; | 1059 final String _path; |
1060 int _id; | 1060 int _id; |
1061 | 1061 |
1062 SendPort _fileService; | 1062 SendPort _fileService; |
1063 } | 1063 } |
OLD | NEW |