| 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 extends _BaseDataInputStream implements InputStream { | 5 class _FileInputStream extends _BaseDataInputStream implements InputStream { |
| 6 _FileInputStream(File file) { | 6 _FileInputStream(File file) { |
| 7 _file = file.openSync(); | 7 _file = file.openSync(); |
| 8 _length = _file.lengthSync(); | 8 _length = _file.lengthSync(); |
| 9 _streamMarkedClosed = true; | 9 _streamMarkedClosed = true; |
| 10 _checkScheduleCallbacks(); | 10 _checkScheduleCallbacks(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 } | 43 } |
| 44 | 44 |
| 45 RandomAccessFile _file; | 45 RandomAccessFile _file; |
| 46 int _length; | 46 int _length; |
| 47 bool _closed = false; | 47 bool _closed = false; |
| 48 } | 48 } |
| 49 | 49 |
| 50 | 50 |
| 51 class _FileOutputStream implements OutputStream { | 51 class _FileOutputStream implements OutputStream { |
| 52 _FileOutputStream(File file) { | 52 _FileOutputStream(File file) { |
| 53 _file = file.openSync(true); | 53 _file = file.openSync(FileMode.WRITE); |
| 54 } | 54 } |
| 55 | 55 |
| 56 bool write(List<int> buffer, [bool copyBuffer = false]) { | 56 bool write(List<int> buffer, [bool copyBuffer = false]) { |
| 57 return _write(buffer, 0, buffer.length); | 57 return _write(buffer, 0, buffer.length); |
| 58 } | 58 } |
| 59 | 59 |
| 60 bool writeFrom(List<int> buffer, [int offset = 0, int len]) { | 60 bool writeFrom(List<int> buffer, [int offset = 0, int len]) { |
| 61 return _write( | 61 return _write( |
| 62 buffer, offset, (len == null) ? buffer.length - offset : len); | 62 buffer, offset, (len == null) ? buffer.length - offset : len); |
| 63 } | 63 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 | 109 |
| 110 void execute(ReceivePort port) { | 110 void execute(ReceivePort port) { |
| 111 _replyPort.send(_FileUtils.exists(_name), port.toSendPort()); | 111 _replyPort.send(_FileUtils.exists(_name), port.toSendPort()); |
| 112 } | 112 } |
| 113 | 113 |
| 114 String _name; | 114 String _name; |
| 115 } | 115 } |
| 116 | 116 |
| 117 | 117 |
| 118 class _OpenOperation extends _FileOperation { | 118 class _OpenOperation extends _FileOperation { |
| 119 _OpenOperation(String this._name, bool this._writable); | 119 _OpenOperation(String this._name, int this._mode); |
| 120 | 120 |
| 121 void execute(ReceivePort port) { | 121 void execute(ReceivePort port) { |
| 122 _replyPort.send(_FileUtils.checkedOpen(_name, _writable), | 122 _replyPort.send(_FileUtils.checkedOpen(_name, _mode), |
| 123 port.toSendPort()); | 123 port.toSendPort()); |
| 124 } | 124 } |
| 125 | 125 |
| 126 String _name; | 126 String _name; |
| 127 bool _writable; | 127 int _mode; |
| 128 } | 128 } |
| 129 | 129 |
| 130 | 130 |
| 131 class _CloseOperation extends _FileOperation { | 131 class _CloseOperation extends _FileOperation { |
| 132 _CloseOperation(int this._id); | 132 _CloseOperation(int this._id); |
| 133 | 133 |
| 134 void execute(ReceivePort port) { | 134 void execute(ReceivePort port) { |
| 135 _replyPort.send(_FileUtils.close(_id), port.toSendPort()); | 135 _replyPort.send(_FileUtils.close(_id), port.toSendPort()); |
| 136 } | 136 } |
| 137 | 137 |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 } | 404 } |
| 405 | 405 |
| 406 Queue<_FileOperation> _queue; | 406 Queue<_FileOperation> _queue; |
| 407 _FileOperationIsolate _isolate; | 407 _FileOperationIsolate _isolate; |
| 408 } | 408 } |
| 409 | 409 |
| 410 | 410 |
| 411 // Helper class containing static file helper methods. | 411 // Helper class containing static file helper methods. |
| 412 class _FileUtils { | 412 class _FileUtils { |
| 413 static bool exists(String name) native "File_Exists"; | 413 static bool exists(String name) native "File_Exists"; |
| 414 static int open(String name, bool writable) native "File_Open"; | 414 static int open(String name, int mode) native "File_Open"; |
| 415 static bool create(String name) native "File_Create"; | 415 static bool create(String name) native "File_Create"; |
| 416 static bool delete(String name) native "File_Delete"; | 416 static bool delete(String name) native "File_Delete"; |
| 417 static String fullPath(String name) native "File_FullPath"; | 417 static String fullPath(String name) native "File_FullPath"; |
| 418 static int close(int id) native "File_Close"; | 418 static int close(int id) native "File_Close"; |
| 419 static int readByte(int id) native "File_ReadByte"; | 419 static int readByte(int id) native "File_ReadByte"; |
| 420 static int readList(int id, List<int> buffer, int offset, int bytes) | 420 static int readList(int id, List<int> buffer, int offset, int bytes) |
| 421 native "File_ReadList"; | 421 native "File_ReadList"; |
| 422 static int writeByte(int id, int value) native "File_WriteByte"; | 422 static int writeByte(int id, int value) native "File_WriteByte"; |
| 423 static int writeList(int id, List<int> buffer, int offset, int bytes) | 423 static int writeList(int id, List<int> buffer, int offset, int bytes) |
| 424 native "File_WriteList"; | 424 native "File_WriteList"; |
| 425 static int writeString(int id, String string) native "File_WriteString"; | 425 static int writeString(int id, String string) native "File_WriteString"; |
| 426 static int position(int id) native "File_Position"; | 426 static int position(int id) native "File_Position"; |
| 427 static bool setPosition(int id, int position) native "File_SetPosition"; | 427 static bool setPosition(int id, int position) native "File_SetPosition"; |
| 428 static bool truncate(int id, int length) native "File_Truncate"; | 428 static bool truncate(int id, int length) native "File_Truncate"; |
| 429 static int length(int id) native "File_Length"; | 429 static int length(int id) native "File_Length"; |
| 430 static int flush(int id) native "File_Flush"; | 430 static int flush(int id) native "File_Flush"; |
| 431 | 431 |
| 432 static int checkedOpen(String name, bool writable) { | 432 static int checkedOpen(String name, int mode) { |
| 433 if (name is !String || writable is !bool) return 0; | 433 if (name is !String || mode is !int) return 0; |
| 434 return open(name, writable); | 434 return open(name, mode); |
| 435 } | 435 } |
| 436 | 436 |
| 437 static bool checkedCreate(String name) { | 437 static bool checkedCreate(String name) { |
| 438 if (name is !String) return false; | 438 if (name is !String) return false; |
| 439 return create(name); | 439 return create(name); |
| 440 } | 440 } |
| 441 | 441 |
| 442 static bool checkedDelete(String name) { | 442 static bool checkedDelete(String name) { |
| 443 if (name is !String) return false; | 443 if (name is !String) return false; |
| 444 return delete(name); | 444 return delete(name); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 if (_asyncUsed) { | 538 if (_asyncUsed) { |
| 539 throw new FileIOException( | 539 throw new FileIOException( |
| 540 "Mixed use of synchronous and asynchronous API"); | 540 "Mixed use of synchronous and asynchronous API"); |
| 541 } | 541 } |
| 542 bool deleted = _FileUtils.checkedDelete(_name); | 542 bool deleted = _FileUtils.checkedDelete(_name); |
| 543 if (!deleted) { | 543 if (!deleted) { |
| 544 throw new FileIOException("Cannot delete file: $_name"); | 544 throw new FileIOException("Cannot delete file: $_name"); |
| 545 } | 545 } |
| 546 } | 546 } |
| 547 | 547 |
| 548 void open([bool writable = false]) { | 548 void open([FileMode mode = FileMode.READ]) { |
| 549 _asyncUsed = true; | 549 _asyncUsed = true; |
| 550 if (mode != FileMode.READ && |
| 551 mode != FileMode.WRITE && |
| 552 mode != FileMode.APPEND) { |
| 553 if (_errorHandler != null) { |
| 554 _errorHandler("Unknown file mode. Use FileMode.READ, FileMode.WRITE " + |
| 555 "or FileMode.APPEND."); |
| 556 return; |
| 557 } |
| 558 } |
| 550 // If no open handler is present, close the file immediately to | 559 // If no open handler is present, close the file immediately to |
| 551 // avoid leaking an open file descriptor. | 560 // avoid leaking an open file descriptor. |
| 552 var handler = _openHandler; | 561 var handler = _openHandler; |
| 553 if (handler === null) { | 562 if (handler === null) { |
| 554 handler = (file) => file.close(); | 563 handler = (file) => file.close(); |
| 555 } | 564 } |
| 556 var handleOpenResult = (id, ignored) { | 565 var handleOpenResult = (id, ignored) { |
| 557 if (id != 0) { | 566 if (id != 0) { |
| 558 var randomAccessFile = new _RandomAccessFile(id, _name); | 567 var randomAccessFile = new _RandomAccessFile(id, _name); |
| 559 handler(randomAccessFile); | 568 handler(randomAccessFile); |
| 560 } else if (_errorHandler != null) { | 569 } else if (_errorHandler != null) { |
| 561 _errorHandler("Cannot open file: $_name"); | 570 _errorHandler("Cannot open file: $_name"); |
| 562 } | 571 } |
| 563 }; | 572 }; |
| 564 var operation = new _OpenOperation(_name, writable); | 573 var operation = new _OpenOperation(_name, mode.mode); |
| 565 _scheduler.enqueue(operation, handleOpenResult); | 574 _scheduler.enqueue(operation, handleOpenResult); |
| 566 } | 575 } |
| 567 | 576 |
| 568 void openSync([bool writable = false]) { | 577 void openSync([FileMode mode = FileMode.READ]) { |
| 569 if (_asyncUsed) { | 578 if (_asyncUsed) { |
| 570 throw new FileIOException( | 579 throw new FileIOException( |
| 571 "Mixed use of synchronous and asynchronous API"); | 580 "Mixed use of synchronous and asynchronous API"); |
| 572 } | 581 } |
| 573 var id = _FileUtils.checkedOpen(_name, writable); | 582 if (mode != FileMode.READ && |
| 583 mode != FileMode.WRITE && |
| 584 mode != FileMode.APPEND) { |
| 585 throw new FileIOException("Unknown file mode. Use FileMode.READ, " + |
| 586 "FileMode.WRITE or FileMode.APPEND."); |
| 587 } |
| 588 var id = _FileUtils.checkedOpen(_name, mode.mode); |
| 574 if (id == 0) { | 589 if (id == 0) { |
| 575 throw new FileIOException("Cannot open file: $_name"); | 590 throw new FileIOException("Cannot open file: $_name"); |
| 576 } | 591 } |
| 577 return new _RandomAccessFile(id, _name); | 592 return new _RandomAccessFile(id, _name); |
| 578 } | 593 } |
| 579 | 594 |
| 580 void fullPath() { | 595 void fullPath() { |
| 581 _asyncUsed = true; | 596 _asyncUsed = true; |
| 582 var handler = _fullPathHandler; | 597 var handler = _fullPathHandler; |
| 583 if (handler == null) handler = (path) => null; | 598 if (handler == null) handler = (path) => null; |
| (...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1048 var _readByteHandler; | 1063 var _readByteHandler; |
| 1049 var _readListHandler; | 1064 var _readListHandler; |
| 1050 var _noPendingWriteHandler; | 1065 var _noPendingWriteHandler; |
| 1051 var _positionHandler; | 1066 var _positionHandler; |
| 1052 var _setPositionHandler; | 1067 var _setPositionHandler; |
| 1053 var _truncateHandler; | 1068 var _truncateHandler; |
| 1054 var _lengthHandler; | 1069 var _lengthHandler; |
| 1055 var _flushHandler; | 1070 var _flushHandler; |
| 1056 var _errorHandler; | 1071 var _errorHandler; |
| 1057 } | 1072 } |
| OLD | NEW |