| 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 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 completer.complete(_file); | 206 completer.complete(_file); |
| 207 }, | 207 }, |
| 208 onError: error, | 208 onError: error, |
| 209 cancelOnError: true); | 209 cancelOnError: true); |
| 210 }) | 210 }) |
| 211 .catchError(completer.completeError); | 211 .catchError(completer.completeError); |
| 212 return completer.future; | 212 return completer.future; |
| 213 } | 213 } |
| 214 | 214 |
| 215 Future<File> close() => | 215 Future<File> close() => |
| 216 _openFuture.then/*<File>*/((openedFile) => openedFile.close()); | 216 _openFuture.then((openedFile) => openedFile.close()); |
| 217 } | 217 } |
| 218 | 218 |
| 219 | 219 |
| 220 // Class for encapsulating the native implementation of files. | 220 // Class for encapsulating the native implementation of files. |
| 221 class _File extends FileSystemEntity implements File { | 221 class _File extends FileSystemEntity implements File { |
| 222 final String path; | 222 final String path; |
| 223 | 223 |
| 224 // Constructor for file. | 224 // Constructor for file. |
| 225 _File(this.path) { | 225 _File(this.path) { |
| 226 if (path is! String) { | 226 if (path is! String) { |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 mode != FileMode.APPEND && | 434 mode != FileMode.APPEND && |
| 435 mode != FileMode.WRITE_ONLY && | 435 mode != FileMode.WRITE_ONLY && |
| 436 mode != FileMode.WRITE_ONLY_APPEND) { | 436 mode != FileMode.WRITE_ONLY_APPEND) { |
| 437 throw new ArgumentError('Invalid file mode for this operation'); | 437 throw new ArgumentError('Invalid file mode for this operation'); |
| 438 } | 438 } |
| 439 var consumer = new _FileStreamConsumer(this, mode); | 439 var consumer = new _FileStreamConsumer(this, mode); |
| 440 return new IOSink(consumer, encoding: encoding); | 440 return new IOSink(consumer, encoding: encoding); |
| 441 } | 441 } |
| 442 | 442 |
| 443 Future<List<int>> readAsBytes() { | 443 Future<List<int>> readAsBytes() { |
| 444 Future<List<int>> readDataChunked(RandomAccessFile file) { | 444 Future<List<int>> readDataChunked(file) { |
| 445 var builder = new BytesBuilder(copy: false); | 445 var builder = new BytesBuilder(copy: false); |
| 446 var completer = new Completer<List<int>>(); | 446 var completer = new Completer(); |
| 447 void read() { | 447 void read() { |
| 448 file.read(_BLOCK_SIZE).then((data) { | 448 file.read(_BLOCK_SIZE).then((data) { |
| 449 if (data.length > 0) { | 449 if (data.length > 0) { |
| 450 builder.add(data); | 450 builder.add(data); |
| 451 read(); | 451 read(); |
| 452 } else { | 452 } else { |
| 453 completer.complete(builder.takeBytes()); | 453 completer.complete(builder.takeBytes()); |
| 454 } | 454 } |
| 455 }, onError: completer.completeError); | 455 }, onError: completer.completeError); |
| 456 } | 456 } |
| 457 read(); | 457 read(); |
| 458 return completer.future; | 458 return completer.future; |
| 459 } | 459 } |
| 460 | 460 |
| 461 return open().then((file) { | 461 return open().then((file) { |
| 462 return file.length().then((length) { | 462 return file.length().then((length) { |
| 463 if (length == 0) { | 463 if (length == 0) { |
| 464 // May be character device, try to read it in chunks. | 464 // May be character device, try to read it in chunks. |
| 465 return readDataChunked(file); | 465 return readDataChunked(file); |
| 466 } | 466 } |
| 467 return file.read(length); | 467 return file.read(length); |
| 468 }).whenComplete(file.close); | 468 }).whenComplete(file.close); |
| 469 }); | 469 }); |
| 470 } | 470 } |
| 471 | 471 |
| 472 List<int> readAsBytesSync() { | 472 List<int> readAsBytesSync() { |
| 473 var opened = openSync(); | 473 var opened = openSync(); |
| 474 try { | 474 try { |
| 475 List<int> data; | 475 var data; |
| 476 var length = opened.lengthSync(); | 476 var length = opened.lengthSync(); |
| 477 if (length == 0) { | 477 if (length == 0) { |
| 478 // May be character device, try to read it in chunks. | 478 // May be character device, try to read it in chunks. |
| 479 var builder = new BytesBuilder(copy: false); | 479 var builder = new BytesBuilder(copy: false); |
| 480 do { | 480 do { |
| 481 data = opened.readSync(_BLOCK_SIZE); | 481 data = opened.readSync(_BLOCK_SIZE); |
| 482 if (data.length > 0) builder.add(data); | 482 if (data.length > 0) builder.add(data); |
| 483 } while (data.length > 0); | 483 } while (data.length > 0); |
| 484 data = builder.takeBytes(); | 484 data = builder.takeBytes(); |
| 485 } else { | 485 } else { |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 663 | 663 |
| 664 Future<List<int>> read(int bytes) { | 664 Future<List<int>> read(int bytes) { |
| 665 if (bytes is !int) { | 665 if (bytes is !int) { |
| 666 throw new ArgumentError(bytes); | 666 throw new ArgumentError(bytes); |
| 667 } | 667 } |
| 668 return _dispatch(_FILE_READ, [null, bytes]).then((response) { | 668 return _dispatch(_FILE_READ, [null, bytes]).then((response) { |
| 669 if (_isErrorResponse(response)) { | 669 if (_isErrorResponse(response)) { |
| 670 throw _exceptionFromResponse(response, "read failed", path); | 670 throw _exceptionFromResponse(response, "read failed", path); |
| 671 } | 671 } |
| 672 _resourceInfo.addRead(response[1].length); | 672 _resourceInfo.addRead(response[1].length); |
| 673 return response[1] as Object/*=List<int>*/; | 673 return response[1]; |
| 674 }); | 674 }); |
| 675 } | 675 } |
| 676 | 676 |
| 677 List<int> readSync(int bytes) { | 677 List<int> readSync(int bytes) { |
| 678 _checkAvailable(); | 678 _checkAvailable(); |
| 679 if (bytes is !int) { | 679 if (bytes is !int) { |
| 680 throw new ArgumentError(bytes); | 680 throw new ArgumentError(bytes); |
| 681 } | 681 } |
| 682 var result = _ops.read(bytes); | 682 var result = _ops.read(bytes); |
| 683 if (result is OSError) { | 683 if (result is OSError) { |
| 684 throw new FileSystemException("readSync failed", path, result); | 684 throw new FileSystemException("readSync failed", path, result); |
| 685 } | 685 } |
| 686 _resourceInfo.addRead(result.length); | 686 _resourceInfo.addRead(result.length); |
| 687 return result as Object/*=List<int>*/; | 687 return result; |
| 688 } | 688 } |
| 689 | 689 |
| 690 Future<int> readInto(List<int> buffer, [int start = 0, int end]) { | 690 Future<int> readInto(List<int> buffer, [int start = 0, int end]) { |
| 691 if ((buffer is !List) || | 691 if ((buffer is !List) || |
| 692 ((start != null) && (start is !int)) || | 692 ((start != null) && (start is !int)) || |
| 693 ((end != null) && (end is !int))) { | 693 ((end != null) && (end is !int))) { |
| 694 throw new ArgumentError(); | 694 throw new ArgumentError(); |
| 695 } | 695 } |
| 696 end = RangeError.checkValidRange(start, end, buffer.length); | 696 end = RangeError.checkValidRange(start, end, buffer.length); |
| 697 if (end == start) { | 697 if (end == start) { |
| 698 return new Future.value(0); | 698 return new Future.value(0); |
| 699 } | 699 } |
| 700 int length = end - start; | 700 int length = end - start; |
| 701 return _dispatch(_FILE_READ_INTO, [null, length]).then((response) { | 701 return _dispatch(_FILE_READ_INTO, [null, length]).then((response) { |
| 702 if (_isErrorResponse(response)) { | 702 if (_isErrorResponse(response)) { |
| 703 throw _exceptionFromResponse(response, "readInto failed", path); | 703 throw _exceptionFromResponse(response, "readInto failed", path); |
| 704 } | 704 } |
| 705 var read = response[1]; | 705 var read = response[1]; |
| 706 var data = response[2] as Object/*=List<int>*/; | 706 var data = response[2]; |
| 707 buffer.setRange(start, start + read, data); | 707 buffer.setRange(start, start + read, data); |
| 708 _resourceInfo.addRead(read); | 708 _resourceInfo.addRead(read); |
| 709 return read; | 709 return read; |
| 710 }); | 710 }); |
| 711 } | 711 } |
| 712 | 712 |
| 713 int readIntoSync(List<int> buffer, [int start = 0, int end]) { | 713 int readIntoSync(List<int> buffer, [int start = 0, int end]) { |
| 714 _checkAvailable(); | 714 _checkAvailable(); |
| 715 if ((buffer is !List) || | 715 if ((buffer is !List) || |
| 716 ((start != null) && (start is !int)) || | 716 ((start != null) && (start is !int)) || |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1016 void _checkAvailable() { | 1016 void _checkAvailable() { |
| 1017 if (_asyncDispatched) { | 1017 if (_asyncDispatched) { |
| 1018 throw new FileSystemException("An async operation is currently pending", | 1018 throw new FileSystemException("An async operation is currently pending", |
| 1019 path); | 1019 path); |
| 1020 } | 1020 } |
| 1021 if (closed) { | 1021 if (closed) { |
| 1022 throw new FileSystemException("File closed", path); | 1022 throw new FileSystemException("File closed", path); |
| 1023 } | 1023 } |
| 1024 } | 1024 } |
| 1025 } | 1025 } |
| OLD | NEW |