| 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 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 const int _SET_POSITION_REQUEST = 8; | 314 const int _SET_POSITION_REQUEST = 8; |
| 315 const int _TRUNCATE_REQUEST = 9; | 315 const int _TRUNCATE_REQUEST = 9; |
| 316 const int _LENGTH_REQUEST = 10; | 316 const int _LENGTH_REQUEST = 10; |
| 317 const int _LENGTH_FROM_NAME_REQUEST = 11; | 317 const int _LENGTH_FROM_NAME_REQUEST = 11; |
| 318 const int _LAST_MODIFIED_REQUEST = 12; | 318 const int _LAST_MODIFIED_REQUEST = 12; |
| 319 const int _FLUSH_REQUEST = 13; | 319 const int _FLUSH_REQUEST = 13; |
| 320 const int _READ_BYTE_REQUEST = 14; | 320 const int _READ_BYTE_REQUEST = 14; |
| 321 const int _WRITE_BYTE_REQUEST = 15; | 321 const int _WRITE_BYTE_REQUEST = 15; |
| 322 const int _READ_LIST_REQUEST = 16; | 322 const int _READ_LIST_REQUEST = 16; |
| 323 const int _WRITE_LIST_REQUEST = 17; | 323 const int _WRITE_LIST_REQUEST = 17; |
| 324 const int _WRITE_STRING_REQUEST = 18; | |
| 325 | 324 |
| 326 // Base class for _File and _RandomAccessFile with shared functions. | 325 // Base class for _File and _RandomAccessFile with shared functions. |
| 327 class _FileBase { | 326 class _FileBase { |
| 328 bool _isErrorResponse(response) { | 327 bool _isErrorResponse(response) { |
| 329 return response is List && response[0] != _SUCCESS_RESPONSE; | 328 return response is List && response[0] != _SUCCESS_RESPONSE; |
| 330 } | 329 } |
| 331 | 330 |
| 332 _exceptionFromResponse(response, String message) { | 331 _exceptionFromResponse(response, String message) { |
| 333 assert(_isErrorResponse(response)); | 332 assert(_isErrorResponse(response)); |
| 334 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { | 333 switch (response[_ERROR_RESPONSE_ERROR_TYPE]) { |
| (...skipping 562 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 897 var result = | 896 var result = |
| 898 _writeList(_id, bufferAndOffset.buffer, bufferAndOffset.offset, bytes); | 897 _writeList(_id, bufferAndOffset.buffer, bufferAndOffset.offset, bytes); |
| 899 if (result is OSError) { | 898 if (result is OSError) { |
| 900 throw new FileIOException("writeList failed for file '$_name'", result); | 899 throw new FileIOException("writeList failed for file '$_name'", result); |
| 901 } | 900 } |
| 902 return result; | 901 return result; |
| 903 } | 902 } |
| 904 | 903 |
| 905 Future<RandomAccessFile> writeString(String string, | 904 Future<RandomAccessFile> writeString(String string, |
| 906 [Encoding encoding = Encoding.UTF_8]) { | 905 [Encoding encoding = Encoding.UTF_8]) { |
| 907 _ensureFileService(); | 906 if (encoding is! Encoding) { |
| 908 Completer<RandomAccessFile> completer = new Completer<RandomAccessFile>(); | 907 var completer = new Completer(); |
| 909 if (closed) return _completeWithClosedException(completer); | 908 new Timer(0, (t) { |
| 910 List request = new List(3); | 909 completer.completeException(new FileIOException( |
| 911 request[0] = _WRITE_STRING_REQUEST; | 910 "Invalid encoding in writeString: $encoding")); |
| 912 request[1] = _id; | 911 }); |
| 913 request[2] = string; | 912 return completer.future; |
| 914 return _fileService.call(request).transform((response) { | 913 } |
| 915 if (_isErrorResponse(response)) { | 914 var data = _StringEncoders.encoder(encoding).encodeString(string); |
| 916 throw _exceptionFromResponse(response, | 915 return writeList(data, 0, data.length); |
| 917 "writeString failed for file '$_name'"); | |
| 918 } | |
| 919 return this; | |
| 920 }); | |
| 921 } | 916 } |
| 922 | 917 |
| 923 external static _writeString(int id, String string); | |
| 924 | |
| 925 int writeStringSync(String string, [Encoding encoding = Encoding.UTF_8]) { | 918 int writeStringSync(String string, [Encoding encoding = Encoding.UTF_8]) { |
| 926 _checkNotClosed(); | 919 if (encoding is! Encoding) { |
| 927 if (string is !String) throw new ArgumentError(); | 920 throw new FileIOException( |
| 928 var result = _writeString(_id, string); | 921 "Invalid encoding in writeStringSync: $encoding"); |
| 929 if (result is OSError) { | |
| 930 throw new FileIOException("writeString failed for file '$_name'"); | |
| 931 } | 922 } |
| 932 return result; | 923 var data = _StringEncoders.encoder(encoding).encodeString(string); |
| 924 return writeListSync(data, 0, data.length); |
| 933 } | 925 } |
| 934 | 926 |
| 935 Future<int> position() { | 927 Future<int> position() { |
| 936 _ensureFileService(); | 928 _ensureFileService(); |
| 937 Completer<int> completer = new Completer<int>(); | 929 Completer<int> completer = new Completer<int>(); |
| 938 if (closed) return _completeWithClosedException(completer); | 930 if (closed) return _completeWithClosedException(completer); |
| 939 List request = new List(2); | 931 List request = new List(2); |
| 940 request[0] = _POSITION_REQUEST; | 932 request[0] = _POSITION_REQUEST; |
| 941 request[1] = _id; | 933 request[1] = _id; |
| 942 return _fileService.call(request).transform((response) { | 934 return _fileService.call(request).transform((response) { |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1088 new FileIOException("File closed '$_name'")); | 1080 new FileIOException("File closed '$_name'")); |
| 1089 }); | 1081 }); |
| 1090 return completer.future; | 1082 return completer.future; |
| 1091 } | 1083 } |
| 1092 | 1084 |
| 1093 final String _name; | 1085 final String _name; |
| 1094 int _id; | 1086 int _id; |
| 1095 | 1087 |
| 1096 SendPort _fileService; | 1088 SendPort _fileService; |
| 1097 } | 1089 } |
| OLD | NEW |