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

Unified Diff: sdk/lib/io/file_impl.dart

Issue 23953004: Throw FileExceptions when in File when reading as String and decoding fails. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/standalone/io/file_read_encoded_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/io/file_impl.dart
diff --git a/sdk/lib/io/file_impl.dart b/sdk/lib/io/file_impl.dart
index ba6b64279c3bfa5231912cb10b73dd69a7cfd554..d803c950ef4b72bcf0cb9d38f67162253194afbc 100644
--- a/sdk/lib/io/file_impl.dart
+++ b/sdk/lib/io/file_impl.dart
@@ -509,28 +509,42 @@ class _File extends FileSystemEntity implements File {
return builder.takeBytes();
}
+ String _tryDecode(List<int> bytes, Encoding encoding) {
+ try {
+ return encoding.decode(bytes);
+ } catch (_) {
+ throw new FileException(
+ "Failed to decode data using encoding '${encoding.name}'", path);
+ }
+ }
+
Future<String> readAsString({Encoding encoding: UTF8}) {
_ensureFileService();
return readAsBytes().then((bytes) {
- return encoding.decode(bytes);
+ return _tryDecode(bytes, encoding);
});
}
String readAsStringSync({Encoding encoding: UTF8}) {
List<int> bytes = readAsBytesSync();
- return encoding.decode(bytes);
+ return _tryDecode(bytes, encoding);
}
- static List<String> _decodeLines(List<int> bytes, Encoding encoding) {
+ List<String> _decodeLines(List<int> bytes, Encoding encoding) {
if (bytes.length == 0) return [];
var list = [];
var controller = new StreamController(sync: true);
+ var error = null;
controller.stream
.transform(encoding.decoder)
.transform(new LineSplitter())
- .listen((line) => list.add(line));
+ .listen((line) => list.add(line), onError: (e) => error = e);
controller.add(bytes);
controller.close();
+ if (error != null) {
+ throw new FileException(
+ "Failed to decode data using encoding '${encoding.name}'", path);
+ }
return list;
}
« no previous file with comments | « no previous file | tests/standalone/io/file_read_encoded_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698