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 | |
560 final String path; | 563 final String path; |
561 int _id; | 564 int _id; |
562 bool _asyncDispatched = false; | 565 bool _asyncDispatched = false; |
563 SendPort _fileService; | 566 SendPort _fileService; |
564 | 567 |
565 _RandomAccessFile(this._id, this.path); | 568 final String _serviceTypePath = 'io/file/randomaccessfiles'; |
Anders Johnsen
2014/05/25 17:59:23
Make getters so they don't take up an extra word.
Cutch
2014/05/26 15:32:17
Done.
| |
569 final String _serviceTypeName = 'RandomAccessFile'; | |
570 | |
571 // Use default Map so we keep order. | |
572 static Map<int, _RandomAccessFile> _files = new Map<int, _RandomAccessFile>(); | |
Anders Johnsen
2014/05/25 17:59:23
Please move statics to the top of the class.
Cutch
2014/05/26 15:32:17
Done.
| |
573 | |
574 Map _toJSON(bool ref) { | |
Anders Johnsen
2014/05/25 17:59:23
Move methods below constructor.
Cutch
2014/05/26 15:32:17
Done.
| |
575 var r = { | |
576 'id': _servicePath, | |
577 'type': _serviceType(ref), | |
578 'name': '$path', | |
579 'user_name': '$path', | |
580 }; | |
581 if (ref) { | |
582 return r; | |
583 } | |
584 r['asyncDispatched'] = _asyncDispatched; | |
585 return r; | |
586 } | |
587 | |
588 _RandomAccessFile(this._id, this.path) { | |
589 _files[_serviceId] = this; | |
590 } | |
591 | |
592 void _maybePerformCleanup() { | |
593 if (closed) { | |
594 _files.remove(_serviceId); | |
595 } | |
596 } | |
566 | 597 |
567 Future<RandomAccessFile> close() { | 598 Future<RandomAccessFile> close() { |
568 return _dispatch(_FILE_CLOSE, [_id], markClosed: true).then((result) { | 599 return _dispatch(_FILE_CLOSE, [_id], markClosed: true).then((result) { |
569 if (result != -1) { | 600 if (result != -1) { |
570 _id = result; | 601 _id = result; |
602 _maybePerformCleanup(); | |
571 return this; | 603 return this; |
572 } else { | 604 } else { |
573 throw new FileSystemException("Cannot close file", path); | 605 throw new FileSystemException("Cannot close file", path); |
574 } | 606 } |
575 }); | 607 }); |
576 } | 608 } |
577 | 609 |
578 external static int _close(int id); | 610 external static int _close(int id); |
579 | 611 |
580 void closeSync() { | 612 void closeSync() { |
581 _checkAvailable(); | 613 _checkAvailable(); |
582 var id = _close(_id); | 614 var id = _close(_id); |
583 if (id == -1) { | 615 if (id == -1) { |
584 throw new FileSystemException("Cannot close file", path); | 616 throw new FileSystemException("Cannot close file", path); |
585 } | 617 } |
586 _id = id; | 618 _id = id; |
619 _maybePerformCleanup(); | |
587 } | 620 } |
588 | 621 |
589 Future<int> readByte() { | 622 Future<int> readByte() { |
590 return _dispatch(_FILE_READ_BYTE, [_id]).then((response) { | 623 return _dispatch(_FILE_READ_BYTE, [_id]).then((response) { |
591 if (_isErrorResponse(response)) { | 624 if (_isErrorResponse(response)) { |
592 throw _exceptionFromResponse(response, "readByte failed", path); | 625 throw _exceptionFromResponse(response, "readByte failed", path); |
593 } | 626 } |
594 return response; | 627 return response; |
595 }); | 628 }); |
596 } | 629 } |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
899 void _checkAvailable() { | 932 void _checkAvailable() { |
900 if (_asyncDispatched) { | 933 if (_asyncDispatched) { |
901 throw new FileSystemException("An async operation is currently pending", | 934 throw new FileSystemException("An async operation is currently pending", |
902 path); | 935 path); |
903 } | 936 } |
904 if (closed) { | 937 if (closed) { |
905 throw new FileSystemException("File closed", path); | 938 throw new FileSystemException("File closed", path); |
906 } | 939 } |
907 } | 940 } |
908 } | 941 } |
OLD | NEW |