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 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 throwIfError(result, "Cannot copy file to '$newPath'", path); | 340 throwIfError(result, "Cannot copy file to '$newPath'", path); |
341 return new File(newPath); | 341 return new File(newPath); |
342 } | 342 } |
343 | 343 |
344 Future<RandomAccessFile> open({FileMode mode: FileMode.READ}) { | 344 Future<RandomAccessFile> open({FileMode mode: FileMode.READ}) { |
345 if (mode != FileMode.READ && | 345 if (mode != FileMode.READ && |
346 mode != FileMode.WRITE && | 346 mode != FileMode.WRITE && |
347 mode != FileMode.APPEND) { | 347 mode != FileMode.APPEND) { |
348 return new Future.error(new ArgumentError()); | 348 return new Future.error(new ArgumentError()); |
349 } | 349 } |
350 return _IOService._dispatch(_FILE_OPEN, [path, mode._mode]).then((response)
{ | 350 return _IOService._dispatch(_FILE_OPEN, [path, mode._mode]) |
351 if (_isErrorResponse(response)) { | 351 .then((response) { |
352 throw _exceptionFromResponse(response, "Cannot open file", path); | 352 if (_isErrorResponse(response)) { |
353 } | 353 throw _exceptionFromResponse(response, "Cannot open file", path); |
354 return new _RandomAccessFile(response, path); | 354 } |
355 }); | 355 return new _RandomAccessFile(response, path); |
| 356 }); |
356 } | 357 } |
357 | 358 |
358 Future<int> length() { | 359 Future<int> length() { |
359 return _IOService._dispatch(_FILE_LENGTH_FROM_PATH, [path]).then((response)
{ | 360 return _IOService._dispatch(_FILE_LENGTH_FROM_PATH, [path]) |
360 if (_isErrorResponse(response)) { | 361 .then((response) { |
361 throw _exceptionFromResponse(response, | 362 if (_isErrorResponse(response)) { |
362 "Cannot retrieve length of file", | 363 throw _exceptionFromResponse(response, |
363 path); | 364 "Cannot retrieve length of file", |
364 } | 365 path); |
365 return response; | 366 } |
366 }); | 367 return response; |
| 368 }); |
367 } | 369 } |
368 | 370 |
369 | 371 |
370 external static _lengthFromPath(String path); | 372 external static _lengthFromPath(String path); |
371 | 373 |
372 int lengthSync() { | 374 int lengthSync() { |
373 var result = _lengthFromPath(path); | 375 var result = _lengthFromPath(path); |
374 throwIfError(result, "Cannot retrieve length of file", path); | 376 throwIfError(result, "Cannot retrieve length of file", path); |
375 return result; | 377 return result; |
376 } | 378 } |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 } | 683 } |
682 var result = _read(_id, bytes); | 684 var result = _read(_id, bytes); |
683 if (result is OSError) { | 685 if (result is OSError) { |
684 throw new FileSystemException("readSync failed", path, result); | 686 throw new FileSystemException("readSync failed", path, result); |
685 } | 687 } |
686 _readCount++; | 688 _readCount++; |
687 _totalRead += result.length; | 689 _totalRead += result.length; |
688 return result; | 690 return result; |
689 } | 691 } |
690 | 692 |
691 Future<int> readInto(List<int> buffer, [int start, int end]) { | 693 Future<int> readInto(List<int> buffer, [int start = 0, int end]) { |
692 if (buffer is !List || | 694 if (buffer is !List || |
693 (start != null && start is !int) || | 695 (start != null && start is !int) || |
694 (end != null && end is !int)) { | 696 (end != null && end is !int)) { |
695 throw new ArgumentError(); | 697 throw new ArgumentError(); |
696 } | 698 } |
697 if (start == null) start = 0; | 699 end = RangeError.checkValidRange(start, end, buffer.length); |
698 if (end == null) end = buffer.length; | 700 if (end == start) return 0; |
699 int length = end - start; | 701 int length = end - start; |
700 return _dispatch(_FILE_READ_INTO, [_id, length]).then((response) { | 702 return _dispatch(_FILE_READ_INTO, [_id, length]).then((response) { |
701 if (_isErrorResponse(response)) { | 703 if (_isErrorResponse(response)) { |
702 throw _exceptionFromResponse(response, "readInto failed", path); | 704 throw _exceptionFromResponse(response, "readInto failed", path); |
703 } | 705 } |
704 var read = response[1]; | 706 var read = response[1]; |
705 var data = response[2]; | 707 var data = response[2]; |
706 buffer.setRange(start, start + read, data); | 708 buffer.setRange(start, start + read, data); |
707 _readCount++; | 709 _readCount++; |
708 _totalRead += read; | 710 _totalRead += read; |
709 return read; | 711 return read; |
710 }); | 712 }); |
711 } | 713 } |
712 | 714 |
713 static void _checkReadWriteListArguments(int length, int start, int end) { | |
714 RangeError.checkValidRange(start, end, length); | |
715 } | |
716 | |
717 external static _readInto(int id, List<int> buffer, int start, int end); | 715 external static _readInto(int id, List<int> buffer, int start, int end); |
718 | 716 |
719 int readIntoSync(List<int> buffer, [int start, int end]) { | 717 int readIntoSync(List<int> buffer, [int start = 0, int end]) { |
720 _checkAvailable(); | 718 _checkAvailable(); |
721 if (buffer is !List || | 719 if (buffer is !List || |
722 (start != null && start is !int) || | 720 (start != null && start is !int) || |
723 (end != null && end is !int)) { | 721 (end != null && end is !int)) { |
724 throw new ArgumentError(); | 722 throw new ArgumentError(); |
725 } | 723 } |
726 if (start == null) start = 0; | 724 end = RangeError.checkValidRange(start, end, buffer.length); |
727 if (end == null) end = buffer.length; | |
728 if (end == start) return 0; | 725 if (end == start) return 0; |
729 _checkReadWriteListArguments(buffer.length, start, end); | |
730 var result = _readInto(_id, buffer, start, end); | 726 var result = _readInto(_id, buffer, start, end); |
731 if (result is OSError) { | 727 if (result is OSError) { |
732 throw new FileSystemException("readInto failed", path, result); | 728 throw new FileSystemException("readInto failed", path, result); |
733 } | 729 } |
734 _readCount++; | 730 _readCount++; |
735 _totalRead += result; | 731 _totalRead += result; |
736 return result; | 732 return result; |
737 } | 733 } |
738 | 734 |
739 Future<RandomAccessFile> writeByte(int value) { | 735 Future<RandomAccessFile> writeByte(int value) { |
(...skipping 19 matching lines...) Expand all Loading... |
759 } | 755 } |
760 var result = _writeByte(_id, value); | 756 var result = _writeByte(_id, value); |
761 if (result is OSError) { | 757 if (result is OSError) { |
762 throw new FileSystemException("writeByte failed", path, result); | 758 throw new FileSystemException("writeByte failed", path, result); |
763 } | 759 } |
764 _writeCount++; | 760 _writeCount++; |
765 _totalWritten++; | 761 _totalWritten++; |
766 return result; | 762 return result; |
767 } | 763 } |
768 | 764 |
769 Future<RandomAccessFile> writeFrom(List<int> buffer, [int start, int end]) { | 765 Future<RandomAccessFile> writeFrom( |
770 if ((buffer is !List && buffer is !ByteData) || | 766 List<int> buffer, [int start = 0, int end]) { |
| 767 if ((buffer is !List) || |
771 (start != null && start is !int) || | 768 (start != null && start is !int) || |
772 (end != null && end is !int)) { | 769 (end != null && end is !int)) { |
773 throw new ArgumentError("Invalid arguments to writeFrom"); | 770 throw new ArgumentError("Invalid arguments to writeFrom"); |
774 } | 771 } |
775 | 772 end = RangeError.checkValidRange(start, end, buffer.length); |
| 773 if (end == start) return; |
776 _BufferAndStart result; | 774 _BufferAndStart result; |
777 try { | 775 try { |
778 result = _ensureFastAndSerializableByteData(buffer, start, end); | 776 result = _ensureFastAndSerializableByteData(buffer, start, end); |
779 } catch (e) { | 777 } catch (e) { |
780 return new Future.error(e); | 778 return new Future.error(e); |
781 } | 779 } |
782 | 780 |
783 List request = new List(4); | 781 List request = new List(4); |
784 request[0] = _id; | 782 request[0] = _id; |
785 request[1] = result.buffer; | 783 request[1] = result.buffer; |
786 request[2] = result.start; | 784 request[2] = result.start; |
787 request[3] = end - (start - result.start); | 785 request[3] = end - (start - result.start); |
788 return _dispatch(_FILE_WRITE_FROM, request).then((response) { | 786 return _dispatch(_FILE_WRITE_FROM, request).then((response) { |
789 if (_isErrorResponse(response)) { | 787 if (_isErrorResponse(response)) { |
790 throw _exceptionFromResponse(response, "writeFrom failed", path); | 788 throw _exceptionFromResponse(response, "writeFrom failed", path); |
791 } | 789 } |
792 _writeCount++; | 790 _writeCount++; |
793 _totalWritten += end - (start - result.start); | 791 _totalWritten += end - (start - result.start); |
794 return this; | 792 return this; |
795 }); | 793 }); |
796 } | 794 } |
797 | 795 |
798 external static _writeFrom(int id, List<int> buffer, int start, int end); | 796 external static _writeFrom(int id, List<int> buffer, int start, int end); |
799 | 797 |
800 void writeFromSync(List<int> buffer, [int start, int end]) { | 798 void writeFromSync(List<int> buffer, [int start = 0, int end]) { |
801 _checkAvailable(); | 799 _checkAvailable(); |
802 if (buffer is !List || | 800 if (buffer is !List || |
803 (start != null && start is !int) || | 801 (start != null && start is !int) || |
804 (end != null && end is !int)) { | 802 (end != null && end is !int)) { |
805 throw new ArgumentError("Invalid arguments to writeFromSync"); | 803 throw new ArgumentError("Invalid arguments to writeFromSync"); |
806 } | 804 } |
807 if (start == null) start = 0; | 805 end = RangeError.checkValidRange(start, end, buffer.length); |
808 if (end == null) end = buffer.length; | |
809 if (end == start) return; | 806 if (end == start) return; |
810 _checkReadWriteListArguments(buffer.length, start, end); | |
811 _BufferAndStart bufferAndStart = | 807 _BufferAndStart bufferAndStart = |
812 _ensureFastAndSerializableByteData(buffer, start, end); | 808 _ensureFastAndSerializableByteData(buffer, start, end); |
813 var result = _writeFrom(_id, | 809 var result = _writeFrom(_id, |
814 bufferAndStart.buffer, | 810 bufferAndStart.buffer, |
815 bufferAndStart.start, | 811 bufferAndStart.start, |
816 end - (start - bufferAndStart.start)); | 812 end - (start - bufferAndStart.start)); |
817 if (result is OSError) { | 813 if (result is OSError) { |
818 throw new FileSystemException("writeFrom failed", path, result); | 814 throw new FileSystemException("writeFrom failed", path, result); |
819 } | 815 } |
820 _writeCount++; | 816 _writeCount++; |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1042 void _checkAvailable() { | 1038 void _checkAvailable() { |
1043 if (_asyncDispatched) { | 1039 if (_asyncDispatched) { |
1044 throw new FileSystemException("An async operation is currently pending", | 1040 throw new FileSystemException("An async operation is currently pending", |
1045 path); | 1041 path); |
1046 } | 1042 } |
1047 if (closed) { | 1043 if (closed) { |
1048 throw new FileSystemException("File closed", path); | 1044 throw new FileSystemException("File closed", path); |
1049 } | 1045 } |
1050 } | 1046 } |
1051 } | 1047 } |
OLD | NEW |