| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 class _FileInputStream extends _BaseDataInputStream implements InputStream { | 5 class _FileInputStream extends _BaseDataInputStream implements InputStream { |
| 6 _FileInputStream(String name) { | 6 _FileInputStream(String name) { |
| 7 _file = new File(name); | 7 _file = new File(name); |
| 8 _data = []; | 8 _data = []; |
| 9 _position = 0; | 9 _position = 0; |
| 10 _file.onError = (e) { | 10 _file.onError = (e) { |
| 11 if (_clientErrorHandler != null) { | 11 if (_clientErrorHandler != null) { |
| 12 _clientErrorHandler(); | 12 _clientErrorHandler(e); |
| 13 } | 13 } |
| 14 }; | 14 }; |
| 15 _file.open(FileMode.READ, (openedFile) { | 15 _file.open(FileMode.READ, (openedFile) { |
| 16 _readDataFromFile(openedFile); | 16 _readDataFromFile(openedFile); |
| 17 }); | 17 }); |
| 18 } | 18 } |
| 19 | 19 |
| 20 _FileInputStream.fromStdio(int fd) { | 20 _FileInputStream.fromStdio(int fd) { |
| 21 assert(fd == 0); | 21 assert(fd == 0); |
| 22 _file = _File._openStdioSync(fd); | 22 _file = _File._openStdioSync(fd); |
| 23 _data = []; | 23 _data = []; |
| 24 _position = 0; | 24 _position = 0; |
| 25 _readDataFromFile(_file); | 25 _readDataFromFile(_file); |
| 26 } | 26 } |
| 27 | 27 |
| 28 void _readDataFromFile(RandomAccessFile openedFile) { | 28 void _readDataFromFile(RandomAccessFile openedFile) { |
| 29 openedFile.onError = (e) { | 29 openedFile.onError = (e) { |
| 30 if (_clientErrorHandler != null) { | 30 if (_clientErrorHandler != null) { |
| 31 _clientErrorHandler(); | 31 _clientErrorHandler(e); |
| 32 } | 32 } |
| 33 }; | 33 }; |
| 34 openedFile.length((length) { | 34 openedFile.length((length) { |
| 35 var contents = new ByteArray(length); | 35 var contents = new ByteArray(length); |
| 36 if (length != 0) { | 36 if (length != 0) { |
| 37 openedFile.readList(contents, 0, length, (read) { | 37 openedFile.readList(contents, 0, length, (read) { |
| 38 if (read != length) { | 38 if (read != length) { |
| 39 if (_clientErrorHandler != null) { | 39 if (_clientErrorHandler != null) { |
| 40 _clientErrorHandler(); | 40 _clientErrorHandler(); |
| 41 } | 41 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 class _FileOutputStream extends _BaseOutputStream implements OutputStream { | 94 class _FileOutputStream extends _BaseOutputStream implements OutputStream { |
| 95 _FileOutputStream(String name, FileMode mode) { | 95 _FileOutputStream(String name, FileMode mode) { |
| 96 _pendingOperations = new List<List<int>>(); | 96 _pendingOperations = new List<List<int>>(); |
| 97 var f = new File(name); | 97 var f = new File(name); |
| 98 f.open(mode, (openedFile) { | 98 f.open(mode, (openedFile) { |
| 99 _file = openedFile; | 99 _file = openedFile; |
| 100 _setupFileHandlers(); | 100 _setupFileHandlers(); |
| 101 _processPendingOperations(); | 101 _processPendingOperations(); |
| 102 }); | 102 }); |
| 103 f.onError = (e) { | 103 f.onError = (e) { |
| 104 if (_onError != null) _onError(); | 104 if (_onError != null) _onError(e); |
| 105 }; | 105 }; |
| 106 } | 106 } |
| 107 | 107 |
| 108 _FileOutputStream.fromStdio(int fd) { | 108 _FileOutputStream.fromStdio(int fd) { |
| 109 assert(1 <= fd && fd <= 2); | 109 assert(1 <= fd && fd <= 2); |
| 110 _file = _File._openStdioSync(fd); | 110 _file = _File._openStdioSync(fd); |
| 111 _setupFileHandlers(); | 111 _setupFileHandlers(); |
| 112 } | 112 } |
| 113 | 113 |
| 114 | 114 |
| 115 void _setupFileHandlers() { | 115 void _setupFileHandlers() { |
| 116 _file.onError = (e) { | 116 _file.onError = (e) { |
| 117 if (_onError != null) _onError(); | 117 if (_onError != null) _onError(e); |
| 118 }; | 118 }; |
| 119 _file.onNoPendingWrites = () { | 119 _file.onNoPendingWrites = () { |
| 120 if (!_streamMarkedClosed && _onNoPendingWrites != null) { | 120 if (!_streamMarkedClosed && _onNoPendingWrites != null) { |
| 121 _onNoPendingWrites(); | 121 _onNoPendingWrites(); |
| 122 } | 122 } |
| 123 }; | 123 }; |
| 124 } | 124 } |
| 125 | 125 |
| 126 bool write(List<int> buffer, [bool copyBuffer = false]) { | 126 bool write(List<int> buffer, [bool copyBuffer = false]) { |
| 127 var data = buffer; | 127 var data = buffer; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 } | 162 } |
| 163 | 163 |
| 164 void set onNoPendingWrites(void callback()) { | 164 void set onNoPendingWrites(void callback()) { |
| 165 _onNoPendingWrites = callback; | 165 _onNoPendingWrites = callback; |
| 166 } | 166 } |
| 167 | 167 |
| 168 void set onClosed(void callback()) { | 168 void set onClosed(void callback()) { |
| 169 _onClosed = callback; | 169 _onClosed = callback; |
| 170 } | 170 } |
| 171 | 171 |
| 172 void set onError(void callback()) { | 172 void set onError(void callback(Exception e)) { |
| 173 _onError = callback; | 173 _onError = callback; |
| 174 } | 174 } |
| 175 | 175 |
| 176 void _processPendingOperations() { | 176 void _processPendingOperations() { |
| 177 _pendingOperations.forEach((buffer) { | 177 _pendingOperations.forEach((buffer) { |
| 178 (buffer != null) ? write(buffer) : close(); | 178 (buffer != null) ? write(buffer) : close(); |
| 179 }); | 179 }); |
| 180 _pendingOperations = null; | 180 _pendingOperations = null; |
| 181 } | 181 } |
| 182 | 182 |
| (...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 553 _asyncUsed = true; | 553 _asyncUsed = true; |
| 554 var chunks = new _BufferList(); | 554 var chunks = new _BufferList(); |
| 555 var stream = openInputStream(); | 555 var stream = openInputStream(); |
| 556 stream.onClosed = () { | 556 stream.onClosed = () { |
| 557 callback(chunks.readBytes(chunks.length)); | 557 callback(chunks.readBytes(chunks.length)); |
| 558 }; | 558 }; |
| 559 stream.onData = () { | 559 stream.onData = () { |
| 560 var chunk = stream.read(); | 560 var chunk = stream.read(); |
| 561 chunks.add(chunk); | 561 chunks.add(chunk); |
| 562 }; | 562 }; |
| 563 stream.onError = () { | 563 stream.onError = (e) { |
| 564 if (_onError != null) { | 564 if (_onError != null) { |
| 565 _onError("Failed to read file"); | 565 _onError(e); |
| 566 } | 566 } |
| 567 }; | 567 }; |
| 568 } | 568 } |
| 569 | 569 |
| 570 List<int> readAsBytesSync() { | 570 List<int> readAsBytesSync() { |
| 571 if (_asyncUsed) { | 571 if (_asyncUsed) { |
| 572 throw new FileIOException( | 572 throw new FileIOException( |
| 573 "Mixed use of synchronous and asynchronous API"); | 573 "Mixed use of synchronous and asynchronous API"); |
| 574 } | 574 } |
| 575 var opened = openSync(); | 575 var opened = openSync(); |
| 576 var length = opened.lengthSync(); | 576 var length = opened.lengthSync(); |
| 577 var result = new ByteArray(length); | 577 var result = new ByteArray(length); |
| 578 var read = opened.readListSync(result, 0, length); | 578 var read = opened.readListSync(result, 0, length); |
| 579 if (read != length) { | 579 if (read != length) { |
| 580 throw new FileIOException("Failed to read file"); | 580 throw new FileIOException("Failed to read file"); |
| 581 } | 581 } |
| 582 opened.closeSync(); | 582 opened.closeSync(); |
| 583 return result; | 583 return result; |
| 584 } | 584 } |
| 585 | 585 |
| 586 void readAsText(Encoding encoding, void callback(String text)) { | 586 void readAsText(Encoding encoding, void callback(String text)) { |
| 587 _asyncUsed = true; | 587 _asyncUsed = true; |
| 588 var decoder = _StringDecoders.decoder(encoding); | 588 var decoder = _StringDecoders.decoder(encoding); |
| 589 readAsBytes((bytes) { | 589 readAsBytes((bytes) { |
| 590 try { | 590 try { |
| 591 decoder.write(bytes); | 591 decoder.write(bytes); |
| 592 } catch (var e) { | 592 } catch (var e) { |
| 593 if (_onError != null) { | 593 if (_onError != null) { |
| 594 _onError(e.toString()); | 594 _onError(e); |
| 595 return; | 595 return; |
| 596 } | 596 } |
| 597 } | 597 } |
| 598 callback(decoder.decoded); | 598 callback(decoder.decoded); |
| 599 }); | 599 }); |
| 600 } | 600 } |
| 601 | 601 |
| 602 String readAsTextSync([Encoding encoding = Encoding.UTF_8]) { | 602 String readAsTextSync([Encoding encoding = Encoding.UTF_8]) { |
| 603 if (_asyncUsed) { | 603 if (_asyncUsed) { |
| 604 throw new FileIOException( | 604 throw new FileIOException( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 627 } | 627 } |
| 628 | 628 |
| 629 void readAsLines(Encoding encoding, void callback(List<String> lines)) { | 629 void readAsLines(Encoding encoding, void callback(List<String> lines)) { |
| 630 _asyncUsed = true; | 630 _asyncUsed = true; |
| 631 var decoder = _StringDecoders.decoder(encoding); | 631 var decoder = _StringDecoders.decoder(encoding); |
| 632 readAsBytes((bytes) { | 632 readAsBytes((bytes) { |
| 633 try { | 633 try { |
| 634 decoder.write(bytes); | 634 decoder.write(bytes); |
| 635 } catch (var e) { | 635 } catch (var e) { |
| 636 if (_onError != null) { | 636 if (_onError != null) { |
| 637 _onError(e.toString()); | 637 _onError(e); |
| 638 return; | 638 return; |
| 639 } | 639 } |
| 640 } | 640 } |
| 641 callback(_getDecodedLines(decoder)); | 641 callback(_getDecodedLines(decoder)); |
| 642 }); | 642 }); |
| 643 } | 643 } |
| 644 | 644 |
| 645 List<String> readAsLinesSync([Encoding encoding = Encoding.UTF_8]) { | 645 List<String> readAsLinesSync([Encoding encoding = Encoding.UTF_8]) { |
| 646 if (_asyncUsed) { | 646 if (_asyncUsed) { |
| 647 throw new FileIOException( | 647 throw new FileIOException( |
| (...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1098 bool _asyncUsed; | 1098 bool _asyncUsed; |
| 1099 int _pendingWrites = 0; | 1099 int _pendingWrites = 0; |
| 1100 | 1100 |
| 1101 SendPort _fileService; | 1101 SendPort _fileService; |
| 1102 | 1102 |
| 1103 Timer _noPendingWriteTimer; | 1103 Timer _noPendingWriteTimer; |
| 1104 | 1104 |
| 1105 Function _onNoPendingWrites; | 1105 Function _onNoPendingWrites; |
| 1106 Function _onError; | 1106 Function _onError; |
| 1107 } | 1107 } |
| OLD | NEW |