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 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
502 var opened = openSync(); | 502 var opened = openSync(); |
503 var builder = new BytesBuilder(); | 503 var builder = new BytesBuilder(); |
504 var data; | 504 var data; |
505 while ((data = opened.readSync(_BLOCK_SIZE)).length > 0) { | 505 while ((data = opened.readSync(_BLOCK_SIZE)).length > 0) { |
506 builder.add(data); | 506 builder.add(data); |
507 } | 507 } |
508 opened.closeSync(); | 508 opened.closeSync(); |
509 return builder.takeBytes(); | 509 return builder.takeBytes(); |
510 } | 510 } |
511 | 511 |
| 512 String _tryDecode(List<int> bytes, Encoding encoding) { |
| 513 try { |
| 514 return encoding.decode(bytes); |
| 515 } catch (_) { |
| 516 throw new FileException( |
| 517 "Failed to decode data using encoding '${encoding.name}'", path); |
| 518 } |
| 519 } |
| 520 |
512 Future<String> readAsString({Encoding encoding: UTF8}) { | 521 Future<String> readAsString({Encoding encoding: UTF8}) { |
513 _ensureFileService(); | 522 _ensureFileService(); |
514 return readAsBytes().then((bytes) { | 523 return readAsBytes().then((bytes) { |
515 return encoding.decode(bytes); | 524 return _tryDecode(bytes, encoding); |
516 }); | 525 }); |
517 } | 526 } |
518 | 527 |
519 String readAsStringSync({Encoding encoding: UTF8}) { | 528 String readAsStringSync({Encoding encoding: UTF8}) { |
520 List<int> bytes = readAsBytesSync(); | 529 List<int> bytes = readAsBytesSync(); |
521 return encoding.decode(bytes); | 530 return _tryDecode(bytes, encoding); |
522 } | 531 } |
523 | 532 |
524 static List<String> _decodeLines(List<int> bytes, Encoding encoding) { | 533 List<String> _decodeLines(List<int> bytes, Encoding encoding) { |
525 if (bytes.length == 0) return []; | 534 if (bytes.length == 0) return []; |
526 var list = []; | 535 var list = []; |
527 var controller = new StreamController(sync: true); | 536 var controller = new StreamController(sync: true); |
| 537 var error = null; |
528 controller.stream | 538 controller.stream |
529 .transform(encoding.decoder) | 539 .transform(encoding.decoder) |
530 .transform(new LineSplitter()) | 540 .transform(new LineSplitter()) |
531 .listen((line) => list.add(line)); | 541 .listen((line) => list.add(line), onError: (e) => error = e); |
532 controller.add(bytes); | 542 controller.add(bytes); |
533 controller.close(); | 543 controller.close(); |
| 544 if (error != null) { |
| 545 throw new FileException( |
| 546 "Failed to decode data using encoding '${encoding.name}'", path); |
| 547 } |
534 return list; | 548 return list; |
535 } | 549 } |
536 | 550 |
537 Future<List<String>> readAsLines({Encoding encoding: UTF8}) { | 551 Future<List<String>> readAsLines({Encoding encoding: UTF8}) { |
538 _ensureFileService(); | 552 _ensureFileService(); |
539 return readAsBytes().then((bytes) { | 553 return readAsBytes().then((bytes) { |
540 return _decodeLines(bytes, encoding); | 554 return _decodeLines(bytes, encoding); |
541 }); | 555 }); |
542 } | 556 } |
543 | 557 |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
983 void _checkNotClosed() { | 997 void _checkNotClosed() { |
984 if (closed) { | 998 if (closed) { |
985 throw new FileException("File closed", path); | 999 throw new FileException("File closed", path); |
986 } | 1000 } |
987 } | 1001 } |
988 | 1002 |
989 Future _closedException() { | 1003 Future _closedException() { |
990 return new Future.error(new FileException("File closed", path)); | 1004 return new Future.error(new FileException("File closed", path)); |
991 } | 1005 } |
992 } | 1006 } |
OLD | NEW |