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

Side by Side Diff: runtime/bin/file_impl.dart

Issue 10913005: Better handling of reading empty files (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Changed the aproach slightly Created 8 years, 3 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 | runtime/tests/vm/data/empty_file » ('j') | 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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 class _FileInputStream extends _BaseDataInputStream implements InputStream { 5 class _FileInputStream extends _BaseDataInputStream implements InputStream {
6 _FileInputStream(String name) 6 _FileInputStream(String name)
7 : _data = const [], 7 : _data = const [],
8 _position = 0, 8 _position = 0,
9 _filePosition = 0 { 9 _filePosition = 0 {
10 var file = new File(name); 10 var file = new File(name);
(...skipping 674 matching lines...) Expand 10 before | Expand all | Expand 10 after
685 } 685 }
686 return new _FileOutputStream(_name, mode); 686 return new _FileOutputStream(_name, mode);
687 } 687 }
688 688
689 Future<List<int>> readAsBytes() { 689 Future<List<int>> readAsBytes() {
690 _ensureFileService(); 690 _ensureFileService();
691 Completer<List<int>> completer = new Completer<List<int>>(); 691 Completer<List<int>> completer = new Completer<List<int>>();
692 var chunks = new _BufferList(); 692 var chunks = new _BufferList();
693 var stream = openInputStream(); 693 var stream = openInputStream();
694 stream.onClosed = () { 694 stream.onClosed = () {
695 completer.complete(chunks.readBytes(chunks.length)); 695 var result = chunks.readBytes(chunks.length);
696 if (result == null) result = <int>[];
697 completer.complete(result);
696 }; 698 };
697 stream.onData = () { 699 stream.onData = () {
698 var chunk = stream.read(); 700 var chunk = stream.read();
699 chunks.add(chunk); 701 chunks.add(chunk);
700 }; 702 };
701 stream.onError = completer.completeException; 703 stream.onError = completer.completeException;
702 return completer.future; 704 return completer.future;
703 } 705 }
704 706
705 List<int> readAsBytesSync() { 707 List<int> readAsBytesSync() {
706 var opened = openSync(); 708 var opened = openSync();
707 var length = opened.lengthSync(); 709 var length = opened.lengthSync();
708 var result = new Uint8List(length); 710 var result = new Uint8List(length);
709 var read = opened.readListSync(result, 0, length); 711 var read = opened.readListSync(result, 0, length);
710 if (read != length) { 712 if (read != length) {
711 throw new FileIOException("Failed to read file"); 713 throw new FileIOException("Failed to read file");
712 } 714 }
713 opened.closeSync(); 715 opened.closeSync();
714 return result; 716 return result;
715 } 717 }
716 718
717 Future<String> readAsText([Encoding encoding = Encoding.UTF_8]) { 719 Future<String> readAsText([Encoding encoding = Encoding.UTF_8]) {
718 _ensureFileService(); 720 _ensureFileService();
719 return readAsBytes().transform((bytes) { 721 return readAsBytes().transform((bytes) {
722 if (bytes.length == 0) return "";
720 var decoder = _StringDecoders.decoder(encoding); 723 var decoder = _StringDecoders.decoder(encoding);
721 decoder.write(bytes); 724 decoder.write(bytes);
722 return decoder.decoded; 725 return decoder.decoded;
723 }); 726 });
724 } 727 }
725 728
726 String readAsTextSync([Encoding encoding = Encoding.UTF_8]) { 729 String readAsTextSync([Encoding encoding = Encoding.UTF_8]) {
727 var decoder = _StringDecoders.decoder(encoding); 730 var decoder = _StringDecoders.decoder(encoding);
728 List<int> bytes = readAsBytesSync(); 731 List<int> bytes = readAsBytesSync();
732 if (bytes.length == 0) return "";
729 decoder.write(bytes); 733 decoder.write(bytes);
730 return decoder.decoded; 734 return decoder.decoded;
731 } 735 }
732 736
733 List<String> _getDecodedLines(_StringDecoder decoder) { 737 List<String> _getDecodedLines(_StringDecoder decoder) {
734 List<String> result = []; 738 List<String> result = [];
735 var line = decoder.decodedLine; 739 var line = decoder.decodedLine;
736 while (line != null) { 740 while (line != null) {
737 result.add(line); 741 result.add(line);
738 line = decoder.decodedLine; 742 line = decoder.decodedLine;
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after
1161 new FileIOException("File closed '$_name'")); 1165 new FileIOException("File closed '$_name'"));
1162 }); 1166 });
1163 return completer.future; 1167 return completer.future;
1164 } 1168 }
1165 1169
1166 final String _name; 1170 final String _name;
1167 int _id; 1171 int _id;
1168 1172
1169 SendPort _fileService; 1173 SendPort _fileService;
1170 } 1174 }
OLDNEW
« no previous file with comments | « no previous file | runtime/tests/vm/data/empty_file » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698