Chromium Code Reviews| Index: runtime/bin/file_impl.dart |
| diff --git a/runtime/bin/file_impl.dart b/runtime/bin/file_impl.dart |
| index 4be9ef345d22c519c5b4b00e173977d9b4f52344..c070c9cbd153212ae5ec34c7752bcb5545b4351c 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,125 @@ class _File implements File { |
| return new _FileOutputStream(openedFile); |
| } |
| + void readAsBytes() { |
| + _asyncUsed = true; |
| + var chunks = []; |
|
Søren Gjesse
2012/02/24 07:24:51
We have the class _BufferList in runtime/bin.
var
Mads Ager (google)
2012/02/24 13:11:17
Done.
|
| + var resultLength = 0; |
| + openInputStream(); |
| + inputStreamHandler = (inputStream) { |
| + inputStream.closeHandler = () { |
| + if (_readAsBytesHandler != null) { |
| + var result = new ByteArray(resultLength); |
| + var resultIndex = 0; |
| + for (var i = 0; i < chunks.length; i++) { |
| + var chunk = chunks[i]; |
| + result.setRange(resultIndex, chunk.length, chunk); |
| + resultIndex += chunk.length; |
| + } |
| + _readAsBytesHandler(result); |
| + } |
| + }; |
| + inputStream.dataHandler = () { |
| + var chunk = inputStream.read(); |
| + chunks.add(chunk); |
| + resultLength += chunk.length; |
| + }; |
| + 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) { |
| + decoder.write(bytes); |
| + _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) { |
| + decoder.write(bytes); |
| + _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 +978,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 +1010,9 @@ class _File implements File { |
| Function _openHandler; |
| Function _inputStreamHandler; |
| Function _outputStreamHandler; |
| + Function _readAsBytesHandler; |
| + Function _readAsTextHandler; |
| + Function _readAsLinesHandler; |
| Function _fullPathHandler; |
| Function _errorHandler; |
| } |