OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 // Read the file in blocks of size 64k. | 7 // Read the file in blocks of size 64k. |
8 const int _BLOCK_SIZE = 64 * 1024; | 8 const int _BLOCK_SIZE = 64 * 1024; |
9 | 9 |
10 | 10 |
(...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
549 String toString() => "File: '$path'"; | 549 String toString() => "File: '$path'"; |
550 | 550 |
551 static throwIfError(Object result, String msg, String path) { | 551 static throwIfError(Object result, String msg, String path) { |
552 if (result is OSError) { | 552 if (result is OSError) { |
553 throw new FileSystemException(msg, path, result); | 553 throw new FileSystemException(msg, path, result); |
554 } | 554 } |
555 } | 555 } |
556 } | 556 } |
557 | 557 |
558 | 558 |
559 class _RandomAccessFile implements RandomAccessFile { | 559 class _RandomAccessFile |
| 560 extends Object with _ServiceObject |
| 561 implements RandomAccessFile { |
| 562 // Use default Map so we keep order. |
| 563 static Map<int, _RandomAccessFile> _files = new Map<int, _RandomAccessFile>(); |
| 564 |
560 final String path; | 565 final String path; |
561 int _id; | 566 int _id; |
562 bool _asyncDispatched = false; | 567 bool _asyncDispatched = false; |
563 SendPort _fileService; | 568 SendPort _fileService; |
564 | 569 |
565 _RandomAccessFile(this._id, this.path); | 570 |
| 571 _RandomAccessFile(this._id, this.path) { |
| 572 _files[_serviceId] = this; |
| 573 } |
| 574 |
| 575 String get _serviceTypePath => 'io/file/randomaccessfiles'; |
| 576 String get _serviceTypeName => 'RandomAccessFile'; |
| 577 |
| 578 Map _toJSON(bool ref) { |
| 579 var r = { |
| 580 'id': _servicePath, |
| 581 'type': _serviceType(ref), |
| 582 'name': '$path', |
| 583 'user_name': '$path', |
| 584 }; |
| 585 if (ref) { |
| 586 return r; |
| 587 } |
| 588 r['asyncDispatched'] = _asyncDispatched; |
| 589 r['fd'] = _id; |
| 590 return r; |
| 591 } |
| 592 |
| 593 void _maybePerformCleanup() { |
| 594 if (closed) { |
| 595 _files.remove(_serviceId); |
| 596 } |
| 597 } |
566 | 598 |
567 Future<RandomAccessFile> close() { | 599 Future<RandomAccessFile> close() { |
568 return _dispatch(_FILE_CLOSE, [_id], markClosed: true).then((result) { | 600 return _dispatch(_FILE_CLOSE, [_id], markClosed: true).then((result) { |
569 if (result != -1) { | 601 if (result != -1) { |
570 _id = result; | 602 _id = result; |
| 603 _maybePerformCleanup(); |
571 return this; | 604 return this; |
572 } else { | 605 } else { |
573 throw new FileSystemException("Cannot close file", path); | 606 throw new FileSystemException("Cannot close file", path); |
574 } | 607 } |
575 }); | 608 }); |
576 } | 609 } |
577 | 610 |
578 external static int _close(int id); | 611 external static int _close(int id); |
579 | 612 |
580 void closeSync() { | 613 void closeSync() { |
581 _checkAvailable(); | 614 _checkAvailable(); |
582 var id = _close(_id); | 615 var id = _close(_id); |
583 if (id == -1) { | 616 if (id == -1) { |
584 throw new FileSystemException("Cannot close file", path); | 617 throw new FileSystemException("Cannot close file", path); |
585 } | 618 } |
586 _id = id; | 619 _id = id; |
| 620 _maybePerformCleanup(); |
587 } | 621 } |
588 | 622 |
589 Future<int> readByte() { | 623 Future<int> readByte() { |
590 return _dispatch(_FILE_READ_BYTE, [_id]).then((response) { | 624 return _dispatch(_FILE_READ_BYTE, [_id]).then((response) { |
591 if (_isErrorResponse(response)) { | 625 if (_isErrorResponse(response)) { |
592 throw _exceptionFromResponse(response, "readByte failed", path); | 626 throw _exceptionFromResponse(response, "readByte failed", path); |
593 } | 627 } |
594 return response; | 628 return response; |
595 }); | 629 }); |
596 } | 630 } |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
899 void _checkAvailable() { | 933 void _checkAvailable() { |
900 if (_asyncDispatched) { | 934 if (_asyncDispatched) { |
901 throw new FileSystemException("An async operation is currently pending", | 935 throw new FileSystemException("An async operation is currently pending", |
902 path); | 936 path); |
903 } | 937 } |
904 if (closed) { | 938 if (closed) { |
905 throw new FileSystemException("File closed", path); | 939 throw new FileSystemException("File closed", path); |
906 } | 940 } |
907 } | 941 } |
908 } | 942 } |
OLD | NEW |