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 open(String name, int mode) native "File_Open"; |
Mads Ager (google)
2012/03/09 09:40:13
Do we want 'void' here?
Søren Gjesse
2012/03/13 08:25:55
No, open now returns either an int or an instance
| |
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) { |
284 return open(name, mode); | 284 throw new IllegalArgumentException(); |
285 }; | |
286 var result = open(name, mode); | |
287 if (result is OSError) { | |
288 throw new FileIOException("Cannot open file", result); | |
289 } | |
290 return result; | |
285 } | 291 } |
286 | 292 |
287 static bool checkedCreate(String name) { | 293 static bool checkedCreate(String name) { |
288 if (name is !String) return false; | 294 if (name is !String) return false; |
289 return create(name); | 295 return create(name); |
290 } | 296 } |
291 | 297 |
292 static bool checkedDelete(String name) { | 298 static bool checkedDelete(String name) { |
293 if (name is !String) return false; | 299 if (name is !String) return false; |
294 return delete(name); | 300 return delete(name); |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
433 if (_onError != null) { | 439 if (_onError != null) { |
434 _onError("Unknown file mode. Use FileMode.READ, FileMode.WRITE " + | 440 _onError("Unknown file mode. Use FileMode.READ, FileMode.WRITE " + |
435 "or FileMode.APPEND."); | 441 "or FileMode.APPEND."); |
436 return; | 442 return; |
437 } | 443 } |
438 } | 444 } |
439 List request = new List(3); | 445 List request = new List(3); |
440 request[0] = _FileUtils.kOpenRequest; | 446 request[0] = _FileUtils.kOpenRequest; |
441 request[1] = _name; | 447 request[1] = _name; |
442 request[2] = mode._mode; // Direct int value for serialization. | 448 request[2] = mode._mode; // Direct int value for serialization. |
443 _fileService.call(request).receive((id, replyTo) { | 449 _fileService.call(request).receive((result, replyTo) { |
444 if (id != 0) { | 450 if (result is Array) { |
451 if (_onError != null) { | |
452 _onError(new FileIOException( | |
453 "Cannot open file", new OSError(result[1], result[0]))); | |
454 } | |
455 } else { | |
445 callback(new _RandomAccessFile(id, _name)); | 456 callback(new _RandomAccessFile(id, _name)); |
446 } else if (_onError != null) { | |
447 _onError("Cannot open file: $_name"); | |
448 } | 457 } |
449 }); | 458 }); |
450 } | 459 } |
451 | 460 |
452 RandomAccessFile openSync([FileMode mode = FileMode.READ]) { | 461 RandomAccessFile openSync([FileMode mode = FileMode.READ]) { |
453 if (_asyncUsed) { | 462 if (_asyncUsed) { |
454 throw new FileIOException( | 463 throw new FileIOException( |
455 "Mixed use of synchronous and asynchronous API"); | 464 "Mixed use of synchronous and asynchronous API"); |
456 } | 465 } |
457 if (mode != FileMode.READ && | 466 if (mode != FileMode.READ && |
458 mode != FileMode.WRITE && | 467 mode != FileMode.WRITE && |
459 mode != FileMode.APPEND) { | 468 mode != FileMode.APPEND) { |
460 throw new FileIOException("Unknown file mode. Use FileMode.READ, " + | 469 throw new FileIOException("Unknown file mode. Use FileMode.READ, " + |
461 "FileMode.WRITE or FileMode.APPEND."); | 470 "FileMode.WRITE or FileMode.APPEND."); |
462 } | 471 } |
463 var id = _FileUtils.checkedOpen(_name, mode._mode); | 472 var id = _FileUtils.checkedOpen(_name, mode._mode); |
464 if (id == 0) { | 473 assert(id != 0); |
465 throw new FileIOException("Cannot open file: $_name"); | |
466 } | |
467 return new _RandomAccessFile(id, _name); | 474 return new _RandomAccessFile(id, _name); |
468 } | 475 } |
469 | 476 |
470 static RandomAccessFile _openStdioSync(int fd) { | 477 static RandomAccessFile _openStdioSync(int fd) { |
471 var id = _FileUtils.openStdio(fd); | 478 var id = _FileUtils.openStdio(fd); |
472 if (id == 0) { | 479 if (id == 0) { |
473 throw new FileIOException("Cannot open stdio file for: $fd"); | 480 throw new FileIOException("Cannot open stdio file for: $fd"); |
474 } | 481 } |
475 return new _RandomAccessFile(id, ""); | 482 return new _RandomAccessFile(id, ""); |
476 } | 483 } |
(...skipping 577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1054 bool _asyncUsed; | 1061 bool _asyncUsed; |
1055 int _pendingWrites = 0; | 1062 int _pendingWrites = 0; |
1056 | 1063 |
1057 SendPort _fileService; | 1064 SendPort _fileService; |
1058 | 1065 |
1059 Timer _noPendingWriteTimer; | 1066 Timer _noPendingWriteTimer; |
1060 | 1067 |
1061 Function _onNoPendingWrites; | 1068 Function _onNoPendingWrites; |
1062 Function _onError; | 1069 Function _onError; |
1063 } | 1070 } |
OLD | NEW |