/
file.dart
302 lines (258 loc) · 8.25 KB
/
file.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// 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.
/**
* FileMode describes the modes in which a file can be opened.
*/
class FileMode {
static final READ = const FileMode(0);
static final WRITE = const FileMode(1);
static final APPEND = const FileMode(2);
const FileMode(int this.mode);
final int mode;
}
interface File default _File {
/**
* Create a File object.
*/
File(String name);
/**
* Check if the file exists. The [existsHandler] is called with the
* result when the operation completes.
*/
void exists();
/**
* Synchronously check if the file exists.
*/
bool existsSync();
/**
* Create the file. The [createHandler] is called when the file has
* been created. The errorHandler is called if the file cannot be
* created. Existing files are left untouched by create. Calling
* create on an existing file might fail if there are restrictive
* permissions on the file.
*/
void create();
/**
* Synchronously create the file. Existing files are left untouched
* by create. Calling create on an existing file might fail if there
* are restrictive permissions on the file.
*/
void createSync();
/**
* Delete the file. The [deleteHandler] is called when the file has
* been successfully deleted. The [errorHandler] is called if the
* file cannot be deleted.
*/
void delete();
/**
* Synchronously delete the file.
*/
void deleteSync();
/**
* Open the file for random access operations. When the file is
* opened the [openHandler] is called with the resulting
* RandomAccessFile. RandomAccessFiles must be closed using the
* [close] method. If the file cannot be opened the [errorHandler]
* is called.
*
* Files can be opened in three modes:
*
* FileMode.READ: open the file for reading. If the file does not
* exist the [errorHandler] is called.
*
* FileMode.WRITE: open the file for both reading and writing and
* truncate the file to length zero. If the file does not exist the
* file is created.
*
* FileMode.APPEND: same as FileMode.WRITE except that the file is
* not truncated.
*
* By default mode is FileMode.READ.
*/
void open([FileMode mode]);
/**
* 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.
*
* Files can be opened in three modes:
*
* FileMode.READ: open the file for reading. If the file does not
* exist the [errorHandler] is called.
*
* FileMode.WRITE: open the file for both reading and writing and
* truncate the file to length zero. If the file does not exist the
* file is created.
*
* FileMode.APPEND: same as FileMode.WRITE except that the file is
* not truncated.
*
* By default mode is FileMode.READ.
*/
RandomAccessFile openSync([FileMode mode]);
/**
* Get the canonical full path corresponding to the file name. The
* [fullPathHandler] is called with the result when the fullPath
* operation completes.
*/
void 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.
*/
InputStream 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.
*/
OutputStream 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.
*/
void close();
/**
* Synchronously close the file.
*/
void closeSync();
/**
* Read a byte from the file. When the byte has been read the
* [readByteHandler] is called with the value.
*/
void readByte();
/**
* Synchronously read a single byte from the file.
*/
int readByteSync();
/**
* Read a List<int> from the file. When the list has been read the
* [readListHandler] is called with an integer indicating how much
* was read.
*/
void readList(List<int> buffer, int offset, int bytes);
/**
* Synchronously read a List<int> from the file. Returns the number
* of bytes read.
*/
int readListSync(List<int> buffer, int offset, int bytes);
/**
* Write a single byte to the file. If the byte cannot be written
* the [errorHandler] is called. When all pending write operations
* have finished the [noPendingWriteHandler] is called.
*/
void writeByte(int value);
/**
* Synchronously write a single byte to the file. Returns the
* number of bytes successfully written.
*/
int writeByteSync(int value);
/**
* Write a List<int> to the file. If the list cannot be written the
* [errorHandler] is called. When all pending write operations have
* finished the [noPendingWriteHandler] is called.
*/
void writeList(List<int> buffer, int offset, int bytes);
/**
* Synchronously write a List<int> to the file. Returns the number
* of bytes successfully written.
*/
int writeListSync(List<int> buffer, int offset, int bytes);
/**
* Write a string to the file. If the string cannot be written the
* [errorHandler] is called. When all pending write operations have
* finished the [noPendingWriteHandler] is called.
*/
// TODO(ager): writeString should take an encoding.
void writeString(String string);
/**
* Synchronously write a single string to the file. Returns the number
* of characters successfully written.
*/
// TODO(ager): writeStringSync should take an encoding.
int writeStringSync(String string);
/**
* Get the current byte position in the file. When the operation
* completes the [positionHandler] is called with the position.
*/
void position();
/**
* Synchronously get the current byte position in the file.
*/
int positionSync();
/**
* Set the byte position in the file. When the operation completes
* the [setPositionHandler] is called.
*/
void setPosition(int position);
/**
* Synchronously set the byte position in the file.
*/
void setPositionSync(int position);
/**
* Truncate (or extend) the file to [length] bytes. When the
* operation completes successfully the [truncateHandler] is called.
*/
void truncate(int length);
/**
* Synchronously truncate (or extend) the file to [length] bytes.
*/
void truncateSync(int length);
/**
* Get the length of the file. When the operation completes the
* [lengthHandler] is called with the length.
*/
void length();
/**
* Synchronously get the length of the file.
*/
int lengthSync();
/**
* Flush the contents of the file to disk. The [flushHandler] is
* called when the flush operation completes.
*/
void flush();
/**
* Synchronously flush the contents of the file to disk.
*/
void flushSync();
/**
* Get the name of the file.
*/
String get name();
void set closeHandler(void handler());
void set readByteHandler(void handler(int byte));
void set readListHandler(void handler(int read));
void set noPendingWriteHandler(void handler());
void set positionHandler(void handler(int position));
void set setPositionHandler(void handler());
void set truncateHandler(void handler());
void set lengthHandler(void handler(int length));
void set flushHandler(void handler());
void set errorHandler(void handler(String error));
}
class FileIOException implements Exception {
const FileIOException([String this.message = ""]);
String toString() => "FileIOException: $message";
final String message;
}