| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 implements FileInputStream { | 5 class _FileInputStream implements FileInputStream { |
| 6 _FileInputStream(File file) { | 6 _FileInputStream(File file) { |
| 7 _file = new File(file.name); | 7 _file = new File(file.name); |
| 8 _file.openSync(); | 8 _file.openSync(); |
| 9 } | 9 } |
| 10 | 10 |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 _CreateOperation(String this._name); | 329 _CreateOperation(String this._name); |
| 330 | 330 |
| 331 void execute(ReceivePort port) { | 331 void execute(ReceivePort port) { |
| 332 _replyPort.send(_File._checkedCreate(_name), port.toSendPort()); | 332 _replyPort.send(_File._checkedCreate(_name), port.toSendPort()); |
| 333 } | 333 } |
| 334 | 334 |
| 335 String _name; | 335 String _name; |
| 336 } | 336 } |
| 337 | 337 |
| 338 | 338 |
| 339 class _DeleteOperation extends _FileOperation { |
| 340 _DeleteOperation(String this._name); |
| 341 |
| 342 void execute(ReceivePort port) { |
| 343 _replyPort.send(_File._checkedDelete(_name), port.toSendPort()); |
| 344 } |
| 345 |
| 346 String _name; |
| 347 } |
| 348 |
| 349 |
| 339 class _ExitOperation extends _FileOperation { | 350 class _ExitOperation extends _FileOperation { |
| 340 void execute(ReceivePort port) { | 351 void execute(ReceivePort port) { |
| 341 port.close(); | 352 port.close(); |
| 342 } | 353 } |
| 343 } | 354 } |
| 344 | 355 |
| 345 | 356 |
| 346 class _FileOperationIsolate extends Isolate { | 357 class _FileOperationIsolate extends Isolate { |
| 347 _FileOperationIsolate() : super.heavy(); | 358 _FileOperationIsolate() : super.heavy(); |
| 348 | 359 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 421 native "File_ReadList"; | 432 native "File_ReadList"; |
| 422 static int _writeByte(int id, int value) native "File_WriteByte"; | 433 static int _writeByte(int id, int value) native "File_WriteByte"; |
| 423 static int _writeList(int id, List<int> buffer, int offset, int bytes) | 434 static int _writeList(int id, List<int> buffer, int offset, int bytes) |
| 424 native "File_WriteList"; | 435 native "File_WriteList"; |
| 425 static int _writeString(int id, String string) native "File_WriteString"; | 436 static int _writeString(int id, String string) native "File_WriteString"; |
| 426 static int _position(int id) native "File_Position"; | 437 static int _position(int id) native "File_Position"; |
| 427 static int _setPosition(int id, int position) native "File_SetPosition"; | 438 static int _setPosition(int id, int position) native "File_SetPosition"; |
| 428 static int _length(int id) native "File_Length"; | 439 static int _length(int id) native "File_Length"; |
| 429 static int _flush(int id) native "File_Flush"; | 440 static int _flush(int id) native "File_Flush"; |
| 430 static bool _create(String name) native "File_Create"; | 441 static bool _create(String name) native "File_Create"; |
| 442 static bool _delete(String name) native "File_Delete"; |
| 431 static String _fullPath(String name) native "File_FullPath"; | 443 static String _fullPath(String name) native "File_FullPath"; |
| 432 | 444 |
| 433 static int _checkReadWriteListArguments(int length, int offset, int bytes) { | 445 static int _checkReadWriteListArguments(int length, int offset, int bytes) { |
| 434 if (offset < 0) return offset; | 446 if (offset < 0) return offset; |
| 435 if (bytes < 0) return bytes; | 447 if (bytes < 0) return bytes; |
| 436 if ((offset + bytes) > length) return offset + bytes; | 448 if ((offset + bytes) > length) return offset + bytes; |
| 437 return 0; | 449 return 0; |
| 438 } | 450 } |
| 439 | 451 |
| 440 static int _checkedOpen(String name, bool writable) { | 452 static int _checkedOpen(String name, bool writable) { |
| 441 if (name is !String || writable is !bool) return 0; | 453 if (name is !String || writable is !bool) return 0; |
| 442 return _open(name, writable); | 454 return _open(name, writable); |
| 443 } | 455 } |
| 444 | 456 |
| 445 static bool _checkedCreate(String name) { | 457 static bool _checkedCreate(String name) { |
| 446 if (name is !String) return false; | 458 if (name is !String) return false; |
| 447 return _create(name); | 459 return _create(name); |
| 448 } | 460 } |
| 449 | 461 |
| 462 static bool _checkedDelete(String name) { |
| 463 if (name is !String) return false; |
| 464 return _delete(name); |
| 465 } |
| 466 |
| 450 static int _checkedWriteString(int id, String string) { | 467 static int _checkedWriteString(int id, String string) { |
| 451 if (string is !String) return -1; | 468 if (string is !String) return -1; |
| 452 return _writeString(id, string); | 469 return _writeString(id, string); |
| 453 } | 470 } |
| 454 | 471 |
| 455 static String _checkedFullPath(String name) { | 472 static String _checkedFullPath(String name) { |
| 456 if (name is !String) return null; | 473 if (name is !String) return null; |
| 457 return _fullPath(name); | 474 return _fullPath(name); |
| 458 } | 475 } |
| 459 | 476 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 if (_asyncUsed) { | 517 if (_asyncUsed) { |
| 501 throw new FileIOException( | 518 throw new FileIOException( |
| 502 "Mixed use of synchronous and asynchronous API"); | 519 "Mixed use of synchronous and asynchronous API"); |
| 503 } | 520 } |
| 504 bool created = _checkedCreate(_name); | 521 bool created = _checkedCreate(_name); |
| 505 if (!created) { | 522 if (!created) { |
| 506 throw new FileIOException("Cannot create file: $_name"); | 523 throw new FileIOException("Cannot create file: $_name"); |
| 507 } | 524 } |
| 508 } | 525 } |
| 509 | 526 |
| 527 void delete() { |
| 528 _asyncUsed = true; |
| 529 var handler = (_deleteHandler != null) ? _deleteHandler : () => null; |
| 530 var handleDeleteResult = (created, ignored) { |
| 531 if (created) { |
| 532 handler(); |
| 533 } else if (_errorHandler != null) { |
| 534 _errorHandler("Cannot delete file: $_name"); |
| 535 } |
| 536 }; |
| 537 var operation = new _DeleteOperation(_name); |
| 538 _scheduler.enqueue(operation, handleDeleteResult); |
| 539 } |
| 540 |
| 541 void deleteSync() { |
| 542 if (_asyncUsed) { |
| 543 throw new FileIOException( |
| 544 "Mixed use of synchronous and asynchronous API"); |
| 545 } |
| 546 bool deleted = _checkedDelete(_name); |
| 547 if (!deleted) { |
| 548 throw new FileIOException("Cannot delete file: $_name"); |
| 549 } |
| 550 } |
| 551 |
| 510 void open([bool writable = false]) { | 552 void open([bool writable = false]) { |
| 511 _asyncUsed = true; | 553 _asyncUsed = true; |
| 512 var handler = (_openHandler != null) ? _openHandler : () => null; | 554 var handler = (_openHandler != null) ? _openHandler : () => null; |
| 513 var handleOpenResult = (result, ignored) { | 555 var handleOpenResult = (result, ignored) { |
| 514 if (result != 0) { | 556 if (result != 0) { |
| 515 _id = result; | 557 _id = result; |
| 516 handler(); | 558 handler(); |
| 517 } else if (_errorHandler != null) { | 559 } else if (_errorHandler != null) { |
| 518 _errorHandler("Cannot open file: $_name"); | 560 _errorHandler("Cannot open file: $_name"); |
| 519 } | 561 } |
| (...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 890 } | 932 } |
| 891 | 933 |
| 892 void set existsHandler(void handler(bool exists)) { | 934 void set existsHandler(void handler(bool exists)) { |
| 893 _existsHandler = handler; | 935 _existsHandler = handler; |
| 894 } | 936 } |
| 895 | 937 |
| 896 void set createHandler(void handler()) { | 938 void set createHandler(void handler()) { |
| 897 _createHandler = handler; | 939 _createHandler = handler; |
| 898 } | 940 } |
| 899 | 941 |
| 942 void set deleteHandler(void handler()) { |
| 943 _deleteHandler = handler; |
| 944 } |
| 945 |
| 900 void set openHandler(void handler()) { | 946 void set openHandler(void handler()) { |
| 901 _openHandler = handler; | 947 _openHandler = handler; |
| 902 } | 948 } |
| 903 | 949 |
| 904 void set closeHandler(void handler()) { | 950 void set closeHandler(void handler()) { |
| 905 _closeHandler = handler; | 951 _closeHandler = handler; |
| 906 } | 952 } |
| 907 | 953 |
| 908 void set readByteHandler(void handler(int byte)) { | 954 void set readByteHandler(void handler(int byte)) { |
| 909 _readByteHandler = handler; | 955 _readByteHandler = handler; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 942 } | 988 } |
| 943 | 989 |
| 944 String _name; | 990 String _name; |
| 945 int _id; | 991 int _id; |
| 946 bool _asyncUsed; | 992 bool _asyncUsed; |
| 947 | 993 |
| 948 _FileOperationScheduler _scheduler; | 994 _FileOperationScheduler _scheduler; |
| 949 | 995 |
| 950 var _existsHandler; | 996 var _existsHandler; |
| 951 var _createHandler; | 997 var _createHandler; |
| 998 var _deleteHandler; |
| 952 var _openHandler; | 999 var _openHandler; |
| 953 var _closeHandler; | 1000 var _closeHandler; |
| 954 var _readByteHandler; | 1001 var _readByteHandler; |
| 955 var _readListHandler; | 1002 var _readListHandler; |
| 956 var _noPendingWriteHandler; | 1003 var _noPendingWriteHandler; |
| 957 var _positionHandler; | 1004 var _positionHandler; |
| 958 var _setPositionHandler; | 1005 var _setPositionHandler; |
| 959 var _lengthHandler; | 1006 var _lengthHandler; |
| 960 var _flushHandler; | 1007 var _flushHandler; |
| 961 var _fullPathHandler; | 1008 var _fullPathHandler; |
| 962 var _errorHandler; | 1009 var _errorHandler; |
| 963 } | 1010 } |
| OLD | NEW |