| Index: runtime/bin/file_impl.dart
|
| diff --git a/runtime/bin/file_impl.dart b/runtime/bin/file_impl.dart
|
| index 00f801d7d9585db6bb4f933fe94497dd1ea97265..5efce7e440d6463b710cc4db14a102311126276d 100644
|
| --- a/runtime/bin/file_impl.dart
|
| +++ b/runtime/bin/file_impl.dart
|
| @@ -2,9 +2,10 @@
|
| // for details. All rights reserved. Use of this source code is governed by a
|
| // BSD-style license that can be found in the LICENSE file.
|
|
|
| -class _FileInputStream implements InputStream {
|
| +class _FileInputStream implements FileInputStream {
|
| _FileInputStream(File file) {
|
| - _file = file;
|
| + _file = new File(file.name);
|
| + _file.openSync();
|
| }
|
|
|
| List<int> read([int len]) {
|
| @@ -20,7 +21,7 @@ class _FileInputStream implements InputStream {
|
| }
|
| }
|
| List<int> buffer = new List<int>(bytesToRead);
|
| - int bytesRead = _file.readList(buffer, 0, bytesToRead);
|
| + int bytesRead = _file.readListSync(buffer, 0, bytesToRead);
|
| if (bytesRead < bytesToRead) {
|
| List<int> newBuffer = new List<int>(bytesRead);
|
| newBuffer.copyFrom(buffer, 0, 0, bytesRead);
|
| @@ -35,15 +36,19 @@ class _FileInputStream implements InputStream {
|
| if (len == null) len = buffer.length;
|
| if (offset < 0) throw new StreamException("Illegal offset $offset");
|
| if (len < 0) throw new StreamException("Illegal length $len");
|
| - return _file.readList(buffer, offset, len);
|
| + return _file.readListSync(buffer, offset, len);
|
| }
|
|
|
| int available() {
|
| - return _file.length - _file.position;
|
| + return _file.lengthSync() - _file.positionSync();
|
| }
|
|
|
| bool closed() {
|
| - _file.position == _file.length;
|
| + _file.positionSync() == _file.lengthSync();
|
| + }
|
| +
|
| + void close() {
|
| + _file.closeSync();
|
| }
|
|
|
| void set dataHandler(void callback()) {
|
| @@ -62,13 +67,14 @@ class _FileInputStream implements InputStream {
|
| }
|
|
|
|
|
| -class _FileOutputStream implements OutputStream {
|
| +class _FileOutputStream implements FileOutputStream {
|
| _FileOutputStream(File file) {
|
| - _file = file;
|
| + _file = new File(file.name);
|
| + _file.openSync(true);
|
| }
|
|
|
| bool write(List<int> buffer, int offset, int len, void callback()) {
|
| - int bytesWritten = _file.writeList(buffer, offset, len);
|
| + int bytesWritten = _file.writeListSync(buffer, offset, len);
|
|
|
| if (bytesWritten == len) {
|
| return true;
|
| @@ -77,58 +83,78 @@ class _FileOutputStream implements OutputStream {
|
| }
|
| }
|
|
|
| + void close() {
|
| + _file.closeSync();
|
| + }
|
| +
|
| File _file;
|
| }
|
|
|
|
|
| // Class for encapsulating the native implementation of files.
|
| -class _File extends FileNativeWrapper implements File {
|
| +class _File implements File {
|
| // Constructor for file.
|
| - factory _File(String name, bool writable) {
|
| - _File file = new _File._internal();
|
| - if (!file._openFile(name, writable)) {
|
| - return null;
|
| + _File(String this._name);
|
| +
|
| + void exists() {
|
| + throw "Unimplemented";
|
| + }
|
| +
|
| + bool existsSync() {
|
| + return _fileExists(_name);
|
| + }
|
| +
|
| + bool _fileExists(String name) native "File_Exists";
|
| +
|
| + void create() {
|
| + throw "Unimplemented";
|
| + }
|
| +
|
| + void createSync() {
|
| + throw "Unimplemented";
|
| + }
|
| +
|
| + void open([bool writable = false]) {
|
| + throw "Unimplemented";
|
| + }
|
| +
|
| + void openSync([bool writable = false]) {
|
| + if (!_openFile(_name, writable)) {
|
| + throw new FileIOException("Cannot open file: $_name");
|
| }
|
| - file._writeable = writable;
|
| - return file;
|
| }
|
| - _File._internal();
|
|
|
| bool _openFile(String name, bool writable) native "File_OpenFile";
|
|
|
| void close() {
|
| - _close();
|
| + throw "Unimplemented";
|
| }
|
| +
|
| + void closeSync() {
|
| + _close();
|
| + }
|
| +
|
| int _close() native "File_Close";
|
|
|
| - int readByte() {
|
| + void readByte() {
|
| + throw "Unimplemented";
|
| + }
|
| +
|
| + int readByteSync() {
|
| int result = _readByte();
|
| if (result == -1) {
|
| throw new FileIOException("Error: readByte failed");
|
| }
|
| return result;
|
| }
|
| - int _readByte() native "File_ReadByte";
|
|
|
| - int writeByte(int value) {
|
| - int result = _writeByte(value);
|
| - if (result == -1) {
|
| - throw new FileIOException("Error: writeByte failed");
|
| - }
|
| - return result;
|
| - }
|
| - int _writeByte(int value) native "File_WriteByte";
|
| + int _readByte() native "File_ReadByte";
|
|
|
| - int writeString(String string) {
|
| - int result = _writeString(string);
|
| - if (result == -1) {
|
| - throw new FileIOException("Error: writeString failed");
|
| - }
|
| - return result;
|
| + void readList(List<int> buffer, int offset, int bytes) {
|
| + throw "Unimplemented";
|
| }
|
| - int _writeString(String string) native "File_WriteString";
|
|
|
| - int readList(List<int> buffer, int offset, int bytes) {
|
| + int readListSync(List<int> buffer, int offset, int bytes) {
|
| if (bytes == 0) {
|
| return 0;
|
| }
|
| @@ -147,10 +173,28 @@ class _File extends FileNativeWrapper implements File {
|
| }
|
| return result;
|
| }
|
| - int _readList(List<int> buffer, int offset, int bytes)
|
| - native "File_ReadList";
|
| +
|
| + int _readList(List<int> buffer, int offset, int bytes) native "File_ReadList";
|
| +
|
| + void writeByte(int value) {
|
| + throw "Unimplemented";
|
| + }
|
| +
|
| + int writeByteSync(int value) {
|
| + int result = _writeByte(value);
|
| + if (result == -1) {
|
| + throw new FileIOException("Error: writeByte failed");
|
| + }
|
| + return result;
|
| + }
|
| +
|
| + int _writeByte(int value) native "File_WriteByte";
|
|
|
| int writeList(List<int> buffer, int offset, int bytes) {
|
| + throw "Unimplemented";
|
| + }
|
| +
|
| + int writeListSync(List<int> buffer, int offset, int bytes) {
|
| if (bytes == 0) {
|
| return 0;
|
| }
|
| @@ -169,58 +213,118 @@ class _File extends FileNativeWrapper implements File {
|
| }
|
| return result;
|
| }
|
| +
|
| int _writeList(List<int> buffer, int offset, int bytes)
|
| native "File_WriteList";
|
|
|
| - int get position() {
|
| + int writeString(String string) {
|
| + throw "Unimplemented";
|
| + }
|
| +
|
| + int writeStringSync(String string) {
|
| + int result = _writeString(string);
|
| + if (result == -1) {
|
| + throw new FileIOException("Error: writeString failed");
|
| + }
|
| + return result;
|
| + }
|
| +
|
| + int _writeString(String string) native "File_WriteString";
|
| +
|
| + int position() {
|
| + throw "Unimplemented";
|
| + }
|
| +
|
| + int positionSync() {
|
| int result = _position;
|
| if (result == -1) {
|
| throw new FileIOException("Error: get position failed");
|
| }
|
| return result;
|
| }
|
| +
|
| int get _position() native "File_Position";
|
|
|
| - int get length() {
|
| + int length() {
|
| + throw "Unimplemented";
|
| + }
|
| +
|
| + int lengthSync() {
|
| int result = _length;
|
| if (result == -1) {
|
| throw new FileIOException("Error: get length failed");
|
| }
|
| return result;
|
| }
|
| +
|
| int get _length() native "File_Length";
|
|
|
| void flush() {
|
| + throw "Unimplemented";
|
| + }
|
| +
|
| + void flushSync() {
|
| int result = _flush();
|
| if (result == -1) {
|
| throw new FileIOException("Error: flush failed");
|
| }
|
| }
|
| +
|
| int _flush() native "File_Flush";
|
|
|
| - // Each file has an unique InputStream.
|
| - InputStream get inputStream() {
|
| - if (_inputStream == null) {
|
| - _inputStream = new _FileInputStream(this);
|
| - }
|
| - return _inputStream;
|
| + InputStream openInputStream() {
|
| + return new _FileInputStream(this);
|
| }
|
|
|
| - // Each file has an unique OutputStream.
|
| - OutputStream get outputStream() {
|
| - if (!_writeable) {
|
| - throw "File is not writable";
|
| - }
|
| - if (_outputStream == null) {
|
| - _outputStream = new _FileOutputStream(this);
|
| - }
|
| - return _outputStream;
|
| + OutputStream openOutputStream() {
|
| + return new _FileOutputStream(this);
|
| + }
|
| +
|
| + String get name() {
|
| + return _name;
|
| + }
|
| +
|
| + void set existsHandler(void handler(bool exists)) {
|
| + _existsHandler = handler;
|
| + }
|
| +
|
| + void set createHandler(void handler()) {
|
| + _createHandler = handler;
|
| + }
|
| +
|
| + void set openHandler(void handler()) {
|
| + _openHandler = handler;
|
| + }
|
| +
|
| + void set closeHandler(void handler()) {
|
| + _closeHandler = handler;
|
| + }
|
| +
|
| + void set readByteHandler(void handler(int byte)) {
|
| + _readByteHandler = handler;
|
| + }
|
| +
|
| + void set readListHandler(void handler(int read)) {
|
| + _readListHandler = handler;
|
| + }
|
| +
|
| + void set noPendingWriteHandler(void handler()) {
|
| + _noPendingWriteHandler = handler;
|
| + }
|
| +
|
| + void set errorHandler(void handler(String error)) {
|
| + _errorHandler = handler;
|
| }
|
|
|
| - // Set of native methods used to provide file functionality.
|
| - static bool fileExists(String name) native "File_Exists";
|
| + String _name;
|
| + int _id;
|
|
|
| - bool _writeable;
|
| - InputStream _inputStream;
|
| - OutputStream _outputStream;
|
| + var _existsHandler;
|
| + var _createHandler;
|
| + var _openHandler;
|
| + var _closeHandler;
|
| + var _readByteHandler;
|
| + var _readListHandler;
|
| + var _noPendingWriteHandler;
|
| + var _errorHandler;
|
| }
|
|
|