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 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 throw new ArgumentError( | 472 throw new ArgumentError( |
473 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND"); | 473 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND"); |
474 } | 474 } |
475 var consumer = new _FileStreamConsumer(this, mode); | 475 var consumer = new _FileStreamConsumer(this, mode); |
476 return new IOSink(consumer, encoding: encoding); | 476 return new IOSink(consumer, encoding: encoding); |
477 } | 477 } |
478 | 478 |
479 Future<List<int>> readAsBytes() { | 479 Future<List<int>> readAsBytes() { |
480 _ensureFileService(); | 480 _ensureFileService(); |
481 Completer<List<int>> completer = new Completer<List<int>>(); | 481 Completer<List<int>> completer = new Completer<List<int>>(); |
482 var chunks = new _BufferList(); | 482 var builder = new BytesBuilder(); |
483 openRead().listen( | 483 openRead().listen( |
484 (d) => chunks.add(d), | 484 (d) => builder.add(d), |
485 onDone: () { | 485 onDone: () { |
486 var result = chunks.readBytes(); | 486 completer.complete(builder.takeBytes()); |
487 completer.complete(result); | |
488 }, | 487 }, |
489 onError: (e) { | 488 onError: (e) { |
490 completer.completeError(e); | 489 completer.completeError(e); |
491 }, | 490 }, |
492 cancelOnError: true); | 491 cancelOnError: true); |
493 return completer.future; | 492 return completer.future; |
494 } | 493 } |
495 | 494 |
496 List<int> readAsBytesSync() { | 495 List<int> readAsBytesSync() { |
497 var opened = openSync(); | 496 var opened = openSync(); |
498 var chunks = new _BufferList(); | 497 var builder = new BytesBuilder(); |
499 var data; | 498 var data; |
500 while ((data = opened.readSync(_BLOCK_SIZE)).length > 0) { | 499 while ((data = opened.readSync(_BLOCK_SIZE)).length > 0) { |
501 chunks.add(data); | 500 builder.add(data); |
502 } | 501 } |
503 opened.closeSync(); | 502 opened.closeSync(); |
504 return chunks.readBytes(); | 503 return builder.takeBytes(); |
505 } | 504 } |
506 | 505 |
507 Future<String> readAsString({Encoding encoding: Encoding.UTF_8}) { | 506 Future<String> readAsString({Encoding encoding: Encoding.UTF_8}) { |
508 _ensureFileService(); | 507 _ensureFileService(); |
509 return readAsBytes().then((bytes) { | 508 return readAsBytes().then((bytes) { |
510 return _decodeString(bytes, encoding); | 509 return _decodeString(bytes, encoding); |
511 }); | 510 }); |
512 } | 511 } |
513 | 512 |
514 String readAsStringSync({Encoding encoding: Encoding.UTF_8}) { | 513 String readAsStringSync({Encoding encoding: Encoding.UTF_8}) { |
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
987 | 986 |
988 Future _closedException() { | 987 Future _closedException() { |
989 return new Future.error(new FileException("File closed", _path)); | 988 return new Future.error(new FileException("File closed", _path)); |
990 } | 989 } |
991 | 990 |
992 final String _path; | 991 final String _path; |
993 int _id; | 992 int _id; |
994 | 993 |
995 SendPort _fileService; | 994 SendPort _fileService; |
996 } | 995 } |
OLD | NEW |