Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(300)

Side by Side Diff: sdk/lib/io/file_impl.dart

Issue 251583004: Clean up readAsLines*. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 476 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 return encoding.decode(bytes); 487 return encoding.decode(bytes);
488 } catch (_) { 488 } catch (_) {
489 throw new FileSystemException( 489 throw new FileSystemException(
490 "Failed to decode data using encoding '${encoding.name}'", path); 490 "Failed to decode data using encoding '${encoding.name}'", path);
491 } 491 }
492 } 492 }
493 493
494 Future<String> readAsString({Encoding encoding: UTF8}) => 494 Future<String> readAsString({Encoding encoding: UTF8}) =>
495 readAsBytes().then((bytes) => _tryDecode(bytes, encoding)); 495 readAsBytes().then((bytes) => _tryDecode(bytes, encoding));
496 496
497 String readAsStringSync({Encoding encoding: UTF8}) { 497 String readAsStringSync({Encoding encoding: UTF8}) =>
498 List<int> bytes = readAsBytesSync(); 498 _tryDecode(readAsBytesSync(), encoding);
499 return _tryDecode(bytes, encoding);
500 }
501 499
502 List<String> _decodeLines(List<int> bytes, Encoding encoding) { 500 Future<List<String>> readAsLines({Encoding encoding: UTF8}) =>
503 if (bytes.length == 0) return []; 501 readAsString(encoding: encoding).then(const LineSplitter().convert);
504 var list = [];
505 var controller = new StreamController(sync: true);
506 var error = null;
507 controller.stream
508 .transform(encoding.decoder)
509 .transform(new LineSplitter())
510 .listen((line) => list.add(line), onError: (e) => error = e);
511 controller.add(bytes);
512 controller.close();
513 if (error != null) {
514 throw new FileSystemException(
515 "Failed to decode data using encoding '${encoding.name}'", path);
516 }
517 return list;
518 }
519
520 Future<List<String>> readAsLines({Encoding encoding: UTF8}) {
521 return readAsBytes().then((bytes) {
522 return _decodeLines(bytes, encoding);
523 });
524 }
525 502
526 List<String> readAsLinesSync({Encoding encoding: UTF8}) => 503 List<String> readAsLinesSync({Encoding encoding: UTF8}) =>
527 _decodeLines(readAsBytesSync(), encoding); 504 const LineSplitter().convert(readAsStringSync(encoding: encoding));
528 505
529 Future<File> writeAsBytes(List<int> bytes, 506 Future<File> writeAsBytes(List<int> bytes,
530 {FileMode mode: FileMode.WRITE, 507 {FileMode mode: FileMode.WRITE,
531 bool flush: false}) { 508 bool flush: false}) {
532 return open(mode: mode).then((file) { 509 return open(mode: mode).then((file) {
533 return file.writeFrom(bytes, 0, bytes.length) 510 return file.writeFrom(bytes, 0, bytes.length)
534 .then((_) { 511 .then((_) {
535 if (flush) return file.flush().then((_) => this); 512 if (flush) return file.flush().then((_) => this);
536 return this; 513 return this;
537 }) 514 })
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after
922 void _checkAvailable() { 899 void _checkAvailable() {
923 if (_asyncDispatched) { 900 if (_asyncDispatched) {
924 throw new FileSystemException("An async operation is currently pending", 901 throw new FileSystemException("An async operation is currently pending",
925 path); 902 path);
926 } 903 }
927 if (closed) { 904 if (closed) {
928 throw new FileSystemException("File closed", path); 905 throw new FileSystemException("File closed", path);
929 } 906 }
930 } 907 }
931 } 908 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698