OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 /** | 6 /** |
7 * FileMode describes the modes in which a file can be opened. | 7 * FileMode describes the modes in which a file can be opened. |
8 */ | 8 */ |
9 class FileMode { | 9 class FileMode { |
10 static final READ = const FileMode(0); | 10 static final READ = const FileMode(0); |
(...skipping 11 matching lines...) Expand all Loading... |
22 * streams using [openInputStream] and [openOutputStream] or open the | 22 * streams using [openInputStream] and [openOutputStream] or open the |
23 * file for random access operations using [open]. | 23 * file for random access operations using [open]. |
24 */ | 24 */ |
25 interface File default _File { | 25 interface File default _File { |
26 /** | 26 /** |
27 * Create a File object. | 27 * Create a File object. |
28 */ | 28 */ |
29 File(String name); | 29 File(String name); |
30 | 30 |
31 /** | 31 /** |
32 * Check if the file exists. The [existsHandler] is called with the | 32 * Check if the file exists. The callback is called with the result |
33 * result when the operation completes. | 33 * when the operation completes. The [onError] function registered |
| 34 * on the file object is called if an error occurs. |
34 */ | 35 */ |
35 void exists(); | 36 void exists(void callback(bool exists)); |
36 | 37 |
37 /** | 38 /** |
38 * Synchronously check if the file exists. | 39 * Synchronously check if the file exists. |
39 */ | 40 */ |
40 bool existsSync(); | 41 bool existsSync(); |
41 | 42 |
42 /** | 43 /** |
43 * Create the file. The [createHandler] is called when the file has | 44 * Create the file. The callback is called when the file has been |
44 * been created. The [errorHandler] is called if the file cannot be | 45 * created. The [onError] function registered on the file object is |
45 * created. Existing files are left untouched by create. Calling | 46 * called if the file cannot be created. Existing files are left |
46 * create on an existing file might fail if there are restrictive | 47 * untouched by create. Calling create on an existing file might |
47 * permissions on the file. | 48 * fail if there are restrictive permissions on the file. |
48 */ | 49 */ |
49 void create(); | 50 void create(void callback()); |
50 | 51 |
51 /** | 52 /** |
52 * Synchronously create the file. Existing files are left untouched | 53 * Synchronously create the file. Existing files are left untouched |
53 * by create. Calling create on an existing file might fail if there | 54 * by create. Calling create on an existing file might fail if there |
54 * are restrictive permissions on the file. | 55 * are restrictive permissions on the file. |
55 */ | 56 */ |
56 void createSync(); | 57 void createSync(); |
57 | 58 |
58 /** | 59 /** |
59 * Delete the file. The [deleteHandler] is called when the file has | 60 * Delete the file. The callback is called when the file has been |
60 * been successfully deleted. The [errorHandler] is called if the | 61 * successfully deleted. The [onError] function registered on the |
61 * file cannot be deleted. | 62 * file object is called if the file cannot be deleted. |
62 */ | 63 */ |
63 void delete(); | 64 void delete(void callback()); |
64 | 65 |
65 /** | 66 /** |
66 * Synchronously delete the file. | 67 * Synchronously delete the file. |
67 */ | 68 */ |
68 void deleteSync(); | 69 void deleteSync(); |
69 | 70 |
70 /** | 71 /** |
71 * Get a Directory object for the directory containing this file. If | 72 * Get a Directory object for the directory containing this |
72 * the file does not exist the [errorHandler] is called. When the | 73 * file. When the operation completes the callback is called with |
73 * operation completes the [directoryHandler] is called with the | 74 * the result. If the file does not exist the [onError] function |
74 * result. | 75 * registered on the file object is called. |
75 */ | 76 */ |
76 void directory(); | 77 void directory(void callback(Directory dir)); |
77 | 78 |
78 /** | 79 /** |
79 * Synchronously get a Directory object for the directory containing | 80 * Synchronously get a Directory object for the directory containing |
80 * this file. | 81 * this file. |
81 */ | 82 */ |
82 Directory directorySync(); | 83 Directory directorySync(); |
83 | 84 |
84 /** | 85 /** |
85 * Open the file for random access operations. When the file is | 86 * Open the file for random access operations. When the file is |
86 * opened the [openHandler] is called with the resulting | 87 * opened the callback is called with the resulting |
87 * RandomAccessFile. RandomAccessFiles must be closed using the | 88 * RandomAccessFile. RandomAccessFiles must be closed using the |
88 * [close] method. If the file cannot be opened the [errorHandler] | 89 * [close] method. If the file cannot be opened [onError] is called. |
89 * is called. | |
90 * | 90 * |
91 * Files can be opened in three modes: | 91 * Files can be opened in three modes: |
92 * | 92 * |
93 * FileMode.READ: open the file for reading. If the file does not | 93 * FileMode.READ: open the file for reading. If the file does not |
94 * exist the [errorHandler] is called. | 94 * exist [onError] is called. |
95 * | 95 * |
96 * FileMode.WRITE: open the file for both reading and writing and | 96 * FileMode.WRITE: open the file for both reading and writing and |
97 * truncate the file to length zero. If the file does not exist the | 97 * truncate the file to length zero. If the file does not exist the |
98 * file is created. | 98 * file is created. |
99 * | 99 * |
100 * FileMode.APPEND: same as FileMode.WRITE except that the file is | 100 * FileMode.APPEND: same as FileMode.WRITE except that the file is |
101 * not truncated. | 101 * not truncated. |
102 * | |
103 * By default mode is FileMode.READ. | |
104 */ | 102 */ |
105 void open([FileMode mode]); | 103 void open(FileMode mode, void callback(RandomAccessFile opened)); |
106 | 104 |
107 /** | 105 /** |
108 * Synchronously open the file for random access operations. The | 106 * Synchronously open the file for random access operations. The |
109 * result is a RandomAccessFile on which random access operations | 107 * result is a RandomAccessFile on which random access operations |
110 * can be performed. Opened RandomAccessFiles must be closed using | 108 * can be performed. Opened RandomAccessFiles must be closed using |
111 * the [close] method. | 109 * the [close] method. |
112 * | 110 * |
113 * See [open] for information on the [:mode:] argument. | 111 * See [open] for information on the [:mode:] argument. |
114 */ | 112 */ |
115 RandomAccessFile openSync([FileMode mode]); | 113 RandomAccessFile openSync(FileMode mode); |
116 | 114 |
117 /** | 115 /** |
118 * Get the canonical full path corresponding to the file name. The | 116 * Get the canonical full path corresponding to the file name. The |
119 * [fullPathHandler] is called with the result when the fullPath | 117 * callback is called with the result when the |
120 * operation completes. | 118 * fullPath operation completes. If the operation fails the |
| 119 * [onError] function registered on the file object is called. |
121 */ | 120 */ |
122 void fullPath(); | 121 void fullPath(void callback(String path)); |
123 | 122 |
124 /** | 123 /** |
125 * Synchronously get the canonical full path corresponding to the file name. | 124 * Synchronously get the canonical full path corresponding to the file name. |
126 */ | 125 */ |
127 String fullPathSync(); | 126 String fullPathSync(); |
128 | 127 |
129 /** | 128 /** |
130 * Create a new independent input stream for the file. The file | 129 * Create a new independent input stream for the file. The file |
131 * input stream must be closed when no longer used to free up system | 130 * input stream must be closed when no longer used to free up system |
132 * resources. | 131 * resources. |
(...skipping 12 matching lines...) Expand all Loading... |
145 * | 144 * |
146 * FileMode.APPEND: create the stream and set the position to the end of | 145 * FileMode.APPEND: create the stream and set the position to the end of |
147 * the underlying file. | 146 * the underlying file. |
148 * | 147 * |
149 * By default the mode is FileMode.WRITE. | 148 * By default the mode is FileMode.WRITE. |
150 */ | 149 */ |
151 OutputStream openOutputStream([FileMode mode]); | 150 OutputStream openOutputStream([FileMode mode]); |
152 | 151 |
153 /** | 152 /** |
154 * Read the entire file contents as a list of bytes. When the | 153 * Read the entire file contents as a list of bytes. When the |
155 * operation completes the [readAsBytesHandler] is called. | 154 * operation completes the callback is called. The [onError] |
156 * The [errorHandler] is called if the operation fails. | 155 * function registered on the file object is called if the operation |
| 156 * fails. |
157 */ | 157 */ |
158 void readAsBytes(); | 158 void readAsBytes(void callback(List<int> bytes)); |
159 | 159 |
160 /** | 160 /** |
161 * Synchronously read the entire file contents as a list of bytes. | 161 * Synchronously read the entire file contents as a list of bytes. |
162 */ | 162 */ |
163 List<int> readAsBytesSync(); | 163 List<int> readAsBytesSync(); |
164 | 164 |
165 /** | 165 /** |
166 * Read the entire file contents as text using the given [encoding] | 166 * Read the entire file contents as text using the given [encoding] |
167 * ('UTF-8', 'ISO-8859-1', 'ASCII'). By default the encoding is | 167 * ('UTF-8', 'ISO-8859-1', 'ASCII'). |
168 * 'UTF-8'. | |
169 * | 168 * |
170 * When the operation completes the [readAsTextHandler] is called | 169 * When the operation completes the callback is called. The |
171 * with the resulting string. The [errorHandler] is called if the | 170 * [onError] function registered on the file object is called if the |
172 * operation fails. | 171 * operation fails. |
173 */ | 172 */ |
174 void readAsText([String encoding]); | 173 void readAsText(String encoding, void callback(String text)); |
175 | 174 |
176 /** | 175 /** |
177 * Synchronously read the entire file contents as text using the | 176 * Synchronously read the entire file contents as text using the |
178 * given [encoding] ('UTF-8', 'ISO-8859-1', 'ASCII'). By default the | 177 * given [encoding] ('UTF-8', 'ISO-8859-1', 'ASCII'). By default the |
179 * encoding is 'UTF-8'. | 178 * encoding is 'UTF-8'. |
180 */ | 179 */ |
181 String readAsTextSync([String encoding]); | 180 String readAsTextSync([String encoding]); |
182 | 181 |
183 /** | 182 /** |
184 * Read the entire file contents as lines of text using the give | 183 * Read the entire file contents as lines of text using the give |
185 * [encoding] ('UTF-8', 'ISO-8859-1', 'ASCII'). By default the | 184 * [encoding] ('UTF-8', 'ISO-8859-1', 'ASCII'). |
186 * encoding is 'UTF-8'. | |
187 * | 185 * |
188 * When the operation completes the [readAsLinesHandler] is called | 186 * When the operation completes the callback is called. The |
189 * with the resulting string. The [errorHandler] is called if the | 187 * [onError] function registered on the file object is called if the |
190 * operation fails. | 188 * operation fails. |
191 */ | 189 */ |
192 void readAsLines(); | 190 void readAsLines(String encoding, void callback(List<String> lines)); |
193 | 191 |
194 /** | 192 /** |
195 * Synchronously read the entire file contents as lines of text | 193 * Synchronously read the entire file contents as lines of text |
196 * using the given [encoding] ('UTF-8', 'ISO-8859-1', 'ASCII'). By | 194 * using the given [encoding] ('UTF-8', 'ISO-8859-1', 'ASCII'). By |
197 * default the encoding is 'UTF-8'. | 195 * default the encoding is 'UTF-8'. |
198 */ | 196 */ |
199 List<String> readAsLinesSync([String encoding]); | 197 List<String> readAsLinesSync([String encoding]); |
200 | 198 |
201 /** | 199 /** |
202 * Get the name of the file. | 200 * Get the name of the file. |
203 */ | 201 */ |
204 String get name(); | 202 String get name(); |
205 | 203 |
206 /** | 204 /** |
207 * Sets the handler that gets called when an [exists] operation | |
208 * completes. | |
209 */ | |
210 void set existsHandler(void handler(bool exists)); | |
211 | |
212 /** | |
213 * Sets the handler that gets called when a [create] operation | |
214 * completes. | |
215 */ | |
216 void set createHandler(void handler()); | |
217 | |
218 /** | |
219 * Sets the handler that gets called when a [delete] operation | |
220 * completes. | |
221 */ | |
222 void set deleteHandler(void handler()); | |
223 | |
224 /** | |
225 * Sets the handler that gets called when a [directory] operation | |
226 * completes. | |
227 */ | |
228 void set directoryHandler(void handler(Directory directory)); | |
229 | |
230 /** | |
231 * Sets the handler that gets called when an [open] operation | |
232 * completes. | |
233 */ | |
234 void set openHandler(void handler(RandomAccessFile openedFile)); | |
235 | |
236 /** | |
237 * Set the handler that gets called when a [readAsBytes] operation | |
238 * completes. | |
239 */ | |
240 void set readAsBytesHandler(void handler(List<int> bytes)); | |
241 | |
242 /** | |
243 * Set the handler that gets called when a [readAsText] operation | |
244 * completes. | |
245 */ | |
246 void set readAsTextHandler(void handler(String text)); | |
247 | |
248 /** | |
249 * Set the handler that gets called when a [readAsLines] operation | |
250 * completes. | |
251 */ | |
252 void set readAsLinesHandler(void handler(List<String> lines)); | |
253 | |
254 /** | |
255 * Sets the handler that gets called when a [fullPath] operation | |
256 * completes. | |
257 */ | |
258 void set fullPathHandler(void handler(String path)); | |
259 | |
260 /** | |
261 * Sets the handler that gets called when errors occur during | 205 * Sets the handler that gets called when errors occur during |
262 * operations on this file. | 206 * operations on this file. |
263 */ | 207 */ |
264 void set errorHandler(void handler(String error)); | 208 void set onError(void handler(String error)); |
265 } | 209 } |
266 | 210 |
267 | 211 |
268 /** | 212 /** |
269 * [RandomAccessFile] provides random access to the data in a | 213 * [RandomAccessFile] provides random access to the data in a |
270 * file. [RandomAccessFile] objects are obtained by calling the | 214 * file. [RandomAccessFile] objects are obtained by calling the |
271 * [:open:] method on a [File] object. | 215 * [:open:] method on a [File] object. |
272 */ | 216 */ |
273 interface RandomAccessFile { | 217 interface RandomAccessFile { |
274 /** | 218 /** |
275 * Close the file. When the file is closed the [closeHandler] is | 219 * Close the file. When the file is closed the callback is called. |
276 * called. | |
277 */ | 220 */ |
278 void close(); | 221 void close(void callback()); |
279 | 222 |
280 /** | 223 /** |
281 * Synchronously close the file. | 224 * Synchronously close the file. |
282 */ | 225 */ |
283 void closeSync(); | 226 void closeSync(); |
284 | 227 |
285 /** | 228 /** |
286 * Read a byte from the file. When the byte has been read the | 229 * Read a byte from the file. When the byte has been read the |
287 * [readByteHandler] is called with the value. | 230 * callback is called with the value. |
288 */ | 231 */ |
289 void readByte(); | 232 void readByte(void callback(int byte)); |
290 | 233 |
291 /** | 234 /** |
292 * Synchronously read a single byte from the file. | 235 * Synchronously read a single byte from the file. |
293 */ | 236 */ |
294 int readByteSync(); | 237 int readByteSync(); |
295 | 238 |
296 /** | 239 /** |
297 * Read a List<int> from the file. When the list has been read the | 240 * Read a List<int> from the file. When the list has been read the |
298 * [readListHandler] is called with an integer indicating how much | 241 * callback is called with an integer indicating how much was read. |
299 * was read. | |
300 */ | 242 */ |
301 void readList(List<int> buffer, int offset, int bytes); | 243 void readList(List<int> buffer, int offset, int bytes, |
| 244 void callback(int read)); |
302 | 245 |
303 /** | 246 /** |
304 * Synchronously read a List<int> from the file. Returns the number | 247 * Synchronously read a List<int> from the file. Returns the number |
305 * of bytes read. | 248 * of bytes read. |
306 */ | 249 */ |
307 int readListSync(List<int> buffer, int offset, int bytes); | 250 int readListSync(List<int> buffer, int offset, int bytes); |
308 | 251 |
309 /** | 252 /** |
310 * Write a single byte to the file. If the byte cannot be written | 253 * Write a single byte to the file. If the byte cannot be written |
311 * the [errorHandler] is called. When all pending write operations | 254 * [onError] is called. When all pending write operations have |
312 * have finished the [noPendingWriteHandler] is called. | 255 * finished [onNoPendingWrites] is called. |
313 */ | 256 */ |
314 void writeByte(int value); | 257 void writeByte(int value); |
315 | 258 |
316 /** | 259 /** |
317 * Synchronously write a single byte to the file. Returns the | 260 * Synchronously write a single byte to the file. Returns the |
318 * number of bytes successfully written. | 261 * number of bytes successfully written. |
319 */ | 262 */ |
320 int writeByteSync(int value); | 263 int writeByteSync(int value); |
321 | 264 |
322 /** | 265 /** |
323 * Write a List<int> to the file. If the list cannot be written the | 266 * Write a List<int> to the file. If the list cannot be written the |
324 * [errorHandler] is called. When all pending write operations have | 267 * [onError] is called. When all pending write operations have |
325 * finished the [noPendingWriteHandler] is called. | 268 * finished [onNoPendingWrites] is called. |
326 */ | 269 */ |
327 void writeList(List<int> buffer, int offset, int bytes); | 270 void writeList(List<int> buffer, int offset, int bytes); |
328 | 271 |
329 /** | 272 /** |
330 * Synchronously write a List<int> to the file. Returns the number | 273 * Synchronously write a List<int> to the file. Returns the number |
331 * of bytes successfully written. | 274 * of bytes successfully written. |
332 */ | 275 */ |
333 int writeListSync(List<int> buffer, int offset, int bytes); | 276 int writeListSync(List<int> buffer, int offset, int bytes); |
334 | 277 |
335 /** | 278 /** |
336 * Write a string to the file. If the string cannot be written the | 279 * Write a string to the file. If the string cannot be written the |
337 * [errorHandler] is called. When all pending write operations have | 280 * [onError] is called. When all pending write operations have |
338 * finished the [noPendingWriteHandler] is called. | 281 * finished [onNoPendingWrites] is called. |
339 */ | 282 */ |
340 // TODO(ager): writeString should take an encoding. | 283 // TODO(ager): writeString should take an encoding. |
341 void writeString(String string); | 284 void writeString(String string); |
342 | 285 |
343 /** | 286 /** |
344 * Synchronously write a single string to the file. Returns the number | 287 * Synchronously write a single string to the file. Returns the number |
345 * of characters successfully written. | 288 * of characters successfully written. |
346 */ | 289 */ |
347 // TODO(ager): writeStringSync should take an encoding. | 290 // TODO(ager): writeStringSync should take an encoding. |
348 int writeStringSync(String string); | 291 int writeStringSync(String string); |
349 | 292 |
350 /** | 293 /** |
351 * Get the current byte position in the file. When the operation | 294 * Get the current byte position in the file. When the operation |
352 * completes the [positionHandler] is called with the position. | 295 * completes the callback is called with the position. |
353 */ | 296 */ |
354 void position(); | 297 void position(void callback(int position)); |
355 | 298 |
356 /** | 299 /** |
357 * Synchronously get the current byte position in the file. | 300 * Synchronously get the current byte position in the file. |
358 */ | 301 */ |
359 int positionSync(); | 302 int positionSync(); |
360 | 303 |
361 /** | 304 /** |
362 * Set the byte position in the file. When the operation completes | 305 * Set the byte position in the file. When the operation completes |
363 * the [setPositionHandler] is called. | 306 * the callback is called. |
364 */ | 307 */ |
365 void setPosition(int position); | 308 void setPosition(int position, void callback()); |
366 | 309 |
367 /** | 310 /** |
368 * Synchronously set the byte position in the file. | 311 * Synchronously set the byte position in the file. |
369 */ | 312 */ |
370 void setPositionSync(int position); | 313 void setPositionSync(int position); |
371 | 314 |
372 /** | 315 /** |
373 * Truncate (or extend) the file to [length] bytes. When the | 316 * Truncate (or extend) the file to [length] bytes. When the |
374 * operation completes successfully the [truncateHandler] is called. | 317 * operation completes successfully the callback is called. |
375 */ | 318 */ |
376 void truncate(int length); | 319 void truncate(int length, void callback()); |
377 | 320 |
378 /** | 321 /** |
379 * Synchronously truncate (or extend) the file to [length] bytes. | 322 * Synchronously truncate (or extend) the file to [length] bytes. |
380 */ | 323 */ |
381 void truncateSync(int length); | 324 void truncateSync(int length); |
382 | 325 |
383 /** | 326 /** |
384 * Get the length of the file. When the operation completes the | 327 * Get the length of the file. When the operation completes the |
385 * [lengthHandler] is called with the length. | 328 * callback is called with the length. |
386 */ | 329 */ |
387 void length(); | 330 void length(void callback(int length)); |
388 | 331 |
389 /** | 332 /** |
390 * Synchronously get the length of the file. | 333 * Synchronously get the length of the file. |
391 */ | 334 */ |
392 int lengthSync(); | 335 int lengthSync(); |
393 | 336 |
394 /** | 337 /** |
395 * Flush the contents of the file to disk. The [flushHandler] is | 338 * Flush the contents of the file to disk. The callback is |
396 * called when the flush operation completes. | 339 * called when the flush operation completes. |
397 */ | 340 */ |
398 void flush(); | 341 void flush(void callback()); |
399 | 342 |
400 /** | 343 /** |
401 * Synchronously flush the contents of the file to disk. | 344 * Synchronously flush the contents of the file to disk. |
402 */ | 345 */ |
403 void flushSync(); | 346 void flushSync(); |
404 | 347 |
405 /** | 348 /** |
406 * Get the name of the file. | 349 * Get the name of the file. |
407 */ | 350 */ |
408 String get name(); | 351 String get name(); |
409 | 352 |
410 /** | 353 /** |
411 * Sets the handler that gets called when a [close] operation | |
412 * completes. | |
413 */ | |
414 void set closeHandler(void handler()); | |
415 | |
416 /** | |
417 * Sets the handler that gets called when a [readByte] operation | |
418 * completes. | |
419 */ | |
420 void set readByteHandler(void handler(int byte)); | |
421 | |
422 /** | |
423 * Sets the handler that gets called when a [readList] operation | |
424 * completes. | |
425 */ | |
426 void set readListHandler(void handler(int read)); | |
427 | |
428 /** | |
429 * Sets the handler that gets called when there are no more write | 354 * Sets the handler that gets called when there are no more write |
430 * operations pending for this file. | 355 * operations pending for this file. |
431 */ | 356 */ |
432 void set noPendingWriteHandler(void handler()); | 357 void set onNoPendingWrites(void handler()); |
433 | |
434 /** | |
435 * Sets the handler that gets called when a [position] operation | |
436 * completes. | |
437 */ | |
438 void set positionHandler(void handler(int position)); | |
439 | |
440 /** | |
441 * Sets the handler that gets called when a [setPosition] operation | |
442 * completes. | |
443 */ | |
444 void set setPositionHandler(void handler()); | |
445 | |
446 /** | |
447 * Sets the handler that gets called when a [truncate] operation | |
448 * completes. | |
449 */ | |
450 void set truncateHandler(void handler()); | |
451 | |
452 /** | |
453 * Sets the handler that gets called when a [length] operation | |
454 * completes. | |
455 */ | |
456 void set lengthHandler(void handler(int length)); | |
457 | |
458 /** | |
459 * Sets the handler that gets called when a [flush] operation | |
460 * completes. | |
461 */ | |
462 void set flushHandler(void handler()); | |
463 | 358 |
464 /** | 359 /** |
465 * Sets the handler that gets called when errors occur when | 360 * Sets the handler that gets called when errors occur when |
466 * operating on this file. | 361 * operating on this file. |
467 */ | 362 */ |
468 void set errorHandler(void handler(String error)); | 363 void set onError(void handler(String error)); |
469 } | 364 } |
470 | 365 |
471 | 366 |
472 class FileIOException implements Exception { | 367 class FileIOException implements Exception { |
473 const FileIOException([String this.message = ""]); | 368 const FileIOException([String this.message = ""]); |
474 String toString() => "FileIOException: $message"; | 369 String toString() => "FileIOException: $message"; |
475 final String message; | 370 final String message; |
476 } | 371 } |
OLD | NEW |