Split File into File and RandomAccessFile.

Update all users of the API.

R=sgjesse@google.com
BUG=
TEST=

Review URL: http://codereview.chromium.org//8883017

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@2272 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
ager@google.com 2011-12-08 19:37:17 +00:00
parent 7578faca4b
commit 64ca753193
9 changed files with 370 additions and 373 deletions

View file

@ -49,18 +49,62 @@ interface File factory _File {
/**
* Open the file for random access operations. When the file is
* opened the openHandler is called. Opened files must be closed
* using the [close] method. By default writable is false.
* opened the openHandler is called with the resulting
* RandomAccessFile. RandomAccessFiles must be closed using the
* close method. By default writable is false.
*/
void open([bool writable]);
/**
* Synchronously open the file for random access operations. Opened
* files must be closed using the [close] method. By default
* writable is false.
* Synchronously open the file for random access operations. The
* result is a RandomAccessFile on which random access operations
* can be performed. Opened RandomAccessFiles must be closed using
* the close method. By default writable is false.
*/
void openSync([bool writable]);
RandomAccessFile openSync([bool writable]);
/**
* Get the canonical full path corresponding to the file name. The
* [fullPathHandler] is called when the fullPath operation
* completes.
*/
String fullPath();
/**
* Synchronously get the canonical full path corresponding to the file name.
*/
String fullPathSync();
/**
* Create a new independent input stream for the file. The file
* input stream must be closed when no longer used to free up
* system resources.
*/
FileInputStream openInputStream();
/**
* Creates a new independent output stream for the file. The file
* output stream must be closed when no longer used to free up
* system resources.
*/
FileOutputStream openOutputStream();
/**
* Get the name of the file.
*/
String get name();
// Event handlers.
void set existsHandler(void handler(bool exists));
void set createHandler(void handler());
void set deleteHandler(void handler());
void set openHandler(void handler(RandomAccessFile openedFile));
void set fullPathHandler(void handler(String path));
void set errorHandler(void handler(String error));
}
interface RandomAccessFile {
/**
* Close the file. When the file is closed the closeHandler is
* called.
@ -193,40 +237,11 @@ interface File factory _File {
*/
void flushSync();
/**
* Get the canonical full path corresponding to the file name. The
* [fullPathHandler] is called when the fullPath operation
* completes.
*/
String fullPath();
/**
* Synchronously get the canonical full path corresponding to the file name.
*/
String fullPathSync();
/**
* Create a new independent input stream for the file. The file
* input stream must be closed when no longer used.
*/
FileInputStream openInputStream();
/**
* Creates a new independent output stream for the file. The file
* output stream must be closed when no longer used.
*/
FileOutputStream openOutputStream();
/**
* Get the name of the file.
*/
String get name();
// Event handlers.
void set existsHandler(void handler(bool exists));
void set createHandler(void handler());
void set deleteHandler(void handler());
void set openHandler(void handler());
void set closeHandler(void handler());
void set readByteHandler(void handler(int byte));
void set readListHandler(void handler(int read));
@ -236,7 +251,6 @@ interface File factory _File {
void set truncateHandler(void handler());
void set lengthHandler(void handler(int length));
void set flushHandler(void handler());
void set fullPathHandler(void handler(String path));
void set errorHandler(void handler(String error));
}

View file

@ -4,8 +4,7 @@
class _FileInputStream implements FileInputStream {
_FileInputStream(File file) {
_file = new File(file.name);
_file.openSync();
_file = file.openSync();
_length = _file.lengthSync();
_checkScheduleCallbacks();
}
@ -105,7 +104,7 @@ class _FileInputStream implements FileInputStream {
}
}
File _file;
RandomAccessFile _file;
int _length;
bool _eof = false;
bool _closed = false;
@ -118,8 +117,7 @@ class _FileInputStream implements FileInputStream {
class _FileOutputStream implements FileOutputStream {
_FileOutputStream(File file) {
_file = new File(file.name);
_file.openSync(true);
_file = file.openSync(true);
}
bool write(List<int> buffer, [bool copyBuffer = false]) {
@ -160,7 +158,7 @@ class _FileOutputStream implements FileOutputStream {
}
}
File _file;
RandomAccessFile _file;
}
@ -181,7 +179,7 @@ class _ExistsOperation extends _FileOperation {
_ExistsOperation(String this._name);
void execute(ReceivePort port) {
_replyPort.send(_File._exists(_name), port.toSendPort());
_replyPort.send(_FileUtils.exists(_name), port.toSendPort());
}
String _name;
@ -192,7 +190,8 @@ class _OpenOperation extends _FileOperation {
_OpenOperation(String this._name, bool this._writable);
void execute(ReceivePort port) {
_replyPort.send(_File._checkedOpen(_name, _writable), port.toSendPort());
_replyPort.send(_FileUtils.checkedOpen(_name, _writable),
port.toSendPort());
}
String _name;
@ -204,7 +203,7 @@ class _CloseOperation extends _FileOperation {
_CloseOperation(int this._id);
void execute(ReceivePort port) {
_replyPort.send(_File._close(_id), port.toSendPort());
_replyPort.send(_FileUtils.close(_id), port.toSendPort());
}
int _id;
@ -215,7 +214,7 @@ class _ReadByteOperation extends _FileOperation {
_ReadByteOperation(int this._id);
void execute(ReceivePort port) {
_replyPort.send(_File._readByte(_id), port.toSendPort());
_replyPort.send(_FileUtils.readByte(_id), port.toSendPort());
}
int _id;
@ -240,7 +239,8 @@ class _ReadListOperation extends _FileOperation {
_replyPort.send(0, port.toSendPort());
return;
}
int index = _File._checkReadWriteListArguments(_length, _offset, _bytes);
int index =
_FileUtils.checkReadWriteListArguments(_length, _offset, _bytes);
if (index != 0) {
_replyPort.send("index out of range in readList: $index",
port.toSendPort());
@ -248,7 +248,8 @@ class _ReadListOperation extends _FileOperation {
}
var buffer = new List(_bytes);
var result =
new _ReadListResult(_File._readList(_id, buffer, 0, _bytes), buffer);
new _ReadListResult(_FileUtils.readList(_id, buffer, 0, _bytes),
buffer);
_replyPort.send(result, port.toSendPort());
}
@ -263,7 +264,7 @@ class _WriteByteOperation extends _FileOperation {
_WriteByteOperation(int this._id, int this._value);
void execute(ReceivePort port) {
_replyPort.send(_File._writeByte(_id, _value), port.toSendPort());
_replyPort.send(_FileUtils.writeByte(_id, _value), port.toSendPort());
}
bool isWrite() => true;
@ -285,13 +286,13 @@ class _WriteListOperation extends _FileOperation {
return;
}
int index =
_File._checkReadWriteListArguments(_buffer.length, _offset, _bytes);
_FileUtils.checkReadWriteListArguments(_buffer.length, _offset, _bytes);
if (index != 0) {
_replyPort.send("index out of range in writeList: $index",
port.toSendPort());
return;
}
var result = _File._writeList(_id, _buffer, _offset, _bytes);
var result = _FileUtils.writeList(_id, _buffer, _offset, _bytes);
_replyPort.send(result, port.toSendPort());
}
@ -308,7 +309,8 @@ class _WriteStringOperation extends _FileOperation {
_WriteStringOperation(int this._id, String this._string);
void execute(ReceivePort port) {
_replyPort.send(_File._checkedWriteString(_id, _string), port.toSendPort());
_replyPort.send(_FileUtils.checkedWriteString(_id, _string),
port.toSendPort());
}
bool isWrite() => true;
@ -322,7 +324,7 @@ class _PositionOperation extends _FileOperation {
_PositionOperation(int this._id);
void execute(ReceivePort port) {
_replyPort.send(_File._position(_id), port.toSendPort());
_replyPort.send(_FileUtils.position(_id), port.toSendPort());
}
int _id;
@ -333,7 +335,7 @@ class _SetPositionOperation extends _FileOperation {
_SetPositionOperation(int this._id, int this._position);
void execute(ReceivePort port) {
_replyPort.send(_File._setPosition(_id, _position), port.toSendPort());
_replyPort.send(_FileUtils.setPosition(_id, _position), port.toSendPort());
}
int _id;
@ -345,7 +347,7 @@ class _TruncateOperation extends _FileOperation {
_TruncateOperation(int this._id, int this._length);
void execute(ReceivePort port) {
_replyPort.send(_File._truncate(_id, _length), port.toSendPort());
_replyPort.send(_FileUtils.truncate(_id, _length), port.toSendPort());
}
int _id;
@ -357,7 +359,7 @@ class _LengthOperation extends _FileOperation {
_LengthOperation(int this._id);
void execute(ReceivePort port) {
_replyPort.send(_File._length(_id), port.toSendPort());
_replyPort.send(_FileUtils.length(_id), port.toSendPort());
}
int _id;
@ -368,7 +370,7 @@ class _FlushOperation extends _FileOperation {
_FlushOperation(int this._id);
void execute(ReceivePort port) {
_replyPort.send(_File._flush(_id), port.toSendPort());
_replyPort.send(_FileUtils.flush(_id), port.toSendPort());
}
int _id;
@ -379,7 +381,7 @@ class _FullPathOperation extends _FileOperation {
_FullPathOperation(String this._name);
void execute(ReceivePort port) {
_replyPort.send(_File._checkedFullPath(_name), port.toSendPort());
_replyPort.send(_FileUtils.checkedFullPath(_name), port.toSendPort());
}
String _name;
@ -390,7 +392,7 @@ class _CreateOperation extends _FileOperation {
_CreateOperation(String this._name);
void execute(ReceivePort port) {
_replyPort.send(_File._checkedCreate(_name), port.toSendPort());
_replyPort.send(_FileUtils.checkedCreate(_name), port.toSendPort());
}
String _name;
@ -401,7 +403,7 @@ class _DeleteOperation extends _FileOperation {
_DeleteOperation(String this._name);
void execute(ReceivePort port) {
_replyPort.send(_File._checkedDelete(_name), port.toSendPort());
_replyPort.send(_FileUtils.checkedDelete(_name), port.toSendPort());
}
String _name;
@ -477,64 +479,67 @@ class _FileOperationScheduler {
}
// Class for encapsulating the native implementation of files.
class _File implements File {
// Constructor for file.
_File(String this._name)
: _scheduler = new _FileOperationScheduler(),
_asyncUsed = false,
_id = 0;
static bool _exists(String name) native "File_Exists";
static int _open(String name, bool writable) native "File_Open";
static int _close(int id) native "File_Close";
static int _readByte(int id) native "File_ReadByte";
static int _readList(int id, List<int> buffer, int offset, int bytes)
// Helper class containing static file helper methods.
class _FileUtils {
static bool exists(String name) native "File_Exists";
static int open(String name, bool writable) native "File_Open";
static bool create(String name) native "File_Create";
static bool delete(String name) native "File_Delete";
static String fullPath(String name) native "File_FullPath";
static int close(int id) native "File_Close";
static int readByte(int id) native "File_ReadByte";
static int readList(int id, List<int> buffer, int offset, int bytes)
native "File_ReadList";
static int _writeByte(int id, int value) native "File_WriteByte";
static int _writeList(int id, List<int> buffer, int offset, int bytes)
static int writeByte(int id, int value) native "File_WriteByte";
static int writeList(int id, List<int> buffer, int offset, int bytes)
native "File_WriteList";
static int _writeString(int id, String string) native "File_WriteString";
static int _position(int id) native "File_Position";
static bool _setPosition(int id, int position) native "File_SetPosition";
static bool _truncate(int id, int length) native "File_Truncate";
static int _length(int id) native "File_Length";
static int _flush(int id) native "File_Flush";
static bool _create(String name) native "File_Create";
static bool _delete(String name) native "File_Delete";
static String _fullPath(String name) native "File_FullPath";
static int writeString(int id, String string) native "File_WriteString";
static int position(int id) native "File_Position";
static bool setPosition(int id, int position) native "File_SetPosition";
static bool truncate(int id, int length) native "File_Truncate";
static int length(int id) native "File_Length";
static int flush(int id) native "File_Flush";
static int _checkReadWriteListArguments(int length, int offset, int bytes) {
static int checkedOpen(String name, bool writable) {
if (name is !String || writable is !bool) return 0;
return open(name, writable);
}
static bool checkedCreate(String name) {
if (name is !String) return false;
return create(name);
}
static bool checkedDelete(String name) {
if (name is !String) return false;
return delete(name);
}
static String checkedFullPath(String name) {
if (name is !String) return null;
return fullPath(name);
}
static int checkReadWriteListArguments(int length, int offset, int bytes) {
if (offset < 0) return offset;
if (bytes < 0) return bytes;
if ((offset + bytes) > length) return offset + bytes;
return 0;
}
static int _checkedOpen(String name, bool writable) {
if (name is !String || writable is !bool) return 0;
return _open(name, writable);
}
static bool _checkedCreate(String name) {
if (name is !String) return false;
return _create(name);
}
static bool _checkedDelete(String name) {
if (name is !String) return false;
return _delete(name);
}
static int _checkedWriteString(int id, String string) {
static int checkedWriteString(int id, String string) {
if (string is !String) return -1;
return _writeString(id, string);
return writeString(id, string);
}
}
static String _checkedFullPath(String name) {
if (name is !String) return null;
return _fullPath(name);
}
// Class for encapsulating the native implementation of files.
class _File implements File {
// Constructor for file.
_File(String this._name)
: _scheduler = new _FileOperationScheduler(),
_asyncUsed = false;
void exists() {
_asyncUsed = true;
@ -558,7 +563,7 @@ class _File implements File {
if (_name is !String) {
throw new FileIOException('File name is not a string: $_name');
}
return _exists(_name);
return _FileUtils.exists(_name);
}
void create() {
@ -580,7 +585,7 @@ class _File implements File {
throw new FileIOException(
"Mixed use of synchronous and asynchronous API");
}
bool created = _checkedCreate(_name);
bool created = _FileUtils.checkedCreate(_name);
if (!created) {
throw new FileIOException("Cannot create file: $_name");
}
@ -605,7 +610,7 @@ class _File implements File {
throw new FileIOException(
"Mixed use of synchronous and asynchronous API");
}
bool deleted = _checkedDelete(_name);
bool deleted = _FileUtils.checkedDelete(_name);
if (!deleted) {
throw new FileIOException("Cannot delete file: $_name");
}
@ -613,11 +618,16 @@ class _File implements File {
void open([bool writable = false]) {
_asyncUsed = true;
var handler = (_openHandler != null) ? _openHandler : () => null;
var handleOpenResult = (result, ignored) {
if (result != 0) {
_id = result;
handler();
// If no open handler is present, close the file immediately to
// avoid leaking an open file descriptor.
var handler = _openHandler;
if (handler === null) {
handler = (file) => file.close();
}
var handleOpenResult = (id, ignored) {
if (id != 0) {
var randomAccessFile = new _RandomAccessFile(id, _name);
handler(randomAccessFile);
} else if (_errorHandler != null) {
_errorHandler("Cannot open file: $_name");
}
@ -631,12 +641,89 @@ class _File implements File {
throw new FileIOException(
"Mixed use of synchronous and asynchronous API");
}
_id = _checkedOpen(_name, writable);
if (_id == 0) {
var id = _FileUtils.checkedOpen(_name, writable);
if (id == 0) {
throw new FileIOException("Cannot open file: $_name");
}
return new _RandomAccessFile(id, _name);
}
void fullPath() {
_asyncUsed = true;
var handler = _fullPathHandler;
if (handler == null) handler = (path) => null;
var handleFullPathResult = (result, ignored) {
if (result != null) {
handler(result);
} else if (_errorHandler != null) {
_errorHandler("fullPath failed");
}
};
var operation = new _FullPathOperation(_name);
_scheduler.enqueue(operation, handleFullPathResult);
}
String fullPathSync() {
if (_asyncUsed) {
throw new FileIOException(
"Mixed use of synchronous and asynchronous API");
}
String result = _FileUtils.checkedFullPath(_name);
if (result == null) {
throw new FileIOException("fullPath failed");
}
return result;
}
InputStream openInputStream() => new _FileInputStream(this);
OutputStream openOutputStream() => new _FileOutputStream(this);
String get name() => _name;
void set existsHandler(void handler(bool exists)) {
_existsHandler = handler;
}
void set createHandler(void handler()) {
_createHandler = handler;
}
void set deleteHandler(void handler()) {
_deleteHandler = handler;
}
void set openHandler(void handler(RandomAccessFile file)) {
_openHandler = handler;
}
void set fullPathHandler(void handler(String)) {
_fullPathHandler = handler;
}
void set errorHandler(void handler(String error)) {
_errorHandler = handler;
}
String _name;
bool _asyncUsed;
_FileOperationScheduler _scheduler;
var _existsHandler;
var _createHandler;
var _deleteHandler;
var _openHandler;
var _fullPathHandler;
var _errorHandler;
}
class _RandomAccessFile implements RandomAccessFile {
_RandomAccessFile(int this._id, String this._name)
: _scheduler = new _FileOperationScheduler(),
_asyncUsed = false;
void close() {
_asyncUsed = true;
var handler = (_closeHandler != null) ? _closeHandler : () => null;
@ -657,7 +744,7 @@ class _File implements File {
throw new FileIOException(
"Mixed use of synchronous and asynchronous API");
}
var id = _close(_id);
var id = _FileUtils.close(_id);
if (id == -1) {
throw new FileIOException("Cannot close file: $_name");
}
@ -684,7 +771,7 @@ class _File implements File {
throw new FileIOException(
"Mixed use of synchronous and asynchronous API");
}
int result = _readByte(_id);
int result = _FileUtils.readByte(_id);
if (result == -1) {
throw new FileIOException("readByte failed");
}
@ -725,11 +812,12 @@ class _File implements File {
throw new FileIOException("Invalid arguments to readList");
}
if (bytes == 0) return 0;
int index = _checkReadWriteListArguments(buffer.length, offset, bytes);
int index =
_FileUtils.checkReadWriteListArguments(buffer.length, offset, bytes);
if (index != 0) {
throw new IndexOutOfRangeException(index);
}
int result = _readList(_id, buffer, offset, bytes);
int result = _FileUtils.readList(_id, buffer, offset, bytes);
if (result == -1) {
throw new FileIOException("readList failed");
}
@ -769,7 +857,7 @@ class _File implements File {
if (value is !int) {
throw new FileIOException("Invalid argument to writeByte");
}
int result = _writeByte(_id, value);
int result = _FileUtils.writeByte(_id, value);
if (result == -1) {
throw new FileIOException("writeByte failed");
}
@ -810,11 +898,12 @@ class _File implements File {
throw new FileIOException("Invalid arguments to writeList");
}
if (bytes == 0) return 0;
int index = _checkReadWriteListArguments(buffer.length, offset, bytes);
int index =
_FileUtils.checkReadWriteListArguments(buffer.length, offset, bytes);
if (index != 0) {
throw new IndexOutOfRangeException(index);
}
int result = _writeList(_id, buffer, offset, bytes);
int result = _FileUtils.writeList(_id, buffer, offset, bytes);
if (result == -1) {
throw new FileIOException("writeList failed");
}
@ -843,7 +932,7 @@ class _File implements File {
throw new FileIOException(
"Mixed use of synchronous and asynchronous API");
}
int result = _checkedWriteString(_id, string);
int result = _FileUtils.checkedWriteString(_id, string);
if (result == -1) {
throw new FileIOException("writeString failed");
}
@ -869,7 +958,7 @@ class _File implements File {
throw new FileIOException(
"Mixed use of synchronous and asynchronous API");
}
int result = _position(_id);
int result = _FileUtils.position(_id);
if (result == -1) {
throw new FileIOException("position failed");
}
@ -896,7 +985,7 @@ class _File implements File {
throw new FileIOException(
"Mixed use of synchronous and asynchronous API");
}
bool result = _setPosition(_id, position);
bool result = _FileUtils.setPosition(_id, position);
if (result == false) {
throw new FileIOException("setPosition failed");
}
@ -921,7 +1010,7 @@ class _File implements File {
throw new FileIOException(
"Mixed use of synchronous and asynchronous API");
}
bool result = _truncate(_id, length);
bool result = _FileUtils.truncate(_id, length);
if (result == false) {
throw new FileIOException("truncate failed");
}
@ -946,7 +1035,7 @@ class _File implements File {
throw new FileIOException(
"Mixed use of synchronous and asynchronous API");
}
int result = _length(_id);
int result = _FileUtils.length(_id);
if (result == -1) {
throw new FileIOException("length failed");
}
@ -972,65 +1061,16 @@ class _File implements File {
throw new FileIOException(
"Mixed use of synchronous and asynchronous API");
}
int result = _flush(_id);
int result = _FileUtils.flush(_id);
if (result == -1) {
throw new FileIOException("flush failed");
}
}
void fullPath() {
_asyncUsed = true;
var handler = _fullPathHandler;
if (handler == null) handler = (path) => null;
var handleFullPathResult = (result, ignored) {
if (result != null) {
handler(result);
} else if (_errorHandler != null) {
_errorHandler("fullPath failed");
}
};
var operation = new _FullPathOperation(_name);
_scheduler.enqueue(operation, handleFullPathResult);
}
String get name() => _name;
String fullPathSync() {
if (_asyncUsed) {
throw new FileIOException(
"Mixed use of synchronous and asynchronous API");
}
String result = _checkedFullPath(_name);
if (result == null) {
throw new FileIOException("fullPath failed");
}
return result;
}
InputStream openInputStream() {
return new _FileInputStream(this);
}
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 deleteHandler(void handler()) {
_deleteHandler = handler;
}
void set openHandler(void handler()) {
_openHandler = handler;
void set errorHandler(void handler(String error)) {
_errorHandler = handler;
}
void set closeHandler(void handler()) {
@ -1069,24 +1109,12 @@ class _File implements File {
_flushHandler = handler;
}
void set fullPathHandler(void handler(String)) {
_fullPathHandler = handler;
}
void set errorHandler(void handler(String error)) {
_errorHandler = handler;
}
String _name;
int _id;
bool _asyncUsed;
_FileOperationScheduler _scheduler;
var _existsHandler;
var _createHandler;
var _deleteHandler;
var _openHandler;
var _closeHandler;
var _readByteHandler;
var _readListHandler;
@ -1096,6 +1124,5 @@ class _File implements File {
var _truncateHandler;
var _lengthHandler;
var _flushHandler;
var _fullPathHandler;
var _errorHandler;
}

View file

@ -19,13 +19,11 @@ class DirectoryTest {
f.createSync();
directory.dirHandler = (dir) {
print(dir);
listedDir = true;
Expect.isTrue(dir.contains('subdir'));
};
directory.fileHandler = (f) {
print(f);
listedFile = true;
Expect.isTrue(f.contains('subdir'));
Expect.isTrue(f.contains('file.txt'));
@ -150,42 +148,41 @@ class DirectoryTest {
Expect.fail("testCreateTemp file.errorHandler called: $error");
};
file.createHandler = () {
file.openHandler = (RandomAccessFile openedFile) {
openedFile.writeList([65, 66, 67, 13], 0, 4);
openedFile.noPendingWriteHandler = () {
openedFile.length();
};
openedFile.lengthHandler = (int length) {
Expect.equals(4, length);
openedFile.close();
};
openedFile.closeHandler = () {
file.exists();
};
file.existsHandler = (bool exists) {
Expect.isTrue(exists);
// Try to delete the directory containing the file - should throw.
bool threw_exception = false;
try {
tempDirectory.deleteSync();
} catch (var e) {
Expect.isTrue(tempDirectory.existsSync());
threw_exception = true;
}
Expect.isTrue(threw_exception);
Expect.isTrue(tempDirectory.existsSync());
// Delete the file, and then delete the directory.
file.delete();
};
file.deleteHandler = () {
tempDirectory.deleteSync();
Expect.isFalse(tempDirectory.existsSync());
};
};
file.open(writable: true);
};
file.openHandler = () {
file.writeList([65, 66, 67, 13], 0, 4);
};
file.noPendingWriteHandler = () {
file.length();
};
file.lengthHandler = (int length) {
Expect.equals(4, length);
file.close();
};
file.closeHandler = () {
file.exists();
};
file.existsHandler = (bool exists) {
Expect.isTrue(exists);
// Try to delete the directory containing the file - should throw.
bool threw_exception = false;
try {
tempDirectory.deleteSync();
} catch (var e) {
Expect.isTrue(tempDirectory.existsSync());
threw_exception = true;
}
Expect.isTrue(threw_exception);
Expect.isTrue(tempDirectory.existsSync());
// Delete the file, and then delete the directory.
file.delete();
};
file.deleteHandler = () {
tempDirectory.deleteSync();
Expect.isFalse(tempDirectory.existsSync());
};
file.create();
};
tempDirectory.createTemp();

View file

@ -12,11 +12,9 @@ void testStringInputStreamSync() {
String fileName = getFilename("tests/standalone/src/readuntil_test.dat");
// File contains "Hello Dart\nwassup!\n"
File file = new File(fileName);
file.openSync();
StringInputStream x = new StringInputStream(file.openInputStream());
String line = x.readLine();
Expect.equals("Hello Dart", line);
file.closeSync();
line = x.readLine();
Expect.equals("wassup!", line);
}
@ -25,9 +23,7 @@ void testInputStreamAsync() {
String fileName = getFilename("tests/standalone/src/readuntil_test.dat");
// File contains "Hello Dart\nwassup!\n"
var expected = "Hello Dart\nwassup!\n".charCodes();
File file = new File(fileName);
file.openSync();
InputStream x = file.openInputStream();
InputStream x = (new File(fileName)).openInputStream();
var byteCount = 0;
x.dataHandler = () {
Expect.equals(expected[byteCount], x.read(1)[0]);
@ -43,7 +39,6 @@ void testStringInputStreamAsync1() {
String fileName = getFilename("tests/standalone/src/readuntil_test.dat");
// File contains "Hello Dart\nwassup!\n"
File file = new File(fileName);
file.openSync();
StringInputStream x = new StringInputStream(file.openInputStream());
var result = "";
x.dataHandler = () {
@ -59,7 +54,6 @@ void testStringInputStreamAsync2() {
String fileName = getFilename("tests/standalone/src/readuntil_test.dat");
// File contains "Hello Dart\nwassup!\n"
File file = new File(fileName);
file.openSync();
StringInputStream x = new StringInputStream(file.openInputStream());
int lineCount = 0;
x.lineHandler = () {
@ -79,12 +73,10 @@ void testChunkedInputStream() {
String fileName = getFilename("tests/standalone/src/readuntil_test.dat");
// File contains 19 bytes ("Hello Dart\nwassup!")
File file = new File(fileName);
file.openSync();
ChunkedInputStream x = new ChunkedInputStream(file.openInputStream());
x.chunkSize = 9;
List<int> chunk = x.read();
Expect.equals(9, chunk.length);
file.closeSync();
x.chunkSize = 5;
chunk = x.read();
Expect.equals(5, chunk.length);

View file

@ -60,29 +60,9 @@ class FileTest {
file.create();
}
static void testCloseNonOpened() {
var file = new File(0);
try {
file.closeSync();
Expect.fail('exception expected');
} catch (var e) {
Expect.isTrue(e is FileIOException);
Expect.isTrue(e.toString().contains('Cannot close file'));
}
file.errorHandler = (s) {
Expect.isTrue(s.contains('Cannot close file'));
};
file.closeHandler = () {
Expect.fail('close non-opened file');
};
file.close();
}
static void testReadListInvalidArgs(buffer, offset, length) {
String filename = getFilename("tests/vm/data/fixed_length_file");
var file = new File(filename);
file.openSync();
var file = (new File(filename)).openSync();
try {
file.readListSync(buffer, offset, length);
Expect.fail('exception expected');
@ -108,8 +88,7 @@ class FileTest {
static void testWriteByteInvalidArgs(value) {
String filename = getFilename("tests/vm/data/fixed_length_file");
var file = new File(filename + "_out");
file.openSync(true);
var file = (new File(filename + "_out")).openSync(true);
try {
file.writeByteSync(value);
Expect.fail('exception expected');
@ -135,8 +114,7 @@ class FileTest {
static void testWriteListInvalidArgs(buffer, offset, bytes) {
String filename = getFilename("tests/vm/data/fixed_length_file");
var file = new File(filename + "_out");
file.openSync(true);
var file = (new File(filename + "_out")).openSync(true);
try {
file.writeListSync(buffer, offset, bytes);
Expect.fail('exception expected');
@ -215,7 +193,6 @@ main() {
FileTest.testOpenInvalidArgs('name', 0);
FileTest.testExistsInvalidArgs(0);
FileTest.testCreateInvalidArgs(0);
FileTest.testCloseNonOpened();
FileTest.testReadListInvalidArgs(12, 0, 1);
FileTest.testReadListInvalidArgs(new List(10), '0', 1);
FileTest.testReadListInvalidArgs(new List(10), 0, '1');

View file

@ -95,7 +95,7 @@ class FileTest {
file.errorHandler = (s) {
Expect.fail("No errors expected");
};
file.openHandler = () {
file.openHandler = (RandomAccessFile file) {
List<int> buffer = new List<int>(10);
file.readListHandler = (bytes_read) {
Expect.equals(5, bytes_read);
@ -124,8 +124,7 @@ class FileTest {
static int testReadSync() {
// Read a file and check part of it's contents.
String filename = getFilename("bin/file_test.cc");
File file = new File(filename);
file.openSync();
RandomAccessFile file = (new File(filename)).openSync();
List<int> buffer = new List<int>(42);
int bytes_read = 0;
bytes_read = file.readListSync(buffer, 0, 12);
@ -155,11 +154,11 @@ class FileTest {
file.errorHandler = (s) {
Expect.fail("No errors expected");
};
file.openHandler = () {
file.openHandler = (RandomAccessFile openedFile) {
List<int> buffer1 = new List<int>(42);
file.readListHandler = (bytes_read) {
openedFile.readListHandler = (bytes_read) {
Expect.equals(42, bytes_read);
file.closeHandler = () {
openedFile.closeHandler = () {
// Write the contents of the file just read into another file.
String outFilename = tempDirectory.path + "/out_read_write";
file = new File(outFilename);
@ -172,19 +171,19 @@ class FileTest {
if (s[0] != '/' && s[0] != '\\' && s[1] != ':') {
Expect.fail("Not a full path");
}
file.openHandler = () {
file.noPendingWriteHandler = () {
file.closeHandler = () {
file.openHandler = (RandomAccessFile openedFile) {
openedFile.noPendingWriteHandler = () {
openedFile.closeHandler = () {
// Now read the contents of the file just written.
List<int> buffer2 = new List<int>(bytes_read);
file = new File(outFilename);
file.errorHandler = (s) {
Expect.fail("No errors expected");
};
file.openHandler = () {
file.readListHandler = (bytes_read) {
file.openHandler = (RandomAccessFile openedfile) {
openedFile.readListHandler = (bytes_read) {
Expect.equals(42, bytes_read);
file.closeHandler = () {
openedFile.closeHandler = () {
// Now compare the two buffers to check if they
// are identical.
Expect.equals(buffer1.length, buffer2.length);
@ -201,15 +200,15 @@ class FileTest {
};
file.delete();
};
file.close();
openedFile.close();
};
file.readList(buffer2, 0, 42);
openedFile.readList(buffer2, 0, 42);
};
file.open();
};
file.close();
openedFile.close();
};
file.writeList(buffer1, 0, bytes_read);
openedFile.writeList(buffer1, 0, bytes_read);
};
file.open(true);
};
@ -217,9 +216,9 @@ class FileTest {
};
file.create();
};
file.close();
openedFile.close();
};
file.readList(buffer1, 0, 42);
openedFile.readList(buffer1, 0, 42);
};
asyncTestStarted();
file.open();
@ -230,8 +229,7 @@ class FileTest {
static int testReadWriteSync() {
// Read a file.
String inFilename = getFilename("tests/vm/data/fixed_length_file");
File file = new File(inFilename);
file.openSync();
RandomAccessFile file = (new File(inFilename)).openSync();
List<int> buffer1 = new List<int>(42);
int bytes_read = 0;
int bytes_written = 0;
@ -240,42 +238,40 @@ class FileTest {
file.closeSync();
// Write the contents of the file just read into another file.
String outFilename = tempDirectory.path + "/out_read_write_sync";
file = new File(outFilename);
file.createSync();
String path = file.fullPathSync();
File outFile = new File(outFilename);
outFile.createSync();
String path = outFile.fullPathSync();
if (path[0] != '/' && path[0] != '\\' && path[1] != ':') {
Expect.fail("Not a full path");
}
Expect.isTrue(new File(path).existsSync());
file.openSync(true);
file.writeListSync(buffer1, 0, bytes_read);
file.closeSync();
RandomAccessFile openedFile = outFile.openSync(true);
openedFile.writeListSync(buffer1, 0, bytes_read);
openedFile.closeSync();
// Now read the contents of the file just written.
List<int> buffer2 = new List<int>(bytes_read);
file = new File(outFilename);
file.openSync();
bytes_read = file.readListSync(buffer2, 0, 42);
openedFile = (new File(outFilename)).openSync();
bytes_read = openedFile.readListSync(buffer2, 0, 42);
Expect.equals(42, bytes_read);
file.closeSync();
openedFile.closeSync();
// Now compare the two buffers to check if they are identical.
Expect.equals(buffer1.length, buffer2.length);
for (int i = 0; i < buffer1.length; i++) {
Expect.equals(buffer1[i], buffer2[i]);
}
// Delete the output file.
file.deleteSync();
Expect.isFalse(file.existsSync());
outFile.deleteSync();
Expect.isFalse(outFile.existsSync());
return 1;
}
// Test for file length functionality.
static int testLength() {
String filename = getFilename("tests/vm/data/fixed_length_file");
File input = new File(filename);
RandomAccessFile input = (new File(filename)).openSync();
input.errorHandler = (s) {
Expect.fail("No errors expected");
};
input.openSync();
input.lengthHandler = (length) {
Expect.equals(42, length);
input.close();
@ -286,8 +282,7 @@ class FileTest {
static int testLengthSync() {
String filename = getFilename("tests/vm/data/fixed_length_file");
File input = new File(filename);
input.openSync();
RandomAccessFile input = (new File(filename)).openSync();
Expect.equals(42, input.lengthSync());
input.closeSync();
return 1;
@ -296,11 +291,10 @@ class FileTest {
// Test for file position functionality.
static int testPosition() {
String filename = getFilename("tests/vm/data/fixed_length_file");
File input = new File(filename);
RandomAccessFile input = (new File(filename)).openSync();
input.errorHandler = (s) {
Expect.fail("No errors expected");
};
input.openSync();
input.positionHandler = (position) {
Expect.equals(0, position);
List<int> buffer = new List<int>(100);
@ -332,8 +326,7 @@ class FileTest {
static int testPositionSync() {
String filename = getFilename("tests/vm/data/fixed_length_file");
File input = new File(filename);
input.openSync();
RandomAccessFile input = (new File(filename)).openSync();
Expect.equals(0, input.positionSync());
List<int> buffer = new List<int>(100);
input.readListSync(buffer, 0, 12);
@ -352,14 +345,14 @@ class FileTest {
file.errorHandler = (error) {
Expect.fail("testTruncate: No errors expected");
};
file.openHandler = () {
file.noPendingWriteHandler = () {
file.lengthHandler = (length) {
file.openHandler = (RandomAccessFile openedFile) {
openedFile.noPendingWriteHandler = () {
openedFile.lengthHandler = (length) {
Expect.equals(10, length);
file.truncateHandler = () {
file.lengthHandler = (length) {
openedFile.truncateHandler = () {
openedFile.lengthHandler = (length) {
Expect.equals(5, length);
file.closeHandler = () {
openedFile.closeHandler = () {
file.deleteHandler = () {
file.existsHandler = (exists) {
Expect.isFalse(exists);
@ -369,15 +362,15 @@ class FileTest {
};
file.delete();
};
file.close();
openedFile.close();
};
file.length();
openedFile.length();
};
file.truncate(5);
openedFile.truncate(5);
};
file.length();
openedFile.length();
};
file.writeList(buffer, 0, 10);
openedFile.writeList(buffer, 0, 10);
};
asyncTestStarted();
file.open(true);
@ -387,12 +380,12 @@ class FileTest {
static int testTruncateSync() {
File file = new File(tempDirectory.path + "/out_truncate_sync");
List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65];
file.openSync(true);
file.writeListSync(buffer, 0, 10);
Expect.equals(10, file.lengthSync());
file.truncateSync(5);
Expect.equals(5, file.lengthSync());
file.closeSync();
RandomAccessFile openedFile = file.openSync(true);
openedFile.writeListSync(buffer, 0, 10);
Expect.equals(10, openedFile.lengthSync());
openedFile.truncateSync(5);
Expect.equals(5, openedFile.lengthSync());
openedFile.closeSync();
file.deleteSync();
Expect.isFalse(file.existsSync());
return 1;
@ -403,10 +396,10 @@ class FileTest {
bool exceptionCaught = false;
bool wrongExceptionCaught = false;
File input = new File(tempDirectory.path + "/out_close_exception");
input.openSync(true);
input.closeSync();
RandomAccessFile openedFile = input.openSync(true);
openedFile.closeSync();
try {
input.readByteSync();
openedFile.readByteSync();
} catch (FileIOException ex) {
exceptionCaught = true;
} catch (Exception ex) {
@ -416,7 +409,7 @@ class FileTest {
Expect.equals(true, !wrongExceptionCaught);
exceptionCaught = false;
try {
input.writeByteSync(1);
openedFile.writeByteSync(1);
} catch (FileIOException ex) {
exceptionCaught = true;
} catch (Exception ex) {
@ -426,7 +419,7 @@ class FileTest {
Expect.equals(true, !wrongExceptionCaught);
exceptionCaught = false;
try {
input.writeStringSync("Test");
openedFile.writeStringSync("Test");
} catch (FileIOException ex) {
exceptionCaught = true;
} catch (Exception ex) {
@ -437,7 +430,7 @@ class FileTest {
exceptionCaught = false;
try {
List<int> buffer = new List<int>(100);
input.readListSync(buffer, 0, 10);
openedFile.readListSync(buffer, 0, 10);
} catch (FileIOException ex) {
exceptionCaught = true;
} catch (Exception ex) {
@ -448,7 +441,7 @@ class FileTest {
exceptionCaught = false;
try {
List<int> buffer = new List<int>(100);
input.writeListSync(buffer, 0, 10);
openedFile.writeListSync(buffer, 0, 10);
} catch (FileIOException ex) {
exceptionCaught = true;
} catch (Exception ex) {
@ -458,7 +451,7 @@ class FileTest {
Expect.equals(true, !wrongExceptionCaught);
exceptionCaught = false;
try {
input.positionSync();
openedFile.positionSync();
} catch (FileIOException ex) {
exceptionCaught = true;
} catch (Exception ex) {
@ -468,7 +461,7 @@ class FileTest {
Expect.equals(true, !wrongExceptionCaught);
exceptionCaught = false;
try {
input.lengthSync();
openedFile.lengthSync();
} catch (FileIOException ex) {
exceptionCaught = true;
} catch (Exception ex) {
@ -478,7 +471,7 @@ class FileTest {
Expect.equals(true, !wrongExceptionCaught);
exceptionCaught = false;
try {
input.flushSync();
openedFile.flushSync();
} catch (FileIOException ex) {
exceptionCaught = true;
} catch (Exception ex) {
@ -530,10 +523,10 @@ class FileTest {
bool exceptionCaught = false;
bool wrongExceptionCaught = false;
File file = new File(tempDirectory.path + "/out_buffer_out_of_bounds");
file.openSync(true);
RandomAccessFile openedFile = file.openSync(true);
try {
List<int> buffer = new List<int>(10);
bool readDone = file.readListSync(buffer, 0, 12);
bool readDone = openedFile.readListSync(buffer, 0, 12);
} catch (IndexOutOfRangeException ex) {
exceptionCaught = true;
} catch (Exception ex) {
@ -544,7 +537,7 @@ class FileTest {
exceptionCaught = false;
try {
List<int> buffer = new List<int>(10);
bool readDone = file.readListSync(buffer, 6, 6);
bool readDone = openedFile.readListSync(buffer, 6, 6);
} catch (IndexOutOfRangeException ex) {
exceptionCaught = true;
} catch (Exception ex) {
@ -555,7 +548,7 @@ class FileTest {
exceptionCaught = false;
try {
List<int> buffer = new List<int>(10);
bool readDone = file.readListSync(buffer, -1, 1);
bool readDone = openedFile.readListSync(buffer, -1, 1);
} catch (IndexOutOfRangeException ex) {
exceptionCaught = true;
} catch (Exception ex) {
@ -566,7 +559,7 @@ class FileTest {
exceptionCaught = false;
try {
List<int> buffer = new List<int>(10);
bool readDone = file.readListSync(buffer, 0, -1);
bool readDone = openedFile.readListSync(buffer, 0, -1);
} catch (IndexOutOfRangeException ex) {
exceptionCaught = true;
} catch (Exception ex) {
@ -577,7 +570,7 @@ class FileTest {
exceptionCaught = false;
try {
List<int> buffer = new List<int>(10);
bool readDone = file.writeListSync(buffer, 0, 12);
bool readDone = openedFile.writeListSync(buffer, 0, 12);
} catch (IndexOutOfRangeException ex) {
exceptionCaught = true;
} catch (Exception ex) {
@ -588,7 +581,7 @@ class FileTest {
exceptionCaught = false;
try {
List<int> buffer = new List<int>(10);
bool readDone = file.writeListSync(buffer, 6, 6);
bool readDone = openedFile.writeListSync(buffer, 6, 6);
} catch (IndexOutOfRangeException ex) {
exceptionCaught = true;
} catch (Exception ex) {
@ -599,7 +592,7 @@ class FileTest {
exceptionCaught = false;
try {
List<int> buffer = new List<int>(10);
bool readDone = file.writeListSync(buffer, -1, 1);
bool readDone = openedFile.writeListSync(buffer, -1, 1);
} catch (IndexOutOfRangeException ex) {
exceptionCaught = true;
} catch (Exception ex) {
@ -610,7 +603,7 @@ class FileTest {
exceptionCaught = false;
try {
List<int> buffer = new List<int>(10);
bool readDone = file.writeListSync(buffer, 0, -1);
bool readDone = openedFile.writeListSync(buffer, 0, -1);
} catch (IndexOutOfRangeException ex) {
exceptionCaught = true;
} catch (Exception ex) {
@ -618,7 +611,7 @@ class FileTest {
}
Expect.equals(true, exceptionCaught);
Expect.equals(true, !wrongExceptionCaught);
file.closeSync();
openedFile.closeSync();
file.deleteSync();
return 1;
}

View file

@ -48,8 +48,7 @@ void ExtractTestsFromMultitest(String filename,
// Read the entire file into a byte buffer and transform it to a
// String. This will treat the file as ascii but the only parts
// we are interested in will be ascii in any case.
File file = new File(filename);
file.openSync();
RandomAccessFile file = (new File(filename)).openSync();
List chars = new List(file.lengthSync());
int offset = 0;
while (offset != chars.length) {
@ -166,32 +165,32 @@ WriteMultitestToFileAndQueueIt(Map<String, String> tests,
file.createHandler = () {
file.open(writable: true);
};
file.openHandler = () {
file.openHandler = (RandomAccessFile openedFile) {
openedFile.noPendingWriteHandler =() {
openedFile.close();
};
openedFile.closeHandler = () {
var outcome = outcomes[key];
bool enableFatalTypeErrors = (supportsFatalTypeErrors &&
outcome.contains('static type error'));
bool isNegative = (outcome.contains('compile-time error') ||
outcome.contains('runtime error') ||
enableFatalTypeErrors);
bool isNegativeIfChecked = outcome.contains('dynamic type error');
doTest(filename,
isNegative,
isNegativeIfChecked,
enableFatalTypeErrors);
WriteMultitestToFileAndQueueIt(tests,
outcomes,
supportsFatalTypeErrors,
currentKey,
basePath,
doTest,
done);
};
var bytes = tests[key].charCodes();
file.writeList(bytes, 0, bytes.length);
};
file.noPendingWriteHandler =() {
file.close();
};
file.closeHandler = () {
var outcome = outcomes[key];
bool enableFatalTypeErrors = (supportsFatalTypeErrors &&
outcome.contains('static type error'));
bool isNegative = (outcome.contains('compile-time error') ||
outcome.contains('runtime error') ||
enableFatalTypeErrors);
bool isNegativeIfChecked = outcome.contains('dynamic type error');
doTest(filename,
isNegative,
isNegativeIfChecked,
enableFatalTypeErrors);
WriteMultitestToFileAndQueueIt(tests,
outcomes,
supportsFatalTypeErrors,
currentKey,
basePath,
doTest,
done);
openedFile.writeList(bytes, 0, bytes.length);
};
file.create();
}

View file

@ -323,8 +323,7 @@ class StandardTestSuite implements TestSuite {
// Read the entire file into a byte buffer and transform it to a
// String. This will treat the file as ascii but the only parts
// we are interested in will be ascii in any case.
File file = new File(filename);
file.openSync();
RandomAccessFile file = (new File(filename)).openSync();
List chars = new List(file.lengthSync());
var offset = 0;
while (offset != chars.length) {

View file

@ -30,8 +30,7 @@ main() {
}
String readFile(String path) {
final file = new File(path);
file.openSync();
final file = (new File(path)).openSync();
final length = file.lengthSync();
final buffer = new List<int>(length);
final bytes = file.readListSync(buffer, 0, length);