| 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 }); | 56 }); |
| 57 } | 57 } |
| 58 | 58 |
| 59 Future _closeFile() { | 59 Future _closeFile() { |
| 60 Future closeFuture; | 60 Future closeFuture; |
| 61 if (_openedFile != null) { | 61 if (_openedFile != null) { |
| 62 Future closeFuture = _openedFile.close(); | 62 Future closeFuture = _openedFile.close(); |
| 63 _openedFile = null; | 63 _openedFile = null; |
| 64 return closeFuture; | 64 return closeFuture; |
| 65 } else { | 65 } else { |
| 66 return new Future.immediate(null); | 66 return new Future.value(); |
| 67 } | 67 } |
| 68 } | 68 } |
| 69 | 69 |
| 70 void _readBlock() { | 70 void _readBlock() { |
| 71 // Don't start a new read if one is already in progress. | 71 // Don't start a new read if one is already in progress. |
| 72 if (_readInProgress) return; | 72 if (_readInProgress) return; |
| 73 _readInProgress = true; | 73 _readInProgress = true; |
| 74 _openedFile.length() | 74 _openedFile.length() |
| 75 .then((length) { | 75 .then((length) { |
| 76 if (_position >= length) { | 76 if (_position >= length) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 104 _unsubscribed = true; | 104 _unsubscribed = true; |
| 105 } | 105 } |
| 106 }); | 106 }); |
| 107 } | 107 } |
| 108 | 108 |
| 109 void _start() { | 109 void _start() { |
| 110 Future<RandomAccessFile> openFuture; | 110 Future<RandomAccessFile> openFuture; |
| 111 if (_path != null) { | 111 if (_path != null) { |
| 112 openFuture = new File(_path).open(mode: FileMode.READ); | 112 openFuture = new File(_path).open(mode: FileMode.READ); |
| 113 } else { | 113 } else { |
| 114 openFuture = new Future.immediate(_File._openStdioSync(0)); | 114 openFuture = new Future.value(_File._openStdioSync(0)); |
| 115 } | 115 } |
| 116 openFuture | 116 openFuture |
| 117 .then((RandomAccessFile opened) { | 117 .then((RandomAccessFile opened) { |
| 118 _openedFile = opened; | 118 _openedFile = opened; |
| 119 _readBlock(); | 119 _readBlock(); |
| 120 }) | 120 }) |
| 121 .catchError((e) { | 121 .catchError((e) { |
| 122 _controller.addError(e); | 122 _controller.addError(e); |
| 123 _controller.close(); | 123 _controller.close(); |
| 124 }); | 124 }); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 139 File _file; | 139 File _file; |
| 140 Future<RandomAccessFile> _openFuture; | 140 Future<RandomAccessFile> _openFuture; |
| 141 StreamSubscription _subscription; | 141 StreamSubscription _subscription; |
| 142 | 142 |
| 143 _FileStreamConsumer(File this._file, FileMode mode) { | 143 _FileStreamConsumer(File this._file, FileMode mode) { |
| 144 _openFuture = _file.open(mode: mode); | 144 _openFuture = _file.open(mode: mode); |
| 145 } | 145 } |
| 146 | 146 |
| 147 _FileStreamConsumer.fromStdio(int fd) { | 147 _FileStreamConsumer.fromStdio(int fd) { |
| 148 assert(1 <= fd && fd <= 2); | 148 assert(1 <= fd && fd <= 2); |
| 149 _openFuture = new Future.immediate(_File._openStdioSync(fd)); | 149 _openFuture = new Future.value(_File._openStdioSync(fd)); |
| 150 } | 150 } |
| 151 | 151 |
| 152 Future<File> addStream(Stream<List<int>> stream) { | 152 Future<File> addStream(Stream<List<int>> stream) { |
| 153 Completer<File> completer = new Completer<File>(); | 153 Completer<File> completer = new Completer<File>(); |
| 154 _openFuture | 154 _openFuture |
| 155 .then((openedFile) { | 155 .then((openedFile) { |
| 156 _subscription = stream.listen( | 156 _subscription = stream.listen( |
| 157 (d) { | 157 (d) { |
| 158 _subscription.pause(); | 158 _subscription.pause(); |
| 159 openedFile.writeFrom(d, 0, d.length) | 159 openedFile.writeFrom(d, 0, d.length) |
| (...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 } | 538 } |
| 539 | 539 |
| 540 Future<File> writeAsBytes(List<int> bytes, | 540 Future<File> writeAsBytes(List<int> bytes, |
| 541 {FileMode mode: FileMode.WRITE}) { | 541 {FileMode mode: FileMode.WRITE}) { |
| 542 try { | 542 try { |
| 543 IOSink sink = openWrite(mode: mode); | 543 IOSink sink = openWrite(mode: mode); |
| 544 sink.add(bytes); | 544 sink.add(bytes); |
| 545 sink.close(); | 545 sink.close(); |
| 546 return sink.done.then((_) => this);; | 546 return sink.done.then((_) => this);; |
| 547 } catch (e) { | 547 } catch (e) { |
| 548 return new Future.immediateError(e); | 548 return new Future.error(e); |
| 549 } | 549 } |
| 550 } | 550 } |
| 551 | 551 |
| 552 void writeAsBytesSync(List<int> bytes, {FileMode mode: FileMode.WRITE}) { | 552 void writeAsBytesSync(List<int> bytes, {FileMode mode: FileMode.WRITE}) { |
| 553 RandomAccessFile opened = openSync(mode: mode); | 553 RandomAccessFile opened = openSync(mode: mode); |
| 554 opened.writeFromSync(bytes, 0, bytes.length); | 554 opened.writeFromSync(bytes, 0, bytes.length); |
| 555 opened.closeSync(); | 555 opened.closeSync(); |
| 556 } | 556 } |
| 557 | 557 |
| 558 Future<File> writeAsString(String contents, | 558 Future<File> writeAsString(String contents, |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 697 result); | 697 result); |
| 698 } | 698 } |
| 699 return result; | 699 return result; |
| 700 } | 700 } |
| 701 | 701 |
| 702 Future<int> readInto(List<int> buffer, [int start, int end]) { | 702 Future<int> readInto(List<int> buffer, [int start, int end]) { |
| 703 _ensureFileService(); | 703 _ensureFileService(); |
| 704 if (buffer is !List || | 704 if (buffer is !List || |
| 705 (start != null && start is !int) || | 705 (start != null && start is !int) || |
| 706 (end != null && end is !int)) { | 706 (end != null && end is !int)) { |
| 707 return new Future.immediateError(new FileIOException( | 707 return new Future.error(new FileIOException( |
| 708 "Invalid arguments to readInto for file '$_path'")); | 708 "Invalid arguments to readInto for file '$_path'")); |
| 709 }; | 709 }; |
| 710 Completer<int> completer = new Completer<int>(); | 710 Completer<int> completer = new Completer<int>(); |
| 711 if (closed) return _completeWithClosedException(completer); | 711 if (closed) return _completeWithClosedException(completer); |
| 712 List request = new List(3); | 712 List request = new List(3); |
| 713 if (start == null) start = 0; | 713 if (start == null) start = 0; |
| 714 if (end == null) end = buffer.length; | 714 if (end == null) end = buffer.length; |
| 715 request[0] = _READ_LIST_REQUEST; | 715 request[0] = _READ_LIST_REQUEST; |
| 716 request[1] = _id; | 716 request[1] = _id; |
| 717 request[2] = end - start; | 717 request[2] = end - start; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 798 result); | 798 result); |
| 799 } | 799 } |
| 800 return result; | 800 return result; |
| 801 } | 801 } |
| 802 | 802 |
| 803 Future<RandomAccessFile> writeFrom(List<int> buffer, [int start, int end]) { | 803 Future<RandomAccessFile> writeFrom(List<int> buffer, [int start, int end]) { |
| 804 _ensureFileService(); | 804 _ensureFileService(); |
| 805 if ((buffer is !List && buffer is !ByteData) || | 805 if ((buffer is !List && buffer is !ByteData) || |
| 806 (start != null && start is !int) || | 806 (start != null && start is !int) || |
| 807 (end != null && end is !int)) { | 807 (end != null && end is !int)) { |
| 808 return new Future.immediateError(new FileIOException( | 808 return new Future.error(new FileIOException( |
| 809 "Invalid arguments to writeFrom for file '$_path'")); | 809 "Invalid arguments to writeFrom for file '$_path'")); |
| 810 } | 810 } |
| 811 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 811 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); |
| 812 | 812 |
| 813 if (closed) return _completeWithClosedException(completer); | 813 if (closed) return _completeWithClosedException(completer); |
| 814 | 814 |
| 815 _BufferAndStart result; | 815 _BufferAndStart result; |
| 816 try { | 816 try { |
| 817 result = _ensureFastAndSerializableBuffer(buffer, start, end); | 817 result = _ensureFastAndSerializableBuffer(buffer, start, end); |
| 818 } catch (e) { | 818 } catch (e) { |
| 819 return new Future.immediateError(e); | 819 return new Future.error(e); |
| 820 } | 820 } |
| 821 | 821 |
| 822 List request = new List(5); | 822 List request = new List(5); |
| 823 request[0] = _WRITE_LIST_REQUEST; | 823 request[0] = _WRITE_LIST_REQUEST; |
| 824 request[1] = _id; | 824 request[1] = _id; |
| 825 request[2] = result.buffer; | 825 request[2] = result.buffer; |
| 826 request[3] = result.start; | 826 request[3] = result.start; |
| 827 request[4] = end - (start - result.start); | 827 request[4] = end - (start - result.start); |
| 828 return _fileService.call(request).then((response) { | 828 return _fileService.call(request).then((response) { |
| 829 if (_isErrorResponse(response)) { | 829 if (_isErrorResponse(response)) { |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1038 new FileIOException("File closed '$_path'")); | 1038 new FileIOException("File closed '$_path'")); |
| 1039 }); | 1039 }); |
| 1040 return completer.future; | 1040 return completer.future; |
| 1041 } | 1041 } |
| 1042 | 1042 |
| 1043 final String _path; | 1043 final String _path; |
| 1044 int _id; | 1044 int _id; |
| 1045 | 1045 |
| 1046 SendPort _fileService; | 1046 SendPort _fileService; |
| 1047 } | 1047 } |
| OLD | NEW |