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 = (String s) { | 10 _file.onError = (String s) { |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
244 "List element is not an integer at index $j"); | 244 "List element is not an integer at index $j"); |
245 } | 245 } |
246 outBuffer[i] = value; | 246 outBuffer[i] = value; |
247 j++; | 247 j++; |
248 } | 248 } |
249 } | 249 } |
250 return [outBuffer, outOffset]; | 250 return [outBuffer, outOffset]; |
251 } | 251 } |
252 | 252 |
253 static bool exists(String name) native "File_Exists"; | 253 static bool exists(String name) native "File_Exists"; |
254 static int open(String name, int mode) native "File_Open"; | 254 static int open(String name, int mode, OSError error) native "File_Open"; |
255 static bool create(String name) native "File_Create"; | 255 static bool create(String name) native "File_Create"; |
256 static bool delete(String name) native "File_Delete"; | 256 static bool delete(String name) native "File_Delete"; |
257 static String fullPath(String name) native "File_FullPath"; | 257 static String fullPath(String name) native "File_FullPath"; |
258 static String directory(String name) native "File_Directory"; | 258 static String directory(String name) native "File_Directory"; |
259 static int close(int id) native "File_Close"; | 259 static int close(int id) native "File_Close"; |
260 static int readByte(int id) native "File_ReadByte"; | 260 static int readByte(int id) native "File_ReadByte"; |
261 static int readList(int id, List<int> buffer, int offset, int bytes) | 261 static int readList(int id, List<int> buffer, int offset, int bytes) |
262 native "File_ReadList"; | 262 native "File_ReadList"; |
263 static int writeByte(int id, int value) native "File_WriteByte"; | 263 static int writeByte(int id, int value) native "File_WriteByte"; |
264 static int writeList(int id, List<int> buffer, int offset, int bytes) { | 264 static int writeList(int id, List<int> buffer, int offset, int bytes) { |
265 List result = | 265 List result = |
266 _FileUtils.ensureFastAndSerializableBuffer(buffer, offset, bytes); | 266 _FileUtils.ensureFastAndSerializableBuffer(buffer, offset, bytes); |
267 List outBuffer = result[0]; | 267 List outBuffer = result[0]; |
268 int outOffset = result[1]; | 268 int outOffset = result[1]; |
269 return writeListNative(id, outBuffer, outOffset, bytes); | 269 return writeListNative(id, outBuffer, outOffset, bytes); |
270 } | 270 } |
271 static int writeListNative(int id, List<int> buffer, int offset, int bytes) | 271 static int writeListNative(int id, List<int> buffer, int offset, int bytes) |
272 native "File_WriteList"; | 272 native "File_WriteList"; |
273 static int writeString(int id, String string) native "File_WriteString"; | 273 static int writeString(int id, String string) native "File_WriteString"; |
274 static int position(int id) native "File_Position"; | 274 static int position(int id) native "File_Position"; |
275 static bool setPosition(int id, int position) native "File_SetPosition"; | 275 static bool setPosition(int id, int position) native "File_SetPosition"; |
276 static bool truncate(int id, int length) native "File_Truncate"; | 276 static bool truncate(int id, int length) native "File_Truncate"; |
277 static int length(int id) native "File_Length"; | 277 static int length(int id) native "File_Length"; |
278 static int flush(int id) native "File_Flush"; | 278 static int flush(int id) native "File_Flush"; |
279 static int openStdio(int fd) native "File_OpenStdio"; | 279 static int openStdio(int fd) native "File_OpenStdio"; |
280 static SendPort newServicePort() native "File_NewServicePort"; | 280 static SendPort newServicePort() native "File_NewServicePort"; |
281 | 281 |
282 static int checkedOpen(String name, int mode) { | 282 static int checkedOpen(String name, int mode) { |
283 if (name is !String || mode is !int) return 0; | 283 if (name is !String || mode is !int) return 0; |
284 return open(name, mode); | 284 var osError = new OSError(); |
285 int id = open(name, mode, osError); | |
286 if (id == 0) { | |
287 throw new FileIOException("Cannot open file", osError); | |
288 } | |
289 return id; | |
285 } | 290 } |
286 | 291 |
287 static bool checkedCreate(String name) { | 292 static bool checkedCreate(String name) { |
288 if (name is !String) return false; | 293 if (name is !String) return false; |
289 return create(name); | 294 return create(name); |
290 } | 295 } |
291 | 296 |
292 static bool checkedDelete(String name) { | 297 static bool checkedDelete(String name) { |
293 if (name is !String) return false; | 298 if (name is !String) return false; |
294 return delete(name); | 299 return delete(name); |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
433 if (_onError != null) { | 438 if (_onError != null) { |
434 _onError("Unknown file mode. Use FileMode.READ, FileMode.WRITE " + | 439 _onError("Unknown file mode. Use FileMode.READ, FileMode.WRITE " + |
435 "or FileMode.APPEND."); | 440 "or FileMode.APPEND."); |
436 return; | 441 return; |
437 } | 442 } |
438 } | 443 } |
439 List request = new List(3); | 444 List request = new List(3); |
440 request[0] = _FileUtils.kOpenRequest; | 445 request[0] = _FileUtils.kOpenRequest; |
441 request[1] = _name; | 446 request[1] = _name; |
442 request[2] = mode._mode; // Direct int value for serialization. | 447 request[2] = mode._mode; // Direct int value for serialization. |
443 _fileService.call(request).receive((id, replyTo) { | 448 _fileService.call(request).receive((result, replyTo) { |
444 if (id != 0) { | 449 if (result is Array) { |
450 if (_onError != null) { | |
451 //_onError("Cannot open file: ${result[0]} ${result[1]}"); | |
Mads Ager (google)
2012/03/08 15:04:27
Code in comments and long line.
Søren Gjesse
2012/03/08 22:50:53
Done.
| |
452 _onError(new FileIOException("Cannot open file", new OSError(result[1] , result[0]))); | |
453 } | |
454 } else { | |
445 callback(new _RandomAccessFile(id, _name)); | 455 callback(new _RandomAccessFile(id, _name)); |
446 } else if (_onError != null) { | |
447 _onError("Cannot open file: $_name"); | |
448 } | 456 } |
449 }); | 457 }); |
450 } | 458 } |
451 | 459 |
452 RandomAccessFile openSync([FileMode mode = FileMode.READ]) { | 460 RandomAccessFile openSync([FileMode mode = FileMode.READ]) { |
453 if (_asyncUsed) { | 461 if (_asyncUsed) { |
454 throw new FileIOException( | 462 throw new FileIOException( |
455 "Mixed use of synchronous and asynchronous API"); | 463 "Mixed use of synchronous and asynchronous API"); |
456 } | 464 } |
457 if (mode != FileMode.READ && | 465 if (mode != FileMode.READ && |
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1054 bool _asyncUsed; | 1062 bool _asyncUsed; |
1055 int _pendingWrites = 0; | 1063 int _pendingWrites = 0; |
1056 | 1064 |
1057 SendPort _fileService; | 1065 SendPort _fileService; |
1058 | 1066 |
1059 Timer _noPendingWriteTimer; | 1067 Timer _noPendingWriteTimer; |
1060 | 1068 |
1061 Function _onNoPendingWrites; | 1069 Function _onNoPendingWrites; |
1062 Function _onError; | 1070 Function _onError; |
1063 } | 1071 } |
OLD | NEW |