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 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 if (mode != FileMode.WRITE && | 429 if (mode != FileMode.WRITE && |
430 mode != FileMode.APPEND) { | 430 mode != FileMode.APPEND) { |
431 throw new ArgumentError( | 431 throw new ArgumentError( |
432 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND"); | 432 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND"); |
433 } | 433 } |
434 var consumer = new _FileStreamConsumer(this, mode); | 434 var consumer = new _FileStreamConsumer(this, mode); |
435 return new IOSink(consumer, encoding: encoding); | 435 return new IOSink(consumer, encoding: encoding); |
436 } | 436 } |
437 | 437 |
438 Future<List<int>> readAsBytes() { | 438 Future<List<int>> readAsBytes() { |
439 Completer<List<int>> completer = new Completer<List<int>>(); | 439 Future<List<int>> readDataChunked(file) { |
440 var builder = new BytesBuilder(); | 440 var builder = new BytesBuilder(copy: false); |
441 openRead().listen( | 441 var completer = new Completer(); |
442 (d) => builder.add(d), | 442 void read() { |
443 onDone: () { | 443 file.read(_BLOCK_SIZE).then((data) { |
444 completer.complete(builder.takeBytes()); | 444 if (data.length > 0) builder.add(data); |
445 }, | 445 if (data.length == _BLOCK_SIZE) { |
446 onError: (e, StackTrace stackTrace) { | 446 read(); |
447 completer.completeError(e, stackTrace); | 447 } else { |
448 }, | 448 completer.complete(builder.takeBytes()); |
449 cancelOnError: true); | 449 } |
450 return completer.future; | 450 }, onError: completer.completeError); |
| 451 } |
| 452 read(); |
| 453 return completer.future; |
| 454 } |
| 455 |
| 456 return open().then((file) { |
| 457 return file.length().then((length) { |
| 458 if (length == 0) { |
| 459 // May be character device, try to read it in chunks. |
| 460 return readDataChunked(file); |
| 461 } |
| 462 return file.read(length); |
| 463 }).whenComplete(file.close); |
| 464 }); |
451 } | 465 } |
452 | 466 |
453 List<int> readAsBytesSync() { | 467 List<int> readAsBytesSync() { |
454 var opened = openSync(); | 468 var opened = openSync(); |
455 var builder = new BytesBuilder(); | |
456 var data; | 469 var data; |
457 while ((data = opened.readSync(_BLOCK_SIZE)).length > 0) { | 470 var length = opened.lengthSync(); |
458 builder.add(data); | 471 if (length == 0) { |
| 472 // May be character device, try to read it in chunks. |
| 473 var builder = new BytesBuilder(copy: false); |
| 474 do { |
| 475 data = opened.readSync(_BLOCK_SIZE); |
| 476 if (data.length > 0) builder.add(data); |
| 477 } while (data.length == _BLOCK_SIZE); |
| 478 data = builder.takeBytes(); |
| 479 } else { |
| 480 data = opened.readSync(length); |
459 } | 481 } |
460 opened.closeSync(); | 482 opened.closeSync(); |
461 return builder.takeBytes(); | 483 return data; |
462 } | 484 } |
463 | 485 |
464 String _tryDecode(List<int> bytes, Encoding encoding) { | 486 String _tryDecode(List<int> bytes, Encoding encoding) { |
465 try { | 487 try { |
466 return encoding.decode(bytes); | 488 return encoding.decode(bytes); |
467 } catch (_) { | 489 } catch (_) { |
468 throw new FileSystemException( | 490 throw new FileSystemException( |
469 "Failed to decode data using encoding '${encoding.name}'", path); | 491 "Failed to decode data using encoding '${encoding.name}'", path); |
470 } | 492 } |
471 } | 493 } |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
901 | 923 |
902 void _checkAvailable() { | 924 void _checkAvailable() { |
903 if (_asyncDispatched) { | 925 if (_asyncDispatched) { |
904 throw new FileSystemException("An async operation is currently pending", p
ath); | 926 throw new FileSystemException("An async operation is currently pending", p
ath); |
905 } | 927 } |
906 if (closed) { | 928 if (closed) { |
907 throw new FileSystemException("File closed", path); | 929 throw new FileSystemException("File closed", path); |
908 } | 930 } |
909 } | 931 } |
910 } | 932 } |
OLD | NEW |