OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class _FileInputStream implements FileInputStream { | 5 class _FileInputStream implements FileInputStream { |
6 _FileInputStream(File file) { | 6 _FileInputStream(File file) { |
7 _file = new File(file.name); | 7 _file = file.openSync(); |
8 _file.openSync(); | |
9 _length = _file.lengthSync(); | 8 _length = _file.lengthSync(); |
10 _checkScheduleCallbacks(); | 9 _checkScheduleCallbacks(); |
11 } | 10 } |
12 | 11 |
13 List<int> read([int len]) { | 12 List<int> read([int len]) { |
14 if (_closed) return null; | 13 if (_closed) return null; |
15 int bytesToRead = available(); | 14 int bytesToRead = available(); |
16 if (bytesToRead == 0) { | 15 if (bytesToRead == 0) { |
17 _checkScheduleCallbacks(); | 16 _checkScheduleCallbacks(); |
18 return null; | 17 return null; |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 } | 97 } |
99 } else if (!_eof) { | 98 } else if (!_eof) { |
100 if (_scheduledCloseCallback == null) { | 99 if (_scheduledCloseCallback == null) { |
101 _scheduledCloseCallback = new Timer(issueCloseCallback, 0, false); | 100 _scheduledCloseCallback = new Timer(issueCloseCallback, 0, false); |
102 _eof = true; | 101 _eof = true; |
103 } | 102 } |
104 } | 103 } |
105 } | 104 } |
106 } | 105 } |
107 | 106 |
108 File _file; | 107 RandomAccessFile _file; |
109 int _length; | 108 int _length; |
110 bool _eof = false; | 109 bool _eof = false; |
111 bool _closed = false; | 110 bool _closed = false; |
112 Timer _scheduledDataCallback; | 111 Timer _scheduledDataCallback; |
113 Timer _scheduledCloseCallback; | 112 Timer _scheduledCloseCallback; |
114 var _clientDataHandler; | 113 var _clientDataHandler; |
115 var _clientCloseHandler; | 114 var _clientCloseHandler; |
116 } | 115 } |
117 | 116 |
118 | 117 |
119 class _FileOutputStream implements FileOutputStream { | 118 class _FileOutputStream implements FileOutputStream { |
120 _FileOutputStream(File file) { | 119 _FileOutputStream(File file) { |
121 _file = new File(file.name); | 120 _file = file.openSync(true); |
122 _file.openSync(true); | |
123 } | 121 } |
124 | 122 |
125 bool write(List<int> buffer, [bool copyBuffer = false]) { | 123 bool write(List<int> buffer, [bool copyBuffer = false]) { |
126 return _write(buffer, 0, buffer.length); | 124 return _write(buffer, 0, buffer.length); |
127 } | 125 } |
128 | 126 |
129 bool writeFrom(List<int> buffer, [int offset, int len]) { | 127 bool writeFrom(List<int> buffer, [int offset, int len]) { |
130 return _write( | 128 return _write( |
131 buffer, offset, (len == null) ? buffer.length - offset : len); | 129 buffer, offset, (len == null) ? buffer.length - offset : len); |
132 } | 130 } |
(...skipping 20 matching lines...) Expand all Loading... |
153 | 151 |
154 bool _write(List<int> buffer, int offset, int len) { | 152 bool _write(List<int> buffer, int offset, int len) { |
155 int bytesWritten = _file.writeListSync(buffer, offset, len); | 153 int bytesWritten = _file.writeListSync(buffer, offset, len); |
156 if (bytesWritten == len) { | 154 if (bytesWritten == len) { |
157 return true; | 155 return true; |
158 } else { | 156 } else { |
159 throw "FileOutputStream: write error"; | 157 throw "FileOutputStream: write error"; |
160 } | 158 } |
161 } | 159 } |
162 | 160 |
163 File _file; | 161 RandomAccessFile _file; |
164 } | 162 } |
165 | 163 |
166 | 164 |
167 class _FileOperation { | 165 class _FileOperation { |
168 abstract void execute(ReceivePort port); | 166 abstract void execute(ReceivePort port); |
169 | 167 |
170 SendPort set replyPort(SendPort port) { | 168 SendPort set replyPort(SendPort port) { |
171 _replyPort = port; | 169 _replyPort = port; |
172 } | 170 } |
173 | 171 |
174 bool isWrite() => false; | 172 bool isWrite() => false; |
175 | 173 |
176 SendPort _replyPort; | 174 SendPort _replyPort; |
177 } | 175 } |
178 | 176 |
179 | 177 |
180 class _ExistsOperation extends _FileOperation { | 178 class _ExistsOperation extends _FileOperation { |
181 _ExistsOperation(String this._name); | 179 _ExistsOperation(String this._name); |
182 | 180 |
183 void execute(ReceivePort port) { | 181 void execute(ReceivePort port) { |
184 _replyPort.send(_File._exists(_name), port.toSendPort()); | 182 _replyPort.send(_FileUtils.exists(_name), port.toSendPort()); |
185 } | 183 } |
186 | 184 |
187 String _name; | 185 String _name; |
188 } | 186 } |
189 | 187 |
190 | 188 |
191 class _OpenOperation extends _FileOperation { | 189 class _OpenOperation extends _FileOperation { |
192 _OpenOperation(String this._name, bool this._writable); | 190 _OpenOperation(String this._name, bool this._writable); |
193 | 191 |
194 void execute(ReceivePort port) { | 192 void execute(ReceivePort port) { |
195 _replyPort.send(_File._checkedOpen(_name, _writable), port.toSendPort()); | 193 _replyPort.send(_FileUtils.checkedOpen(_name, _writable), |
| 194 port.toSendPort()); |
196 } | 195 } |
197 | 196 |
198 String _name; | 197 String _name; |
199 bool _writable; | 198 bool _writable; |
200 } | 199 } |
201 | 200 |
202 | 201 |
203 class _CloseOperation extends _FileOperation { | 202 class _CloseOperation extends _FileOperation { |
204 _CloseOperation(int this._id); | 203 _CloseOperation(int this._id); |
205 | 204 |
206 void execute(ReceivePort port) { | 205 void execute(ReceivePort port) { |
207 _replyPort.send(_File._close(_id), port.toSendPort()); | 206 _replyPort.send(_FileUtils.close(_id), port.toSendPort()); |
208 } | 207 } |
209 | 208 |
210 int _id; | 209 int _id; |
211 } | 210 } |
212 | 211 |
213 | 212 |
214 class _ReadByteOperation extends _FileOperation { | 213 class _ReadByteOperation extends _FileOperation { |
215 _ReadByteOperation(int this._id); | 214 _ReadByteOperation(int this._id); |
216 | 215 |
217 void execute(ReceivePort port) { | 216 void execute(ReceivePort port) { |
218 _replyPort.send(_File._readByte(_id), port.toSendPort()); | 217 _replyPort.send(_FileUtils.readByte(_id), port.toSendPort()); |
219 } | 218 } |
220 | 219 |
221 int _id; | 220 int _id; |
222 } | 221 } |
223 | 222 |
224 | 223 |
225 class _ReadListResult { | 224 class _ReadListResult { |
226 _ReadListResult(this.read, this.buffer); | 225 _ReadListResult(this.read, this.buffer); |
227 int read; | 226 int read; |
228 List buffer; | 227 List buffer; |
229 } | 228 } |
230 | 229 |
231 | 230 |
232 class _ReadListOperation extends _FileOperation { | 231 class _ReadListOperation extends _FileOperation { |
233 _ReadListOperation(int this._id, | 232 _ReadListOperation(int this._id, |
234 int this._length, | 233 int this._length, |
235 int this._offset, | 234 int this._offset, |
236 int this._bytes); | 235 int this._bytes); |
237 | 236 |
238 void execute(ReceivePort port) { | 237 void execute(ReceivePort port) { |
239 if (_bytes == 0) { | 238 if (_bytes == 0) { |
240 _replyPort.send(0, port.toSendPort()); | 239 _replyPort.send(0, port.toSendPort()); |
241 return; | 240 return; |
242 } | 241 } |
243 int index = _File._checkReadWriteListArguments(_length, _offset, _bytes); | 242 int index = |
| 243 _FileUtils.checkReadWriteListArguments(_length, _offset, _bytes); |
244 if (index != 0) { | 244 if (index != 0) { |
245 _replyPort.send("index out of range in readList: $index", | 245 _replyPort.send("index out of range in readList: $index", |
246 port.toSendPort()); | 246 port.toSendPort()); |
247 return; | 247 return; |
248 } | 248 } |
249 var buffer = new List(_bytes); | 249 var buffer = new List(_bytes); |
250 var result = | 250 var result = |
251 new _ReadListResult(_File._readList(_id, buffer, 0, _bytes), buffer); | 251 new _ReadListResult(_FileUtils.readList(_id, buffer, 0, _bytes), |
| 252 buffer); |
252 _replyPort.send(result, port.toSendPort()); | 253 _replyPort.send(result, port.toSendPort()); |
253 } | 254 } |
254 | 255 |
255 int _id; | 256 int _id; |
256 int _length; | 257 int _length; |
257 int _offset; | 258 int _offset; |
258 int _bytes; | 259 int _bytes; |
259 } | 260 } |
260 | 261 |
261 | 262 |
262 class _WriteByteOperation extends _FileOperation { | 263 class _WriteByteOperation extends _FileOperation { |
263 _WriteByteOperation(int this._id, int this._value); | 264 _WriteByteOperation(int this._id, int this._value); |
264 | 265 |
265 void execute(ReceivePort port) { | 266 void execute(ReceivePort port) { |
266 _replyPort.send(_File._writeByte(_id, _value), port.toSendPort()); | 267 _replyPort.send(_FileUtils.writeByte(_id, _value), port.toSendPort()); |
267 } | 268 } |
268 | 269 |
269 bool isWrite() => true; | 270 bool isWrite() => true; |
270 | 271 |
271 int _id; | 272 int _id; |
272 int _value; | 273 int _value; |
273 } | 274 } |
274 | 275 |
275 | 276 |
276 class _WriteListOperation extends _FileOperation { | 277 class _WriteListOperation extends _FileOperation { |
277 _WriteListOperation(int this._id, | 278 _WriteListOperation(int this._id, |
278 List this._buffer, | 279 List this._buffer, |
279 int this._offset, | 280 int this._offset, |
280 int this._bytes); | 281 int this._bytes); |
281 | 282 |
282 void execute(ReceivePort port) { | 283 void execute(ReceivePort port) { |
283 if (_bytes == 0) { | 284 if (_bytes == 0) { |
284 _replyPort.send(0, port.toSendPort()); | 285 _replyPort.send(0, port.toSendPort()); |
285 return; | 286 return; |
286 } | 287 } |
287 int index = | 288 int index = |
288 _File._checkReadWriteListArguments(_buffer.length, _offset, _bytes); | 289 _FileUtils.checkReadWriteListArguments(_buffer.length, _offset, _bytes); |
289 if (index != 0) { | 290 if (index != 0) { |
290 _replyPort.send("index out of range in writeList: $index", | 291 _replyPort.send("index out of range in writeList: $index", |
291 port.toSendPort()); | 292 port.toSendPort()); |
292 return; | 293 return; |
293 } | 294 } |
294 var result = _File._writeList(_id, _buffer, _offset, _bytes); | 295 var result = _FileUtils.writeList(_id, _buffer, _offset, _bytes); |
295 _replyPort.send(result, port.toSendPort()); | 296 _replyPort.send(result, port.toSendPort()); |
296 } | 297 } |
297 | 298 |
298 bool isWrite() => true; | 299 bool isWrite() => true; |
299 | 300 |
300 int _id; | 301 int _id; |
301 List _buffer; | 302 List _buffer; |
302 int _offset; | 303 int _offset; |
303 int _bytes; | 304 int _bytes; |
304 } | 305 } |
305 | 306 |
306 | 307 |
307 class _WriteStringOperation extends _FileOperation { | 308 class _WriteStringOperation extends _FileOperation { |
308 _WriteStringOperation(int this._id, String this._string); | 309 _WriteStringOperation(int this._id, String this._string); |
309 | 310 |
310 void execute(ReceivePort port) { | 311 void execute(ReceivePort port) { |
311 _replyPort.send(_File._checkedWriteString(_id, _string), port.toSendPort()); | 312 _replyPort.send(_FileUtils.checkedWriteString(_id, _string), |
| 313 port.toSendPort()); |
312 } | 314 } |
313 | 315 |
314 bool isWrite() => true; | 316 bool isWrite() => true; |
315 | 317 |
316 int _id; | 318 int _id; |
317 String _string; | 319 String _string; |
318 } | 320 } |
319 | 321 |
320 | 322 |
321 class _PositionOperation extends _FileOperation { | 323 class _PositionOperation extends _FileOperation { |
322 _PositionOperation(int this._id); | 324 _PositionOperation(int this._id); |
323 | 325 |
324 void execute(ReceivePort port) { | 326 void execute(ReceivePort port) { |
325 _replyPort.send(_File._position(_id), port.toSendPort()); | 327 _replyPort.send(_FileUtils.position(_id), port.toSendPort()); |
326 } | 328 } |
327 | 329 |
328 int _id; | 330 int _id; |
329 } | 331 } |
330 | 332 |
331 | 333 |
332 class _SetPositionOperation extends _FileOperation { | 334 class _SetPositionOperation extends _FileOperation { |
333 _SetPositionOperation(int this._id, int this._position); | 335 _SetPositionOperation(int this._id, int this._position); |
334 | 336 |
335 void execute(ReceivePort port) { | 337 void execute(ReceivePort port) { |
336 _replyPort.send(_File._setPosition(_id, _position), port.toSendPort()); | 338 _replyPort.send(_FileUtils.setPosition(_id, _position), port.toSendPort()); |
337 } | 339 } |
338 | 340 |
339 int _id; | 341 int _id; |
340 int _position; | 342 int _position; |
341 } | 343 } |
342 | 344 |
343 | 345 |
344 class _TruncateOperation extends _FileOperation { | 346 class _TruncateOperation extends _FileOperation { |
345 _TruncateOperation(int this._id, int this._length); | 347 _TruncateOperation(int this._id, int this._length); |
346 | 348 |
347 void execute(ReceivePort port) { | 349 void execute(ReceivePort port) { |
348 _replyPort.send(_File._truncate(_id, _length), port.toSendPort()); | 350 _replyPort.send(_FileUtils.truncate(_id, _length), port.toSendPort()); |
349 } | 351 } |
350 | 352 |
351 int _id; | 353 int _id; |
352 int _length; | 354 int _length; |
353 } | 355 } |
354 | 356 |
355 | 357 |
356 class _LengthOperation extends _FileOperation { | 358 class _LengthOperation extends _FileOperation { |
357 _LengthOperation(int this._id); | 359 _LengthOperation(int this._id); |
358 | 360 |
359 void execute(ReceivePort port) { | 361 void execute(ReceivePort port) { |
360 _replyPort.send(_File._length(_id), port.toSendPort()); | 362 _replyPort.send(_FileUtils.length(_id), port.toSendPort()); |
361 } | 363 } |
362 | 364 |
363 int _id; | 365 int _id; |
364 } | 366 } |
365 | 367 |
366 | 368 |
367 class _FlushOperation extends _FileOperation { | 369 class _FlushOperation extends _FileOperation { |
368 _FlushOperation(int this._id); | 370 _FlushOperation(int this._id); |
369 | 371 |
370 void execute(ReceivePort port) { | 372 void execute(ReceivePort port) { |
371 _replyPort.send(_File._flush(_id), port.toSendPort()); | 373 _replyPort.send(_FileUtils.flush(_id), port.toSendPort()); |
372 } | 374 } |
373 | 375 |
374 int _id; | 376 int _id; |
375 } | 377 } |
376 | 378 |
377 | 379 |
378 class _FullPathOperation extends _FileOperation { | 380 class _FullPathOperation extends _FileOperation { |
379 _FullPathOperation(String this._name); | 381 _FullPathOperation(String this._name); |
380 | 382 |
381 void execute(ReceivePort port) { | 383 void execute(ReceivePort port) { |
382 _replyPort.send(_File._checkedFullPath(_name), port.toSendPort()); | 384 _replyPort.send(_FileUtils.checkedFullPath(_name), port.toSendPort()); |
383 } | 385 } |
384 | 386 |
385 String _name; | 387 String _name; |
386 } | 388 } |
387 | 389 |
388 | 390 |
389 class _CreateOperation extends _FileOperation { | 391 class _CreateOperation extends _FileOperation { |
390 _CreateOperation(String this._name); | 392 _CreateOperation(String this._name); |
391 | 393 |
392 void execute(ReceivePort port) { | 394 void execute(ReceivePort port) { |
393 _replyPort.send(_File._checkedCreate(_name), port.toSendPort()); | 395 _replyPort.send(_FileUtils.checkedCreate(_name), port.toSendPort()); |
394 } | 396 } |
395 | 397 |
396 String _name; | 398 String _name; |
397 } | 399 } |
398 | 400 |
399 | 401 |
400 class _DeleteOperation extends _FileOperation { | 402 class _DeleteOperation extends _FileOperation { |
401 _DeleteOperation(String this._name); | 403 _DeleteOperation(String this._name); |
402 | 404 |
403 void execute(ReceivePort port) { | 405 void execute(ReceivePort port) { |
404 _replyPort.send(_File._checkedDelete(_name), port.toSendPort()); | 406 _replyPort.send(_FileUtils.checkedDelete(_name), port.toSendPort()); |
405 } | 407 } |
406 | 408 |
407 String _name; | 409 String _name; |
408 } | 410 } |
409 | 411 |
410 | 412 |
411 class _ExitOperation extends _FileOperation { | 413 class _ExitOperation extends _FileOperation { |
412 void execute(ReceivePort port) { | 414 void execute(ReceivePort port) { |
413 port.close(); | 415 port.close(); |
414 } | 416 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 } | 472 } |
471 }); | 473 }); |
472 return queuedWrites == 0; | 474 return queuedWrites == 0; |
473 } | 475 } |
474 | 476 |
475 Queue<_FileOperation> _queue; | 477 Queue<_FileOperation> _queue; |
476 _FileOperationIsolate _isolate; | 478 _FileOperationIsolate _isolate; |
477 } | 479 } |
478 | 480 |
479 | 481 |
| 482 // Helper class containing static file helper methods. |
| 483 class _FileUtils { |
| 484 static bool exists(String name) native "File_Exists"; |
| 485 static int open(String name, bool writable) native "File_Open"; |
| 486 static bool create(String name) native "File_Create"; |
| 487 static bool delete(String name) native "File_Delete"; |
| 488 static String fullPath(String name) native "File_FullPath"; |
| 489 static int close(int id) native "File_Close"; |
| 490 static int readByte(int id) native "File_ReadByte"; |
| 491 static int readList(int id, List<int> buffer, int offset, int bytes) |
| 492 native "File_ReadList"; |
| 493 static int writeByte(int id, int value) native "File_WriteByte"; |
| 494 static int writeList(int id, List<int> buffer, int offset, int bytes) |
| 495 native "File_WriteList"; |
| 496 static int writeString(int id, String string) native "File_WriteString"; |
| 497 static int position(int id) native "File_Position"; |
| 498 static bool setPosition(int id, int position) native "File_SetPosition"; |
| 499 static bool truncate(int id, int length) native "File_Truncate"; |
| 500 static int length(int id) native "File_Length"; |
| 501 static int flush(int id) native "File_Flush"; |
| 502 |
| 503 static int checkedOpen(String name, bool writable) { |
| 504 if (name is !String || writable is !bool) return 0; |
| 505 return open(name, writable); |
| 506 } |
| 507 |
| 508 static bool checkedCreate(String name) { |
| 509 if (name is !String) return false; |
| 510 return create(name); |
| 511 } |
| 512 |
| 513 static bool checkedDelete(String name) { |
| 514 if (name is !String) return false; |
| 515 return delete(name); |
| 516 } |
| 517 |
| 518 static String checkedFullPath(String name) { |
| 519 if (name is !String) return null; |
| 520 return fullPath(name); |
| 521 } |
| 522 |
| 523 static int checkReadWriteListArguments(int length, int offset, int bytes) { |
| 524 if (offset < 0) return offset; |
| 525 if (bytes < 0) return bytes; |
| 526 if ((offset + bytes) > length) return offset + bytes; |
| 527 return 0; |
| 528 } |
| 529 |
| 530 static int checkedWriteString(int id, String string) { |
| 531 if (string is !String) return -1; |
| 532 return writeString(id, string); |
| 533 } |
| 534 } |
| 535 |
| 536 |
480 // Class for encapsulating the native implementation of files. | 537 // Class for encapsulating the native implementation of files. |
481 class _File implements File { | 538 class _File implements File { |
482 // Constructor for file. | 539 // Constructor for file. |
483 _File(String this._name) | 540 _File(String this._name) |
484 : _scheduler = new _FileOperationScheduler(), | 541 : _scheduler = new _FileOperationScheduler(), |
485 _asyncUsed = false, | 542 _asyncUsed = false; |
486 _id = 0; | |
487 | |
488 static bool _exists(String name) native "File_Exists"; | |
489 static int _open(String name, bool writable) native "File_Open"; | |
490 static int _close(int id) native "File_Close"; | |
491 static int _readByte(int id) native "File_ReadByte"; | |
492 static int _readList(int id, List<int> buffer, int offset, int bytes) | |
493 native "File_ReadList"; | |
494 static int _writeByte(int id, int value) native "File_WriteByte"; | |
495 static int _writeList(int id, List<int> buffer, int offset, int bytes) | |
496 native "File_WriteList"; | |
497 static int _writeString(int id, String string) native "File_WriteString"; | |
498 static int _position(int id) native "File_Position"; | |
499 static bool _setPosition(int id, int position) native "File_SetPosition"; | |
500 static bool _truncate(int id, int length) native "File_Truncate"; | |
501 static int _length(int id) native "File_Length"; | |
502 static int _flush(int id) native "File_Flush"; | |
503 static bool _create(String name) native "File_Create"; | |
504 static bool _delete(String name) native "File_Delete"; | |
505 static String _fullPath(String name) native "File_FullPath"; | |
506 | |
507 static int _checkReadWriteListArguments(int length, int offset, int bytes) { | |
508 if (offset < 0) return offset; | |
509 if (bytes < 0) return bytes; | |
510 if ((offset + bytes) > length) return offset + bytes; | |
511 return 0; | |
512 } | |
513 | |
514 static int _checkedOpen(String name, bool writable) { | |
515 if (name is !String || writable is !bool) return 0; | |
516 return _open(name, writable); | |
517 } | |
518 | |
519 static bool _checkedCreate(String name) { | |
520 if (name is !String) return false; | |
521 return _create(name); | |
522 } | |
523 | |
524 static bool _checkedDelete(String name) { | |
525 if (name is !String) return false; | |
526 return _delete(name); | |
527 } | |
528 | |
529 static int _checkedWriteString(int id, String string) { | |
530 if (string is !String) return -1; | |
531 return _writeString(id, string); | |
532 } | |
533 | |
534 static String _checkedFullPath(String name) { | |
535 if (name is !String) return null; | |
536 return _fullPath(name); | |
537 } | |
538 | 543 |
539 void exists() { | 544 void exists() { |
540 _asyncUsed = true; | 545 _asyncUsed = true; |
541 if (_name is !String) { | 546 if (_name is !String) { |
542 if (_errorHandler != null) { | 547 if (_errorHandler != null) { |
543 _errorHandler('File name is not a string: $_name'); | 548 _errorHandler('File name is not a string: $_name'); |
544 } | 549 } |
545 return; | 550 return; |
546 } | 551 } |
547 var handler = | 552 var handler = |
548 (_existsHandler != null) ? _existsHandler : (result) => null; | 553 (_existsHandler != null) ? _existsHandler : (result) => null; |
549 var operation = new _ExistsOperation(_name); | 554 var operation = new _ExistsOperation(_name); |
550 _scheduler.enqueue(operation, (result, ignored) { _existsHandler(result); })
; | 555 _scheduler.enqueue(operation, (result, ignored) { _existsHandler(result); })
; |
551 } | 556 } |
552 | 557 |
553 bool existsSync() { | 558 bool existsSync() { |
554 if (_asyncUsed) { | 559 if (_asyncUsed) { |
555 throw new FileIOException( | 560 throw new FileIOException( |
556 "Mixed use of synchronous and asynchronous API"); | 561 "Mixed use of synchronous and asynchronous API"); |
557 } | 562 } |
558 if (_name is !String) { | 563 if (_name is !String) { |
559 throw new FileIOException('File name is not a string: $_name'); | 564 throw new FileIOException('File name is not a string: $_name'); |
560 } | 565 } |
561 return _exists(_name); | 566 return _FileUtils.exists(_name); |
562 } | 567 } |
563 | 568 |
564 void create() { | 569 void create() { |
565 _asyncUsed = true; | 570 _asyncUsed = true; |
566 var handler = (_createHandler != null) ? _createHandler : () => null; | 571 var handler = (_createHandler != null) ? _createHandler : () => null; |
567 var handleCreateResult = (created, ignored) { | 572 var handleCreateResult = (created, ignored) { |
568 if (created) { | 573 if (created) { |
569 handler(); | 574 handler(); |
570 } else if (_errorHandler != null) { | 575 } else if (_errorHandler != null) { |
571 _errorHandler("Cannot create file: $_name"); | 576 _errorHandler("Cannot create file: $_name"); |
572 } | 577 } |
573 }; | 578 }; |
574 var operation = new _CreateOperation(_name); | 579 var operation = new _CreateOperation(_name); |
575 _scheduler.enqueue(operation, handleCreateResult); | 580 _scheduler.enqueue(operation, handleCreateResult); |
576 } | 581 } |
577 | 582 |
578 void createSync() { | 583 void createSync() { |
579 if (_asyncUsed) { | 584 if (_asyncUsed) { |
580 throw new FileIOException( | 585 throw new FileIOException( |
581 "Mixed use of synchronous and asynchronous API"); | 586 "Mixed use of synchronous and asynchronous API"); |
582 } | 587 } |
583 bool created = _checkedCreate(_name); | 588 bool created = _FileUtils.checkedCreate(_name); |
584 if (!created) { | 589 if (!created) { |
585 throw new FileIOException("Cannot create file: $_name"); | 590 throw new FileIOException("Cannot create file: $_name"); |
586 } | 591 } |
587 } | 592 } |
588 | 593 |
589 void delete() { | 594 void delete() { |
590 _asyncUsed = true; | 595 _asyncUsed = true; |
591 var handler = (_deleteHandler != null) ? _deleteHandler : () => null; | 596 var handler = (_deleteHandler != null) ? _deleteHandler : () => null; |
592 var handleDeleteResult = (created, ignored) { | 597 var handleDeleteResult = (created, ignored) { |
593 if (created) { | 598 if (created) { |
594 handler(); | 599 handler(); |
595 } else if (_errorHandler != null) { | 600 } else if (_errorHandler != null) { |
596 _errorHandler("Cannot delete file: $_name"); | 601 _errorHandler("Cannot delete file: $_name"); |
597 } | 602 } |
598 }; | 603 }; |
599 var operation = new _DeleteOperation(_name); | 604 var operation = new _DeleteOperation(_name); |
600 _scheduler.enqueue(operation, handleDeleteResult); | 605 _scheduler.enqueue(operation, handleDeleteResult); |
601 } | 606 } |
602 | 607 |
603 void deleteSync() { | 608 void deleteSync() { |
604 if (_asyncUsed) { | 609 if (_asyncUsed) { |
605 throw new FileIOException( | 610 throw new FileIOException( |
606 "Mixed use of synchronous and asynchronous API"); | 611 "Mixed use of synchronous and asynchronous API"); |
607 } | 612 } |
608 bool deleted = _checkedDelete(_name); | 613 bool deleted = _FileUtils.checkedDelete(_name); |
609 if (!deleted) { | 614 if (!deleted) { |
610 throw new FileIOException("Cannot delete file: $_name"); | 615 throw new FileIOException("Cannot delete file: $_name"); |
611 } | 616 } |
612 } | 617 } |
613 | 618 |
614 void open([bool writable = false]) { | 619 void open([bool writable = false]) { |
615 _asyncUsed = true; | 620 _asyncUsed = true; |
616 var handler = (_openHandler != null) ? _openHandler : () => null; | 621 // If no open handler is present, close the file immediately to |
617 var handleOpenResult = (result, ignored) { | 622 // avoid leaking an open file descriptor. |
618 if (result != 0) { | 623 var handler = _openHandler; |
619 _id = result; | 624 if (handler === null) { |
620 handler(); | 625 handler = (file) => file.close(); |
| 626 } |
| 627 var handleOpenResult = (id, ignored) { |
| 628 if (id != 0) { |
| 629 var randomAccessFile = new _RandomAccessFile(id, _name); |
| 630 handler(randomAccessFile); |
621 } else if (_errorHandler != null) { | 631 } else if (_errorHandler != null) { |
622 _errorHandler("Cannot open file: $_name"); | 632 _errorHandler("Cannot open file: $_name"); |
623 } | 633 } |
624 }; | 634 }; |
625 var operation = new _OpenOperation(_name, writable); | 635 var operation = new _OpenOperation(_name, writable); |
626 _scheduler.enqueue(operation, handleOpenResult); | 636 _scheduler.enqueue(operation, handleOpenResult); |
627 } | 637 } |
628 | 638 |
629 void openSync([bool writable = false]) { | 639 void openSync([bool writable = false]) { |
630 if (_asyncUsed) { | 640 if (_asyncUsed) { |
631 throw new FileIOException( | 641 throw new FileIOException( |
632 "Mixed use of synchronous and asynchronous API"); | 642 "Mixed use of synchronous and asynchronous API"); |
633 } | 643 } |
634 _id = _checkedOpen(_name, writable); | 644 var id = _FileUtils.checkedOpen(_name, writable); |
635 if (_id == 0) { | 645 if (id == 0) { |
636 throw new FileIOException("Cannot open file: $_name"); | 646 throw new FileIOException("Cannot open file: $_name"); |
637 } | 647 } |
| 648 return new _RandomAccessFile(id, _name); |
638 } | 649 } |
639 | 650 |
| 651 void fullPath() { |
| 652 _asyncUsed = true; |
| 653 var handler = _fullPathHandler; |
| 654 if (handler == null) handler = (path) => null; |
| 655 var handleFullPathResult = (result, ignored) { |
| 656 if (result != null) { |
| 657 handler(result); |
| 658 } else if (_errorHandler != null) { |
| 659 _errorHandler("fullPath failed"); |
| 660 } |
| 661 }; |
| 662 var operation = new _FullPathOperation(_name); |
| 663 _scheduler.enqueue(operation, handleFullPathResult); |
| 664 } |
| 665 |
| 666 String fullPathSync() { |
| 667 if (_asyncUsed) { |
| 668 throw new FileIOException( |
| 669 "Mixed use of synchronous and asynchronous API"); |
| 670 } |
| 671 String result = _FileUtils.checkedFullPath(_name); |
| 672 if (result == null) { |
| 673 throw new FileIOException("fullPath failed"); |
| 674 } |
| 675 return result; |
| 676 } |
| 677 |
| 678 InputStream openInputStream() => new _FileInputStream(this); |
| 679 |
| 680 OutputStream openOutputStream() => new _FileOutputStream(this); |
| 681 |
| 682 String get name() => _name; |
| 683 |
| 684 void set existsHandler(void handler(bool exists)) { |
| 685 _existsHandler = handler; |
| 686 } |
| 687 |
| 688 void set createHandler(void handler()) { |
| 689 _createHandler = handler; |
| 690 } |
| 691 |
| 692 void set deleteHandler(void handler()) { |
| 693 _deleteHandler = handler; |
| 694 } |
| 695 |
| 696 void set openHandler(void handler(RandomAccessFile file)) { |
| 697 _openHandler = handler; |
| 698 } |
| 699 |
| 700 void set fullPathHandler(void handler(String)) { |
| 701 _fullPathHandler = handler; |
| 702 } |
| 703 |
| 704 void set errorHandler(void handler(String error)) { |
| 705 _errorHandler = handler; |
| 706 } |
| 707 |
| 708 String _name; |
| 709 bool _asyncUsed; |
| 710 |
| 711 _FileOperationScheduler _scheduler; |
| 712 |
| 713 var _existsHandler; |
| 714 var _createHandler; |
| 715 var _deleteHandler; |
| 716 var _openHandler; |
| 717 var _fullPathHandler; |
| 718 var _errorHandler; |
| 719 } |
| 720 |
| 721 |
| 722 class _RandomAccessFile implements RandomAccessFile { |
| 723 _RandomAccessFile(int this._id, String this._name) |
| 724 : _scheduler = new _FileOperationScheduler(), |
| 725 _asyncUsed = false; |
| 726 |
640 void close() { | 727 void close() { |
641 _asyncUsed = true; | 728 _asyncUsed = true; |
642 var handler = (_closeHandler != null) ? _closeHandler : () => null; | 729 var handler = (_closeHandler != null) ? _closeHandler : () => null; |
643 var handleOpenResult = (result, ignored) { | 730 var handleOpenResult = (result, ignored) { |
644 if (result != -1) { | 731 if (result != -1) { |
645 _id = result; | 732 _id = result; |
646 handler(); | 733 handler(); |
647 } else if (_errorHandler != null) { | 734 } else if (_errorHandler != null) { |
648 _errorHandler("Cannot close file: $_name"); | 735 _errorHandler("Cannot close file: $_name"); |
649 } | 736 } |
650 }; | 737 }; |
651 var operation = new _CloseOperation(_id); | 738 var operation = new _CloseOperation(_id); |
652 _scheduler.enqueue(operation, handleOpenResult); | 739 _scheduler.enqueue(operation, handleOpenResult); |
653 } | 740 } |
654 | 741 |
655 void closeSync() { | 742 void closeSync() { |
656 if (_asyncUsed) { | 743 if (_asyncUsed) { |
657 throw new FileIOException( | 744 throw new FileIOException( |
658 "Mixed use of synchronous and asynchronous API"); | 745 "Mixed use of synchronous and asynchronous API"); |
659 } | 746 } |
660 var id = _close(_id); | 747 var id = _FileUtils.close(_id); |
661 if (id == -1) { | 748 if (id == -1) { |
662 throw new FileIOException("Cannot close file: $_name"); | 749 throw new FileIOException("Cannot close file: $_name"); |
663 } | 750 } |
664 _id = id; | 751 _id = id; |
665 } | 752 } |
666 | 753 |
667 void readByte() { | 754 void readByte() { |
668 _asyncUsed = true; | 755 _asyncUsed = true; |
669 var handler = | 756 var handler = |
670 (_readByteHandler != null) ? _readByteHandler : (byte) => null; | 757 (_readByteHandler != null) ? _readByteHandler : (byte) => null; |
671 var handleReadByteResult = (result, ignored) { | 758 var handleReadByteResult = (result, ignored) { |
672 if (result != -1) { | 759 if (result != -1) { |
673 handler(result); | 760 handler(result); |
674 } else if (_errorHandler != null) { | 761 } else if (_errorHandler != null) { |
675 _errorHandler("readByte failed"); | 762 _errorHandler("readByte failed"); |
676 } | 763 } |
677 }; | 764 }; |
678 var operation = new _ReadByteOperation(_id); | 765 var operation = new _ReadByteOperation(_id); |
679 _scheduler.enqueue(operation, handleReadByteResult); | 766 _scheduler.enqueue(operation, handleReadByteResult); |
680 } | 767 } |
681 | 768 |
682 int readByteSync() { | 769 int readByteSync() { |
683 if (_asyncUsed) { | 770 if (_asyncUsed) { |
684 throw new FileIOException( | 771 throw new FileIOException( |
685 "Mixed use of synchronous and asynchronous API"); | 772 "Mixed use of synchronous and asynchronous API"); |
686 } | 773 } |
687 int result = _readByte(_id); | 774 int result = _FileUtils.readByte(_id); |
688 if (result == -1) { | 775 if (result == -1) { |
689 throw new FileIOException("readByte failed"); | 776 throw new FileIOException("readByte failed"); |
690 } | 777 } |
691 return result; | 778 return result; |
692 } | 779 } |
693 | 780 |
694 void readList(List<int> buffer, int offset, int bytes) { | 781 void readList(List<int> buffer, int offset, int bytes) { |
695 _asyncUsed = true; | 782 _asyncUsed = true; |
696 if (buffer is !List || offset is !int || bytes is !int) { | 783 if (buffer is !List || offset is !int || bytes is !int) { |
697 if (_errorHandler != null) { | 784 if (_errorHandler != null) { |
(...skipping 20 matching lines...) Expand all Loading... |
718 | 805 |
719 int readListSync(List<int> buffer, int offset, int bytes) { | 806 int readListSync(List<int> buffer, int offset, int bytes) { |
720 if (_asyncUsed) { | 807 if (_asyncUsed) { |
721 throw new FileIOException( | 808 throw new FileIOException( |
722 "Mixed use of synchronous and asynchronous API"); | 809 "Mixed use of synchronous and asynchronous API"); |
723 } | 810 } |
724 if (buffer is !List || offset is !int || bytes is !int) { | 811 if (buffer is !List || offset is !int || bytes is !int) { |
725 throw new FileIOException("Invalid arguments to readList"); | 812 throw new FileIOException("Invalid arguments to readList"); |
726 } | 813 } |
727 if (bytes == 0) return 0; | 814 if (bytes == 0) return 0; |
728 int index = _checkReadWriteListArguments(buffer.length, offset, bytes); | 815 int index = |
| 816 _FileUtils.checkReadWriteListArguments(buffer.length, offset, bytes); |
729 if (index != 0) { | 817 if (index != 0) { |
730 throw new IndexOutOfRangeException(index); | 818 throw new IndexOutOfRangeException(index); |
731 } | 819 } |
732 int result = _readList(_id, buffer, offset, bytes); | 820 int result = _FileUtils.readList(_id, buffer, offset, bytes); |
733 if (result == -1) { | 821 if (result == -1) { |
734 throw new FileIOException("readList failed"); | 822 throw new FileIOException("readList failed"); |
735 } | 823 } |
736 return result; | 824 return result; |
737 } | 825 } |
738 | 826 |
739 void _checkPendingWrites() { | 827 void _checkPendingWrites() { |
740 if (_scheduler.noPendingWrite() && _noPendingWriteHandler != null) { | 828 if (_scheduler.noPendingWrite() && _noPendingWriteHandler != null) { |
741 _noPendingWriteHandler(); | 829 _noPendingWriteHandler(); |
742 } | 830 } |
(...skipping 19 matching lines...) Expand all Loading... |
762 } | 850 } |
763 | 851 |
764 int writeByteSync(int value) { | 852 int writeByteSync(int value) { |
765 if (_asyncUsed) { | 853 if (_asyncUsed) { |
766 throw new FileIOException( | 854 throw new FileIOException( |
767 "Mixed use of synchronous and asynchronous API"); | 855 "Mixed use of synchronous and asynchronous API"); |
768 } | 856 } |
769 if (value is !int) { | 857 if (value is !int) { |
770 throw new FileIOException("Invalid argument to writeByte"); | 858 throw new FileIOException("Invalid argument to writeByte"); |
771 } | 859 } |
772 int result = _writeByte(_id, value); | 860 int result = _FileUtils.writeByte(_id, value); |
773 if (result == -1) { | 861 if (result == -1) { |
774 throw new FileIOException("writeByte failed"); | 862 throw new FileIOException("writeByte failed"); |
775 } | 863 } |
776 return result; | 864 return result; |
777 } | 865 } |
778 | 866 |
779 void writeList(List<int> buffer, int offset, int bytes) { | 867 void writeList(List<int> buffer, int offset, int bytes) { |
780 _asyncUsed = true; | 868 _asyncUsed = true; |
781 if (buffer is !List || offset is !int || bytes is !int) { | 869 if (buffer is !List || offset is !int || bytes is !int) { |
782 if (_errorHandler != null) { | 870 if (_errorHandler != null) { |
(...skipping 20 matching lines...) Expand all Loading... |
803 | 891 |
804 int writeListSync(List<int> buffer, int offset, int bytes) { | 892 int writeListSync(List<int> buffer, int offset, int bytes) { |
805 if (_asyncUsed) { | 893 if (_asyncUsed) { |
806 throw new FileIOException( | 894 throw new FileIOException( |
807 "Mixed use of synchronous and asynchronous API"); | 895 "Mixed use of synchronous and asynchronous API"); |
808 } | 896 } |
809 if (buffer is !List || offset is !int || bytes is !int) { | 897 if (buffer is !List || offset is !int || bytes is !int) { |
810 throw new FileIOException("Invalid arguments to writeList"); | 898 throw new FileIOException("Invalid arguments to writeList"); |
811 } | 899 } |
812 if (bytes == 0) return 0; | 900 if (bytes == 0) return 0; |
813 int index = _checkReadWriteListArguments(buffer.length, offset, bytes); | 901 int index = |
| 902 _FileUtils.checkReadWriteListArguments(buffer.length, offset, bytes); |
814 if (index != 0) { | 903 if (index != 0) { |
815 throw new IndexOutOfRangeException(index); | 904 throw new IndexOutOfRangeException(index); |
816 } | 905 } |
817 int result = _writeList(_id, buffer, offset, bytes); | 906 int result = _FileUtils.writeList(_id, buffer, offset, bytes); |
818 if (result == -1) { | 907 if (result == -1) { |
819 throw new FileIOException("writeList failed"); | 908 throw new FileIOException("writeList failed"); |
820 } | 909 } |
821 return result; | 910 return result; |
822 } | 911 } |
823 | 912 |
824 void writeString(String string) { | 913 void writeString(String string) { |
825 _asyncUsed = true; | 914 _asyncUsed = true; |
826 var handleWriteStringResult = (result, ignored) { | 915 var handleWriteStringResult = (result, ignored) { |
827 if (result == -1 &&_errorHandler != null) { | 916 if (result == -1 &&_errorHandler != null) { |
828 _errorHandler("writeString failed"); | 917 _errorHandler("writeString failed"); |
829 return; | 918 return; |
830 } | 919 } |
831 if (result < string.length) { | 920 if (result < string.length) { |
832 writeString(string.substring(result)); | 921 writeString(string.substring(result)); |
833 } else { | 922 } else { |
834 _checkPendingWrites(); | 923 _checkPendingWrites(); |
835 } | 924 } |
836 }; | 925 }; |
837 var operation = new _WriteStringOperation(_id, string); | 926 var operation = new _WriteStringOperation(_id, string); |
838 _scheduler.enqueue(operation, handleWriteStringResult); | 927 _scheduler.enqueue(operation, handleWriteStringResult); |
839 } | 928 } |
840 | 929 |
841 int writeStringSync(String string) { | 930 int writeStringSync(String string) { |
842 if (_asyncUsed) { | 931 if (_asyncUsed) { |
843 throw new FileIOException( | 932 throw new FileIOException( |
844 "Mixed use of synchronous and asynchronous API"); | 933 "Mixed use of synchronous and asynchronous API"); |
845 } | 934 } |
846 int result = _checkedWriteString(_id, string); | 935 int result = _FileUtils.checkedWriteString(_id, string); |
847 if (result == -1) { | 936 if (result == -1) { |
848 throw new FileIOException("writeString failed"); | 937 throw new FileIOException("writeString failed"); |
849 } | 938 } |
850 return result; | 939 return result; |
851 } | 940 } |
852 | 941 |
853 void position() { | 942 void position() { |
854 _asyncUsed = true; | 943 _asyncUsed = true; |
855 var handler = (_positionHandler != null) ? _positionHandler : (pos) => null; | 944 var handler = (_positionHandler != null) ? _positionHandler : (pos) => null; |
856 var handlePositionResult = (result, ignored) { | 945 var handlePositionResult = (result, ignored) { |
857 if (result == -1 && _errorHandler != null) { | 946 if (result == -1 && _errorHandler != null) { |
858 _errorHandler("position failed"); | 947 _errorHandler("position failed"); |
859 return; | 948 return; |
860 } | 949 } |
861 handler(result); | 950 handler(result); |
862 }; | 951 }; |
863 var operation = new _PositionOperation(_id); | 952 var operation = new _PositionOperation(_id); |
864 _scheduler.enqueue(operation, handlePositionResult); | 953 _scheduler.enqueue(operation, handlePositionResult); |
865 } | 954 } |
866 | 955 |
867 int positionSync() { | 956 int positionSync() { |
868 if (_asyncUsed) { | 957 if (_asyncUsed) { |
869 throw new FileIOException( | 958 throw new FileIOException( |
870 "Mixed use of synchronous and asynchronous API"); | 959 "Mixed use of synchronous and asynchronous API"); |
871 } | 960 } |
872 int result = _position(_id); | 961 int result = _FileUtils.position(_id); |
873 if (result == -1) { | 962 if (result == -1) { |
874 throw new FileIOException("position failed"); | 963 throw new FileIOException("position failed"); |
875 } | 964 } |
876 return result; | 965 return result; |
877 } | 966 } |
878 | 967 |
879 void setPosition(int position) { | 968 void setPosition(int position) { |
880 _asyncUsed = true; | 969 _asyncUsed = true; |
881 var handler = | 970 var handler = |
882 (_setPositionHandler != null) ? _setPositionHandler : () => null; | 971 (_setPositionHandler != null) ? _setPositionHandler : () => null; |
883 var handleSetPositionResult = (result, ignored) { | 972 var handleSetPositionResult = (result, ignored) { |
884 if (result == false && _errorHandler != null) { | 973 if (result == false && _errorHandler != null) { |
885 _errorHandler("setPosition failed"); | 974 _errorHandler("setPosition failed"); |
886 return; | 975 return; |
887 } | 976 } |
888 handler(); | 977 handler(); |
889 }; | 978 }; |
890 var operation = new _SetPositionOperation(_id, position); | 979 var operation = new _SetPositionOperation(_id, position); |
891 _scheduler.enqueue(operation, handleSetPositionResult); | 980 _scheduler.enqueue(operation, handleSetPositionResult); |
892 } | 981 } |
893 | 982 |
894 void setPositionSync(int position) { | 983 void setPositionSync(int position) { |
895 if (_asyncUsed) { | 984 if (_asyncUsed) { |
896 throw new FileIOException( | 985 throw new FileIOException( |
897 "Mixed use of synchronous and asynchronous API"); | 986 "Mixed use of synchronous and asynchronous API"); |
898 } | 987 } |
899 bool result = _setPosition(_id, position); | 988 bool result = _FileUtils.setPosition(_id, position); |
900 if (result == false) { | 989 if (result == false) { |
901 throw new FileIOException("setPosition failed"); | 990 throw new FileIOException("setPosition failed"); |
902 } | 991 } |
903 } | 992 } |
904 | 993 |
905 void truncate(int length) { | 994 void truncate(int length) { |
906 _asyncUsed = true; | 995 _asyncUsed = true; |
907 var handler = (_truncateHandler != null) ? _truncateHandler : () => null; | 996 var handler = (_truncateHandler != null) ? _truncateHandler : () => null; |
908 var handleTruncateResult = (result, ignored) { | 997 var handleTruncateResult = (result, ignored) { |
909 if (result == false && _errorHandler != null) { | 998 if (result == false && _errorHandler != null) { |
910 _errorHandler("truncate failed"); | 999 _errorHandler("truncate failed"); |
911 return; | 1000 return; |
912 } | 1001 } |
913 handler(); | 1002 handler(); |
914 }; | 1003 }; |
915 var operation = new _TruncateOperation(_id, length); | 1004 var operation = new _TruncateOperation(_id, length); |
916 _scheduler.enqueue(operation, handleTruncateResult); | 1005 _scheduler.enqueue(operation, handleTruncateResult); |
917 } | 1006 } |
918 | 1007 |
919 void truncateSync(int length) { | 1008 void truncateSync(int length) { |
920 if (_asyncUsed) { | 1009 if (_asyncUsed) { |
921 throw new FileIOException( | 1010 throw new FileIOException( |
922 "Mixed use of synchronous and asynchronous API"); | 1011 "Mixed use of synchronous and asynchronous API"); |
923 } | 1012 } |
924 bool result = _truncate(_id, length); | 1013 bool result = _FileUtils.truncate(_id, length); |
925 if (result == false) { | 1014 if (result == false) { |
926 throw new FileIOException("truncate failed"); | 1015 throw new FileIOException("truncate failed"); |
927 } | 1016 } |
928 } | 1017 } |
929 | 1018 |
930 void length() { | 1019 void length() { |
931 _asyncUsed = true; | 1020 _asyncUsed = true; |
932 var handler = (_lengthHandler != null) ? _lengthHandler : (pos) => null; | 1021 var handler = (_lengthHandler != null) ? _lengthHandler : (pos) => null; |
933 var handleLengthResult = (result, ignored) { | 1022 var handleLengthResult = (result, ignored) { |
934 if (result == -1 && _errorHandler != null) { | 1023 if (result == -1 && _errorHandler != null) { |
935 _errorHandler("length failed"); | 1024 _errorHandler("length failed"); |
936 return; | 1025 return; |
937 } | 1026 } |
938 handler(result); | 1027 handler(result); |
939 }; | 1028 }; |
940 var operation = new _LengthOperation(_id); | 1029 var operation = new _LengthOperation(_id); |
941 _scheduler.enqueue(operation, handleLengthResult); | 1030 _scheduler.enqueue(operation, handleLengthResult); |
942 } | 1031 } |
943 | 1032 |
944 int lengthSync() { | 1033 int lengthSync() { |
945 if (_asyncUsed) { | 1034 if (_asyncUsed) { |
946 throw new FileIOException( | 1035 throw new FileIOException( |
947 "Mixed use of synchronous and asynchronous API"); | 1036 "Mixed use of synchronous and asynchronous API"); |
948 } | 1037 } |
949 int result = _length(_id); | 1038 int result = _FileUtils.length(_id); |
950 if (result == -1) { | 1039 if (result == -1) { |
951 throw new FileIOException("length failed"); | 1040 throw new FileIOException("length failed"); |
952 } | 1041 } |
953 return result; | 1042 return result; |
954 } | 1043 } |
955 | 1044 |
956 void flush() { | 1045 void flush() { |
957 _asyncUsed = true; | 1046 _asyncUsed = true; |
958 var handler = (_flushHandler != null) ? _flushHandler : (pos) => null; | 1047 var handler = (_flushHandler != null) ? _flushHandler : (pos) => null; |
959 var handleFlushResult = (result, ignored) { | 1048 var handleFlushResult = (result, ignored) { |
960 if (result == -1 && _errorHandler != null) { | 1049 if (result == -1 && _errorHandler != null) { |
961 _errorHandler("flush failed"); | 1050 _errorHandler("flush failed"); |
962 return; | 1051 return; |
963 } | 1052 } |
964 handler(); | 1053 handler(); |
965 }; | 1054 }; |
966 var operation = new _FlushOperation(_id); | 1055 var operation = new _FlushOperation(_id); |
967 _scheduler.enqueue(operation, handleFlushResult); | 1056 _scheduler.enqueue(operation, handleFlushResult); |
968 } | 1057 } |
969 | 1058 |
970 void flushSync() { | 1059 void flushSync() { |
971 if (_asyncUsed) { | 1060 if (_asyncUsed) { |
972 throw new FileIOException( | 1061 throw new FileIOException( |
973 "Mixed use of synchronous and asynchronous API"); | 1062 "Mixed use of synchronous and asynchronous API"); |
974 } | 1063 } |
975 int result = _flush(_id); | 1064 int result = _FileUtils.flush(_id); |
976 if (result == -1) { | 1065 if (result == -1) { |
977 throw new FileIOException("flush failed"); | 1066 throw new FileIOException("flush failed"); |
978 } | 1067 } |
979 } | 1068 } |
980 | 1069 |
981 void fullPath() { | 1070 String get name() => _name; |
982 _asyncUsed = true; | |
983 var handler = _fullPathHandler; | |
984 if (handler == null) handler = (path) => null; | |
985 var handleFullPathResult = (result, ignored) { | |
986 if (result != null) { | |
987 handler(result); | |
988 } else if (_errorHandler != null) { | |
989 _errorHandler("fullPath failed"); | |
990 } | |
991 }; | |
992 var operation = new _FullPathOperation(_name); | |
993 _scheduler.enqueue(operation, handleFullPathResult); | |
994 } | |
995 | 1071 |
996 String fullPathSync() { | 1072 void set errorHandler(void handler(String error)) { |
997 if (_asyncUsed) { | 1073 _errorHandler = handler; |
998 throw new FileIOException( | |
999 "Mixed use of synchronous and asynchronous API"); | |
1000 } | |
1001 String result = _checkedFullPath(_name); | |
1002 if (result == null) { | |
1003 throw new FileIOException("fullPath failed"); | |
1004 } | |
1005 return result; | |
1006 } | |
1007 | |
1008 InputStream openInputStream() { | |
1009 return new _FileInputStream(this); | |
1010 } | |
1011 | |
1012 OutputStream openOutputStream() { | |
1013 return new _FileOutputStream(this); | |
1014 } | |
1015 | |
1016 String get name() { | |
1017 return _name; | |
1018 } | |
1019 | |
1020 void set existsHandler(void handler(bool exists)) { | |
1021 _existsHandler = handler; | |
1022 } | |
1023 | |
1024 void set createHandler(void handler()) { | |
1025 _createHandler = handler; | |
1026 } | |
1027 | |
1028 void set deleteHandler(void handler()) { | |
1029 _deleteHandler = handler; | |
1030 } | |
1031 | |
1032 void set openHandler(void handler()) { | |
1033 _openHandler = handler; | |
1034 } | 1074 } |
1035 | 1075 |
1036 void set closeHandler(void handler()) { | 1076 void set closeHandler(void handler()) { |
1037 _closeHandler = handler; | 1077 _closeHandler = handler; |
1038 } | 1078 } |
1039 | 1079 |
1040 void set readByteHandler(void handler(int byte)) { | 1080 void set readByteHandler(void handler(int byte)) { |
1041 _readByteHandler = handler; | 1081 _readByteHandler = handler; |
1042 } | 1082 } |
1043 | 1083 |
(...skipping 18 matching lines...) Expand all Loading... |
1062 } | 1102 } |
1063 | 1103 |
1064 void set lengthHandler(void handler(int length)) { | 1104 void set lengthHandler(void handler(int length)) { |
1065 _lengthHandler = handler; | 1105 _lengthHandler = handler; |
1066 } | 1106 } |
1067 | 1107 |
1068 void set flushHandler(void handler()) { | 1108 void set flushHandler(void handler()) { |
1069 _flushHandler = handler; | 1109 _flushHandler = handler; |
1070 } | 1110 } |
1071 | 1111 |
1072 void set fullPathHandler(void handler(String)) { | |
1073 _fullPathHandler = handler; | |
1074 } | |
1075 | |
1076 void set errorHandler(void handler(String error)) { | |
1077 _errorHandler = handler; | |
1078 } | |
1079 | |
1080 String _name; | 1112 String _name; |
1081 int _id; | 1113 int _id; |
1082 bool _asyncUsed; | 1114 bool _asyncUsed; |
1083 | 1115 |
1084 _FileOperationScheduler _scheduler; | 1116 _FileOperationScheduler _scheduler; |
1085 | 1117 |
1086 var _existsHandler; | |
1087 var _createHandler; | |
1088 var _deleteHandler; | |
1089 var _openHandler; | |
1090 var _closeHandler; | 1118 var _closeHandler; |
1091 var _readByteHandler; | 1119 var _readByteHandler; |
1092 var _readListHandler; | 1120 var _readListHandler; |
1093 var _noPendingWriteHandler; | 1121 var _noPendingWriteHandler; |
1094 var _positionHandler; | 1122 var _positionHandler; |
1095 var _setPositionHandler; | 1123 var _setPositionHandler; |
1096 var _truncateHandler; | 1124 var _truncateHandler; |
1097 var _lengthHandler; | 1125 var _lengthHandler; |
1098 var _flushHandler; | 1126 var _flushHandler; |
1099 var _fullPathHandler; | |
1100 var _errorHandler; | 1127 var _errorHandler; |
1101 } | 1128 } |
OLD | NEW |