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 : _data = const [], | 7 : _data = const [], |
8 _position = 0, | 8 _position = 0, |
9 _filePosition = 0 { | 9 _filePosition = 0 { |
10 var file = new File(name); | 10 var file = new File(name); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 } | 58 } |
59 | 59 |
60 void _fillBuffer() { | 60 void _fillBuffer() { |
61 Expect.equals(_position, _data.length); | 61 Expect.equals(_position, _data.length); |
62 if (_openedFile == null) return; // Called before the file is opened. | 62 if (_openedFile == null) return; // Called before the file is opened. |
63 int size = min(_bufferLength, _fileLength - _filePosition); | 63 int size = min(_bufferLength, _fileLength - _filePosition); |
64 if (size == 0) { | 64 if (size == 0) { |
65 _closeFile(); | 65 _closeFile(); |
66 return; | 66 return; |
67 } | 67 } |
68 // If there is currently a _fillBuffer call waiting on readList, | 68 // If there is currently a _fillBuffer call waiting on read, |
69 // let it fill the buffer instead of us. | 69 // let it fill the buffer instead of us. |
70 if (_activeFillBufferCall) return; | 70 if (_activeFillBufferCall) return; |
71 _activeFillBufferCall = true; | 71 _activeFillBufferCall = true; |
72 if (_data.length != size) { | 72 var future = _openedFile.read(size); |
73 _data = new Uint8List(size); | 73 future.then((data) { |
74 // Maintain the invariant signalling that the buffer is empty. | 74 _data = data; |
75 _position = _data.length; | |
76 } | |
77 var future = _openedFile.readList(_data, 0, _data.length); | |
78 future.then((read) { | |
79 _filePosition += read; | |
80 if (read != _data.length) { | |
81 _data = _data.getRange(0, read); | |
82 } | |
83 _position = 0; | 75 _position = 0; |
| 76 _filePosition += _data.length; |
84 _activeFillBufferCall = false; | 77 _activeFillBufferCall = false; |
85 | 78 |
86 if (_fileLength == _filePosition) { | 79 if (_fileLength == _filePosition) { |
87 _closeFile(); | 80 _closeFile(); |
88 } | 81 } |
89 _checkScheduleCallbacks(); | 82 _checkScheduleCallbacks(); |
90 }); | 83 }); |
91 future.handleException((e) { | 84 future.handleException((e) { |
92 _activeFillBufferCall = false; | 85 _activeFillBufferCall = false; |
93 _reportError(e); | 86 _reportError(e); |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 const int _CLOSE_REQUEST = 6; | 305 const int _CLOSE_REQUEST = 6; |
313 const int _POSITION_REQUEST = 7; | 306 const int _POSITION_REQUEST = 7; |
314 const int _SET_POSITION_REQUEST = 8; | 307 const int _SET_POSITION_REQUEST = 8; |
315 const int _TRUNCATE_REQUEST = 9; | 308 const int _TRUNCATE_REQUEST = 9; |
316 const int _LENGTH_REQUEST = 10; | 309 const int _LENGTH_REQUEST = 10; |
317 const int _LENGTH_FROM_NAME_REQUEST = 11; | 310 const int _LENGTH_FROM_NAME_REQUEST = 11; |
318 const int _LAST_MODIFIED_REQUEST = 12; | 311 const int _LAST_MODIFIED_REQUEST = 12; |
319 const int _FLUSH_REQUEST = 13; | 312 const int _FLUSH_REQUEST = 13; |
320 const int _READ_BYTE_REQUEST = 14; | 313 const int _READ_BYTE_REQUEST = 14; |
321 const int _WRITE_BYTE_REQUEST = 15; | 314 const int _WRITE_BYTE_REQUEST = 15; |
322 const int _READ_LIST_REQUEST = 16; | 315 const int _READ_REQUEST = 16; |
323 const int _WRITE_LIST_REQUEST = 17; | 316 const int _READ_LIST_REQUEST = 17; |
| 317 const int _WRITE_LIST_REQUEST = 18; |
324 | 318 |
325 // Base class for _File and _RandomAccessFile with shared functions. | 319 // Base class for _File and _RandomAccessFile with shared functions. |
326 class _FileBase { | 320 class _FileBase { |
327 bool _isErrorResponse(response) { | 321 bool _isErrorResponse(response) { |
328 return response is List && response[0] != _SUCCESS_RESPONSE; | 322 return response is List && response[0] != _SUCCESS_RESPONSE; |
329 } | 323 } |
330 | 324 |
331 _exceptionFromResponse(response, String message) { | 325 _exceptionFromResponse(response, String message) { |
332 assert(_isErrorResponse(response)); | 326 assert(_isErrorResponse(response)); |
333 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { | 327 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { |
(...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
780 | 774 |
781 int readByteSync() { | 775 int readByteSync() { |
782 _checkNotClosed(); | 776 _checkNotClosed(); |
783 var result = _readByte(_id); | 777 var result = _readByte(_id); |
784 if (result is OSError) { | 778 if (result is OSError) { |
785 throw new FileIOException("readByte failed for file '$_name'", result); | 779 throw new FileIOException("readByte failed for file '$_name'", result); |
786 } | 780 } |
787 return result; | 781 return result; |
788 } | 782 } |
789 | 783 |
| 784 Future<List<int>> read(int bytes) { |
| 785 _ensureFileService(); |
| 786 Completer<List<int>> completer = new Completer<List<int>>(); |
| 787 if (bytes is !int) { |
| 788 // Complete asynchronously so the user has a chance to setup |
| 789 // handlers without getting exceptions when registering the |
| 790 // then handler. |
| 791 new Timer(0, (t) { |
| 792 completer.completeException(new FileIOException( |
| 793 "Invalid arguments to read for file '$_name'")); |
| 794 }); |
| 795 return completer.future; |
| 796 }; |
| 797 if (closed) return _completeWithClosedException(completer); |
| 798 List request = new List(3); |
| 799 request[0] = _READ_REQUEST; |
| 800 request[1] = _id; |
| 801 request[2] = bytes; |
| 802 return _fileService.call(request).transform((response) { |
| 803 if (_isErrorResponse(response)) { |
| 804 throw _exceptionFromResponse(response, |
| 805 "read failed for file '$_name'"); |
| 806 } |
| 807 return response[1]; |
| 808 }); |
| 809 } |
| 810 |
| 811 external static _read(int id, int bytes); |
| 812 |
| 813 List<int> readSync(int bytes) { |
| 814 if (bytes is !int) { |
| 815 throw new FileIOException( |
| 816 "Invalid arguments to readSync for file '$_name'"); |
| 817 } |
| 818 return _read(_id, bytes); |
| 819 } |
| 820 |
790 Future<int> readList(List<int> buffer, int offset, int bytes) { | 821 Future<int> readList(List<int> buffer, int offset, int bytes) { |
791 _ensureFileService(); | 822 _ensureFileService(); |
792 Completer<int> completer = new Completer<int>(); | 823 Completer<int> completer = new Completer<int>(); |
793 if (buffer is !List || offset is !int || bytes is !int) { | 824 if (buffer is !List || offset is !int || bytes is !int) { |
794 // Complete asynchronously so the user has a chance to setup | 825 // Complete asynchronously so the user has a chance to setup |
795 // handlers without getting exceptions when registering the | 826 // handlers without getting exceptions when registering the |
796 // then handler. | 827 // then handler. |
797 new Timer(0, (t) { | 828 new Timer(0, (t) { |
798 completer.completeException(new FileIOException( | 829 completer.completeException(new FileIOException( |
799 "Invalid arguments to readList for file '$_name'")); | 830 "Invalid arguments to readList for file '$_name'")); |
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1126 new FileIOException("File closed '$_name'")); | 1157 new FileIOException("File closed '$_name'")); |
1127 }); | 1158 }); |
1128 return completer.future; | 1159 return completer.future; |
1129 } | 1160 } |
1130 | 1161 |
1131 final String _name; | 1162 final String _name; |
1132 int _id; | 1163 int _id; |
1133 | 1164 |
1134 SendPort _fileService; | 1165 SendPort _fileService; |
1135 } | 1166 } |
OLD | NEW |