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

Unified Diff: runtime/bin/file_impl.dart

Issue 9452014: Implement File.{readAsBytes, readAsText, readAsLines}. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 8 years, 10 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 | « runtime/bin/file.dart ('k') | tests/standalone/src/FileTest.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/file_impl.dart
diff --git a/runtime/bin/file_impl.dart b/runtime/bin/file_impl.dart
index 4be9ef345d22c519c5b4b00e173977d9b4f52344..cbbb8a566a79ee0be65b7f3a12396b39f03dfff3 100644
--- a/runtime/bin/file_impl.dart
+++ b/runtime/bin/file_impl.dart
@@ -765,6 +765,7 @@ class _File implements File {
}
void openInputStream() {
+ _asyncUsed = true;
// Create a new file object to handle the opening of the file for
// creating an input stream. Currently the file input stream uses
// synchronous calls on the opened file so we need to open it
@@ -792,6 +793,7 @@ class _File implements File {
}
void openOutputStream([FileMode mode = FileMode.WRITE]) {
+ _asyncUsed = true;
if (mode != FileMode.WRITE &&
mode != FileMode.APPEND) {
throw new FileIOException(
@@ -827,6 +829,130 @@ class _File implements File {
return new _FileOutputStream(openedFile);
}
+ void readAsBytes() {
+ _asyncUsed = true;
+ var chunks = new _BufferList();
+ openInputStream();
+ inputStreamHandler = (inputStream) {
+ inputStream.closeHandler = () {
+ if (_readAsBytesHandler != null) {
+ _readAsBytesHandler(chunks.readBytes(chunks.length));
+ }
+ };
+ inputStream.dataHandler = () {
+ var chunk = inputStream.read();
+ chunks.add(chunk);
+ };
+ inputStream.errorHandler = () {
+ if (_errorHandler != null) {
+ _errorHandler("Failed to read file as bytes: $_name");
+ }
+ };
+ };
+ }
+
+ List<int> readAsBytesSync() {
+ if (_asyncUsed) {
+ throw new FileIOException(
+ "Mixed use of synchronous and asynchronous API");
+ }
+ var opened = openSync();
+ var length = opened.lengthSync();
+ var result = new ByteArray(length);
+ var read = opened.readListSync(result, 0, length);
+ if (read != length) {
+ throw new FileIOException("Failed reading file as bytes: $_name");
+ }
+ return result;
+ }
+
+ _StringDecoder _getDecoder(encoding) {
+ if (encoding == "UTF-8") {
+ return new _UTF8Decoder();
+ } else if (encoding == "ISO-8859-1") {
+ return new _Latin1Decoder();
+ } else if (encoding == "ASCII") {
+ return new _AsciiDecoder();
+ }
+ throw new FileIOException("Unsupported encoding $_encoding");
+ }
+
+ void readAsText([String encoding = "UTF-8"]) {
+ _asyncUsed = true;
+ var decoder = _getDecoder(encoding);
+ readAsBytes();
+ readAsBytesHandler = (bytes) {
+ if (_readAsTextHandler != null) {
+ try {
+ decoder.write(bytes);
+ } catch (var e) {
+ if (_errorHandler != null) {
+ _errorHandler(e.toString());
+ return;
+ }
+ }
+ _readAsTextHandler(decoder.decoded);
+ }
+ };
+ }
+
+ String readAsTextSync([String encoding = "UTF-8"]) {
+ if (_asyncUsed) {
+ throw new FileIOException(
+ "Mixed use of synchronous and asynchronous API");
+ }
+ var decoder = _getDecoder(encoding);
+ List<int> bytes = readAsBytesSync();
+ decoder.write(bytes);
+ return decoder.decoded;
+ }
+
+ List<String> _getDecodedLines(_StringDecoder decoder) {
+ List<String> result = [];
+ var line = decoder.decodedLine;
+ while (line != null) {
+ result.add(line);
+ line = decoder.decodedLine;
+ }
+ // If there is more data with no terminating line break we treat
+ // it as the last line.
+ var data = decoder.decoded;
+ if (data != null) {
+ result.add(data);
+ }
+ return result;
+ }
+
+ void readAsLines([String encoding = "UTF-8"]) {
+ _asyncUsed = true;
+ var decoder = _getDecoder(encoding);
+ readAsBytes();
+ readAsBytesHandler = (bytes) {
+ if (_readAsLinesHandler != null) {
+ try {
+ decoder.write(bytes);
+ } catch (var e) {
+ if (_errorHandler != null) {
+ _errorHandler(e.toString());
+ return;
+ }
+ }
+ _readAsLinesHandler(_getDecodedLines(decoder));
+ }
+ };
+ }
+
+ List<String> readAsLinesSync([String encoding = "UTF-8"]) {
+ if (_asyncUsed) {
+ throw new FileIOException(
+ "Mixed use of synchronous and asynchronous API");
+ }
+ var decoder = _getDecoder(encoding);
+ List<int> bytes = readAsBytesSync();
+ decoder.write(bytes);
+ return _getDecodedLines(decoder);
+ }
+
String get name() => _name;
void set existsHandler(void handler(bool exists)) {
@@ -857,6 +983,18 @@ class _File implements File {
_outputStreamHandler = handler;
}
+ void set readAsBytesHandler(void handler(List<int> bytes)) {
+ _readAsBytesHandler = handler;
+ }
+
+ void set readAsTextHandler(void handler(String text)) {
+ _readAsTextHandler = handler;
+ }
+
+ void set readAsLinesHandler(void handler(List<String> lines)) {
+ _readAsLinesHandler = handler;
+ }
+
void set fullPathHandler(void handler(String)) {
_fullPathHandler = handler;
}
@@ -877,6 +1015,9 @@ class _File implements File {
Function _openHandler;
Function _inputStreamHandler;
Function _outputStreamHandler;
+ Function _readAsBytesHandler;
+ Function _readAsTextHandler;
+ Function _readAsLinesHandler;
Function _fullPathHandler;
Function _errorHandler;
}
« no previous file with comments | « runtime/bin/file.dart ('k') | tests/standalone/src/FileTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698