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 class _FileInputStream extends _BaseDataInputStream implements InputStream { | 5 class _FileInputStream extends _BaseDataInputStream implements InputStream { |
6 _FileInputStream(String name) { | 6 _FileInputStream(String name) { |
7 _file = new File(name); | 7 _file = new File(name); |
8 _data = []; | 8 _data = []; |
9 _position = 0; | 9 _position = 0; |
10 _file.errorHandler = (String s) { | 10 _file.onError = (String s) { |
11 if (_clientErrorHandler != null) { | 11 if (_clientErrorHandler != null) { |
12 _clientErrorHandler(); | 12 _clientErrorHandler(); |
13 } | 13 } |
14 }; | 14 }; |
15 _file.open(); | 15 _file.open(); |
16 _file.openHandler = (openedFile) { | 16 _file.onOpen = (openedFile) { |
17 _readDataFromFile(openedFile); | 17 _readDataFromFile(openedFile); |
18 }; | 18 }; |
19 } | 19 } |
20 | 20 |
21 _FileInputStream.fromStdio(int fd) { | 21 _FileInputStream.fromStdio(int fd) { |
22 assert(fd == 0); | 22 assert(fd == 0); |
23 _file = _File._openStdioSync(fd); | 23 _file = _File._openStdioSync(fd); |
24 _data = []; | 24 _data = []; |
25 _position = 0; | 25 _position = 0; |
26 _readDataFromFile(_file); | 26 _readDataFromFile(_file); |
27 } | 27 } |
28 | 28 |
29 void _readDataFromFile(RandomAccessFile openedFile) { | 29 void _readDataFromFile(RandomAccessFile openedFile) { |
30 openedFile.errorHandler = (String s) { | 30 openedFile.onError = (String s) { |
31 if (_clientErrorHandler != null) { | 31 if (_clientErrorHandler != null) { |
32 _clientErrorHandler(); | 32 _clientErrorHandler(); |
33 } | 33 } |
34 }; | 34 }; |
35 openedFile.length(); | 35 openedFile.length(); |
36 openedFile.lengthHandler = (length) { | 36 openedFile.onLength = (length) { |
37 var contents = new ByteArray(length); | 37 var contents = new ByteArray(length); |
38 if (length != 0) { | 38 if (length != 0) { |
39 openedFile.readList(contents, 0, length); | 39 openedFile.readList(contents, 0, length); |
40 openedFile.readListHandler = (read) { | 40 openedFile.onReadList = (read) { |
41 if (read != length) { | 41 if (read != length) { |
42 if (_clientErrorHandler != null) { | 42 if (_clientErrorHandler != null) { |
43 _clientErrorHandler(); | 43 _clientErrorHandler(); |
44 } | 44 } |
45 _streamMarkedClosed = true; | 45 _streamMarkedClosed = true; |
46 _checkScheduleCallbacks(); | 46 _checkScheduleCallbacks(); |
47 } else { | 47 } else { |
48 _data = contents; | 48 _data = contents; |
49 _streamMarkedClosed = true; | 49 _streamMarkedClosed = true; |
50 _checkScheduleCallbacks(); | 50 _checkScheduleCallbacks(); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 int _position; | 92 int _position; |
93 bool _closed = false; | 93 bool _closed = false; |
94 } | 94 } |
95 | 95 |
96 | 96 |
97 class _FileOutputStream implements OutputStream { | 97 class _FileOutputStream implements OutputStream { |
98 _FileOutputStream(String name, FileMode mode) { | 98 _FileOutputStream(String name, FileMode mode) { |
99 _pendingOperations = new List<List<int>>(); | 99 _pendingOperations = new List<List<int>>(); |
100 var f = new File(name); | 100 var f = new File(name); |
101 f.open(mode); | 101 f.open(mode); |
102 f.openHandler = (openedFile) { | 102 f.onOpen = (openedFile) { |
103 _file = openedFile; | 103 _file = openedFile; |
104 _setupFileHandlers(); | 104 _setupFileHandlers(); |
105 _processPendingOperations(); | 105 _processPendingOperations(); |
106 }; | 106 }; |
107 f.errorHandler = (e) { | 107 f.onError = (e) { |
108 if (_errorHandler != null) _errorHandler(); | 108 if (_onError != null) _onError(); |
109 }; | 109 }; |
110 } | 110 } |
111 | 111 |
112 _FileOutputStream.fromStdio(int fd) { | 112 _FileOutputStream.fromStdio(int fd) { |
113 assert(1 <= fd && fd <= 2); | 113 assert(1 <= fd && fd <= 2); |
114 _file = _File._openStdioSync(fd); | 114 _file = _File._openStdioSync(fd); |
115 _setupFileHandlers(); | 115 _setupFileHandlers(); |
116 } | 116 } |
117 | 117 |
118 | 118 |
119 void _setupFileHandlers() { | 119 void _setupFileHandlers() { |
120 _file.errorHandler = (e) { | 120 _file.onError = (e) { |
121 if (_errorHandler != null) _errorHandler(); | 121 if (_onError != null) _onError(); |
122 }; | 122 }; |
123 _file.noPendingWriteHandler = () { | 123 _file.onNoPendingWrite = () { |
124 if (!_streamMarkedClosed && _noPendingWriteHandler != null) { | 124 if (!_streamMarkedClosed && _onNoPendingWrite != null) { |
125 _noPendingWriteHandler(); | 125 _onNoPendingWrite(); |
126 } | 126 } |
127 }; | 127 }; |
128 _file.closeHandler = () { | 128 _file.onClose = () { |
129 if (_closeHandler != null) _closeHandler(); | 129 if (_onClose != null) _onClose(); |
130 }; | 130 }; |
131 } | 131 } |
132 | 132 |
133 bool write(List<int> buffer, [bool copyBuffer = false]) { | 133 bool write(List<int> buffer, [bool copyBuffer = false]) { |
134 var data = buffer; | 134 var data = buffer; |
135 if (copyBuffer) { | 135 if (copyBuffer) { |
136 var length = buffer.length; | 136 var length = buffer.length; |
137 data = new ByteArray(length); | 137 data = new ByteArray(length); |
138 data.setRange(0, length, buffer, 0); | 138 data.setRange(0, length, buffer, 0); |
139 } | 139 } |
(...skipping 19 matching lines...) Expand all Loading... |
159 | 159 |
160 void close() { | 160 void close() { |
161 if (_file == null) { | 161 if (_file == null) { |
162 _pendingOperations.add(null); | 162 _pendingOperations.add(null); |
163 } else if (!_streamMarkedClosed) { | 163 } else if (!_streamMarkedClosed) { |
164 _file.close(); | 164 _file.close(); |
165 _streamMarkedClosed = true; | 165 _streamMarkedClosed = true; |
166 } | 166 } |
167 } | 167 } |
168 | 168 |
169 void set noPendingWriteHandler(void callback()) { | 169 void set onNoPendingWrite(void callback()) { |
170 _noPendingWriteHandler = callback; | 170 _onNoPendingWrite = callback; |
171 } | 171 } |
172 | 172 |
173 void set closeHandler(void callback()) { | 173 void set onClose(void callback()) { |
174 _closeHandler = callback; | 174 _onClose = callback; |
175 } | 175 } |
176 | 176 |
177 void set errorHandler(void callback()) { | 177 void set onError(void callback()) { |
178 _errorHandler = callback; | 178 _onError = callback; |
179 } | 179 } |
180 | 180 |
181 void _processPendingOperations() { | 181 void _processPendingOperations() { |
182 _pendingOperations.forEach((buffer) { | 182 _pendingOperations.forEach((buffer) { |
183 (buffer != null) ? write(buffer) : close(); | 183 (buffer != null) ? write(buffer) : close(); |
184 }); | 184 }); |
185 _pendingOperations = null; | 185 _pendingOperations = null; |
186 } | 186 } |
187 | 187 |
188 void _write(List<int> buffer, int offset, int len) { | 188 void _write(List<int> buffer, int offset, int len) { |
189 _file.writeList(buffer, offset, len); | 189 _file.writeList(buffer, offset, len); |
190 } | 190 } |
191 | 191 |
192 RandomAccessFile _file; | 192 RandomAccessFile _file; |
193 | 193 |
194 // When this is set to true the stream is marked closed. When a | 194 // When this is set to true the stream is marked closed. When a |
195 // stream is marked closed no more data can be written. | 195 // stream is marked closed no more data can be written. |
196 bool _streamMarkedClosed = false; | 196 bool _streamMarkedClosed = false; |
197 | 197 |
198 // When this is set to true the close callback has been called and | 198 // When this is set to true the close callback has been called and |
199 // the stream is fully closed. | 199 // the stream is fully closed. |
200 bool _closeCallbackCalled = false; | 200 bool _closeCallbackCalled = false; |
201 | 201 |
202 // List of pending writes that were issued before the underlying | 202 // List of pending writes that were issued before the underlying |
203 // file was successfully opened. | 203 // file was successfully opened. |
204 List<List<int>> _pendingOperations; | 204 List<List<int>> _pendingOperations; |
205 | 205 |
206 Function _noPendingWriteHandler; | 206 Function _onNoPendingWrite; |
207 Function _closeHandler; | 207 Function _onClose; |
208 Function _errorHandler; | 208 Function _onError; |
209 } | 209 } |
210 | 210 |
211 | 211 |
212 // Helper class containing static file helper methods. | 212 // Helper class containing static file helper methods. |
213 class _FileUtils { | 213 class _FileUtils { |
214 static final kExistsRequest = 0; | 214 static final kExistsRequest = 0; |
215 static final kCreateRequest = 1; | 215 static final kCreateRequest = 1; |
216 static final kDeleteRequest = 2; | 216 static final kDeleteRequest = 2; |
217 static final kOpenRequest = 3; | 217 static final kOpenRequest = 3; |
218 static final kFullPathRequest = 4; | 218 static final kFullPathRequest = 4; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 | 320 |
321 // Class for encapsulating the native implementation of files. | 321 // Class for encapsulating the native implementation of files. |
322 class _File implements File { | 322 class _File implements File { |
323 // Constructor for file. | 323 // Constructor for file. |
324 _File(String this._name) : _asyncUsed = false; | 324 _File(String this._name) : _asyncUsed = false; |
325 | 325 |
326 void exists() { | 326 void exists() { |
327 _ensureFileService(); | 327 _ensureFileService(); |
328 _asyncUsed = true; | 328 _asyncUsed = true; |
329 if (_name is !String) { | 329 if (_name is !String) { |
330 if (_errorHandler != null) { | 330 if (_onError != null) { |
331 _errorHandler('File name is not a string: $_name'); | 331 _onError('File name is not a string: $_name'); |
332 } | 332 } |
333 return; | 333 return; |
334 } | 334 } |
335 List request = new List(2); | 335 List request = new List(2); |
336 request[0] = _FileUtils.kExistsRequest; | 336 request[0] = _FileUtils.kExistsRequest; |
337 request[1] = _name; | 337 request[1] = _name; |
338 _fileService.call(request).receive((exists, replyTo) { | 338 _fileService.call(request).receive((exists, replyTo) { |
339 if (_existsHandler != null) _existsHandler(exists); | 339 if (_onExists != null) _onExists(exists); |
340 }); | 340 }); |
341 } | 341 } |
342 | 342 |
343 bool existsSync() { | 343 bool existsSync() { |
344 if (_asyncUsed) { | 344 if (_asyncUsed) { |
345 throw new FileIOException( | 345 throw new FileIOException( |
346 "Mixed use of synchronous and asynchronous API"); | 346 "Mixed use of synchronous and asynchronous API"); |
347 } | 347 } |
348 if (_name is !String) { | 348 if (_name is !String) { |
349 throw new FileIOException('File name is not a string: $_name'); | 349 throw new FileIOException('File name is not a string: $_name'); |
350 } | 350 } |
351 return _FileUtils.exists(_name); | 351 return _FileUtils.exists(_name); |
352 } | 352 } |
353 | 353 |
354 void create() { | 354 void create() { |
355 _ensureFileService(); | 355 _ensureFileService(); |
356 _asyncUsed = true; | 356 _asyncUsed = true; |
357 List request = new List(2); | 357 List request = new List(2); |
358 request[0] = _FileUtils.kCreateRequest; | 358 request[0] = _FileUtils.kCreateRequest; |
359 request[1] = _name; | 359 request[1] = _name; |
360 _fileService.call(request).receive((created, replyTo) { | 360 _fileService.call(request).receive((created, replyTo) { |
361 if (created) { | 361 if (created) { |
362 if (_createHandler != null) _createHandler(); | 362 if (_onCreate != null) _onCreate(); |
363 } else if (_errorHandler != null) { | 363 } else if (_onError != null) { |
364 _errorHandler("Cannot create file: $_name"); | 364 _onError("Cannot create file: $_name"); |
365 } | 365 } |
366 }); | 366 }); |
367 } | 367 } |
368 | 368 |
369 void createSync() { | 369 void createSync() { |
370 if (_asyncUsed) { | 370 if (_asyncUsed) { |
371 throw new FileIOException( | 371 throw new FileIOException( |
372 "Mixed use of synchronous and asynchronous API"); | 372 "Mixed use of synchronous and asynchronous API"); |
373 } | 373 } |
374 bool created = _FileUtils.checkedCreate(_name); | 374 bool created = _FileUtils.checkedCreate(_name); |
375 if (!created) { | 375 if (!created) { |
376 throw new FileIOException("Cannot create file: $_name"); | 376 throw new FileIOException("Cannot create file: $_name"); |
377 } | 377 } |
378 } | 378 } |
379 | 379 |
380 void delete() { | 380 void delete() { |
381 _ensureFileService(); | 381 _ensureFileService(); |
382 _asyncUsed = true; | 382 _asyncUsed = true; |
383 List request = new List(2); | 383 List request = new List(2); |
384 request[0] = _FileUtils.kDeleteRequest; | 384 request[0] = _FileUtils.kDeleteRequest; |
385 request[1] = _name; | 385 request[1] = _name; |
386 _fileService.call(request).receive((deleted, replyTo) { | 386 _fileService.call(request).receive((deleted, replyTo) { |
387 if (deleted) { | 387 if (deleted) { |
388 if (_deleteHandler != null) _deleteHandler(); | 388 if (_onDelete != null) _onDelete(); |
389 } else if (_errorHandler != null) { | 389 } else if (_onError != null) { |
390 _errorHandler("Cannot delete file: $_name"); | 390 _onError("Cannot delete file: $_name"); |
391 } | 391 } |
392 }); | 392 }); |
393 } | 393 } |
394 | 394 |
395 void deleteSync() { | 395 void deleteSync() { |
396 if (_asyncUsed) { | 396 if (_asyncUsed) { |
397 throw new FileIOException( | 397 throw new FileIOException( |
398 "Mixed use of synchronous and asynchronous API"); | 398 "Mixed use of synchronous and asynchronous API"); |
399 } | 399 } |
400 bool deleted = _FileUtils.checkedDelete(_name); | 400 bool deleted = _FileUtils.checkedDelete(_name); |
401 if (!deleted) { | 401 if (!deleted) { |
402 throw new FileIOException("Cannot delete file: $_name"); | 402 throw new FileIOException("Cannot delete file: $_name"); |
403 } | 403 } |
404 } | 404 } |
405 | 405 |
406 void directory() { | 406 void directory() { |
407 _ensureFileService(); | 407 _ensureFileService(); |
408 _asyncUsed = true; | 408 _asyncUsed = true; |
409 List request = new List(2); | 409 List request = new List(2); |
410 request[0] = _FileUtils.kDirectoryRequest; | 410 request[0] = _FileUtils.kDirectoryRequest; |
411 request[1] = _name; | 411 request[1] = _name; |
412 _fileService.call(request).receive((path, replyTo) { | 412 _fileService.call(request).receive((path, replyTo) { |
413 if (path != null) { | 413 if (path != null) { |
414 if (_directoryHandler != null) _directoryHandler(new Directory(path)); | 414 if (_onDirectory != null) _onDirectory(new Directory(path)); |
415 } else if (_errorHandler != null) { | 415 } else if (_onError != null) { |
416 _errorHandler("Cannot get directory for: ${_name}"); | 416 _onError("Cannot get directory for: ${_name}"); |
417 } | 417 } |
418 }); | 418 }); |
419 } | 419 } |
420 | 420 |
421 Directory directorySync() { | 421 Directory directorySync() { |
422 if (_asyncUsed) { | 422 if (_asyncUsed) { |
423 throw new FileIOException( | 423 throw new FileIOException( |
424 "Mixed use of synchronous and asynchronous API"); | 424 "Mixed use of synchronous and asynchronous API"); |
425 } | 425 } |
426 if (!existsSync()) { | 426 if (!existsSync()) { |
427 throw new FileIOException("Cannot get directory for: $_name"); | 427 throw new FileIOException("Cannot get directory for: $_name"); |
428 } | 428 } |
429 return new Directory(_FileUtils.directory(_name)); | 429 return new Directory(_FileUtils.directory(_name)); |
430 } | 430 } |
431 | 431 |
432 void open([FileMode mode = FileMode.READ]) { | 432 void open([FileMode mode = FileMode.READ]) { |
433 _ensureFileService(); | 433 _ensureFileService(); |
434 _asyncUsed = true; | 434 _asyncUsed = true; |
435 if (mode != FileMode.READ && | 435 if (mode != FileMode.READ && |
436 mode != FileMode.WRITE && | 436 mode != FileMode.WRITE && |
437 mode != FileMode.APPEND) { | 437 mode != FileMode.APPEND) { |
438 if (_errorHandler != null) { | 438 if (_onError != null) { |
439 _errorHandler("Unknown file mode. Use FileMode.READ, FileMode.WRITE " + | 439 _onError("Unknown file mode. Use FileMode.READ, FileMode.WRITE " + |
440 "or FileMode.APPEND."); | 440 "or FileMode.APPEND."); |
441 return; | 441 return; |
442 } | 442 } |
443 } | 443 } |
444 List request = new List(3); | 444 List request = new List(3); |
445 request[0] = _FileUtils.kOpenRequest; | 445 request[0] = _FileUtils.kOpenRequest; |
446 request[1] = _name; | 446 request[1] = _name; |
447 request[2] = mode._mode; // Direct int value for serialization. | 447 request[2] = mode._mode; // Direct int value for serialization. |
448 _fileService.call(request).receive((id, replyTo) { | 448 _fileService.call(request).receive((id, replyTo) { |
449 var handler = _openHandler; | 449 var handler = _onOpen; |
450 if (handler === null) { | 450 if (handler === null) { |
451 // If no open handler is present, close the file immediately to | 451 // If no open handler is present, close the file immediately to |
452 // avoid leaking an open file descriptor. | 452 // avoid leaking an open file descriptor. |
453 handler = (file) => file.close(); | 453 handler = (file) => file.close(); |
454 } | 454 } |
455 if (id != 0) { | 455 if (id != 0) { |
456 var randomAccessFile = new _RandomAccessFile(id, _name); | 456 var randomAccessFile = new _RandomAccessFile(id, _name); |
457 handler(randomAccessFile); | 457 handler(randomAccessFile); |
458 } else if (_errorHandler != null) { | 458 } else if (_onError != null) { |
459 _errorHandler("Cannot open file: $_name"); | 459 _onError("Cannot open file: $_name"); |
460 } | 460 } |
461 }); | 461 }); |
462 } | 462 } |
463 | 463 |
464 RandomAccessFile openSync([FileMode mode = FileMode.READ]) { | 464 RandomAccessFile openSync([FileMode mode = FileMode.READ]) { |
465 if (_asyncUsed) { | 465 if (_asyncUsed) { |
466 throw new FileIOException( | 466 throw new FileIOException( |
467 "Mixed use of synchronous and asynchronous API"); | 467 "Mixed use of synchronous and asynchronous API"); |
468 } | 468 } |
469 if (mode != FileMode.READ && | 469 if (mode != FileMode.READ && |
(...skipping 18 matching lines...) Expand all Loading... |
488 } | 488 } |
489 | 489 |
490 void fullPath() { | 490 void fullPath() { |
491 _ensureFileService(); | 491 _ensureFileService(); |
492 _asyncUsed = true; | 492 _asyncUsed = true; |
493 List request = new List(2); | 493 List request = new List(2); |
494 request[0] = _FileUtils.kFullPathRequest; | 494 request[0] = _FileUtils.kFullPathRequest; |
495 request[1] = _name; | 495 request[1] = _name; |
496 _fileService.call(request).receive((result, replyTo) { | 496 _fileService.call(request).receive((result, replyTo) { |
497 if (result != null) { | 497 if (result != null) { |
498 if (_fullPathHandler != null) _fullPathHandler(result); | 498 if (_onFullPath != null) _onFullPath(result); |
499 } else if (_errorHandler != null) { | 499 } else if (_onError != null) { |
500 _errorHandler("fullPath failed"); | 500 _onError("fullPath failed"); |
501 } | 501 } |
502 }); | 502 }); |
503 } | 503 } |
504 | 504 |
505 String fullPathSync() { | 505 String fullPathSync() { |
506 if (_asyncUsed) { | 506 if (_asyncUsed) { |
507 throw new FileIOException( | 507 throw new FileIOException( |
508 "Mixed use of synchronous and asynchronous API"); | 508 "Mixed use of synchronous and asynchronous API"); |
509 } | 509 } |
510 String result = _FileUtils.checkedFullPath(_name); | 510 String result = _FileUtils.checkedFullPath(_name); |
(...skipping 13 matching lines...) Expand all Loading... |
524 throw new FileIOException( | 524 throw new FileIOException( |
525 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND"); | 525 "Wrong FileMode. Use FileMode.WRITE or FileMode.APPEND"); |
526 } | 526 } |
527 return new _FileOutputStream(_name, mode); | 527 return new _FileOutputStream(_name, mode); |
528 } | 528 } |
529 | 529 |
530 void readAsBytes() { | 530 void readAsBytes() { |
531 _asyncUsed = true; | 531 _asyncUsed = true; |
532 var chunks = new _BufferList(); | 532 var chunks = new _BufferList(); |
533 var stream = openInputStream(); | 533 var stream = openInputStream(); |
534 stream.closeHandler = () { | 534 stream.onClose = () { |
535 if (_readAsBytesHandler != null) { | 535 if (_onReadAsBytes != null) { |
536 _readAsBytesHandler(chunks.readBytes(chunks.length)); | 536 _onReadAsBytes(chunks.readBytes(chunks.length)); |
537 } | 537 } |
538 }; | 538 }; |
539 stream.dataHandler = () { | 539 stream.onData = () { |
540 var chunk = stream.read(); | 540 var chunk = stream.read(); |
541 chunks.add(chunk); | 541 chunks.add(chunk); |
542 }; | 542 }; |
543 stream.errorHandler = () { | 543 stream.onError = () { |
544 if (_errorHandler != null) { | 544 if (_onError != null) { |
545 _errorHandler("Failed to read file as bytes: $_name"); | 545 _onError("Failed to read file as bytes: $_name"); |
546 } | 546 } |
547 }; | 547 }; |
548 } | 548 } |
549 | 549 |
550 List<int> readAsBytesSync() { | 550 List<int> readAsBytesSync() { |
551 if (_asyncUsed) { | 551 if (_asyncUsed) { |
552 throw new FileIOException( | 552 throw new FileIOException( |
553 "Mixed use of synchronous and asynchronous API"); | 553 "Mixed use of synchronous and asynchronous API"); |
554 } | 554 } |
555 var opened = openSync(); | 555 var opened = openSync(); |
(...skipping 15 matching lines...) Expand all Loading... |
571 } else if (encoding == "ASCII") { | 571 } else if (encoding == "ASCII") { |
572 return new _AsciiDecoder(); | 572 return new _AsciiDecoder(); |
573 } | 573 } |
574 throw new FileIOException("Unsupported encoding $_encoding"); | 574 throw new FileIOException("Unsupported encoding $_encoding"); |
575 } | 575 } |
576 | 576 |
577 void readAsText([String encoding = "UTF-8"]) { | 577 void readAsText([String encoding = "UTF-8"]) { |
578 _asyncUsed = true; | 578 _asyncUsed = true; |
579 var decoder = _getDecoder(encoding); | 579 var decoder = _getDecoder(encoding); |
580 readAsBytes(); | 580 readAsBytes(); |
581 readAsBytesHandler = (bytes) { | 581 onReadAsBytes = (bytes) { |
582 if (_readAsTextHandler != null) { | 582 if (_onReadAsText != null) { |
583 try { | 583 try { |
584 decoder.write(bytes); | 584 decoder.write(bytes); |
585 } catch (var e) { | 585 } catch (var e) { |
586 if (_errorHandler != null) { | 586 if (_onError != null) { |
587 _errorHandler(e.toString()); | 587 _onError(e.toString()); |
588 return; | 588 return; |
589 } | 589 } |
590 } | 590 } |
591 _readAsTextHandler(decoder.decoded); | 591 _onReadAsText(decoder.decoded); |
592 } | 592 } |
593 }; | 593 }; |
594 } | 594 } |
595 | 595 |
596 String readAsTextSync([String encoding = "UTF-8"]) { | 596 String readAsTextSync([String encoding = "UTF-8"]) { |
597 if (_asyncUsed) { | 597 if (_asyncUsed) { |
598 throw new FileIOException( | 598 throw new FileIOException( |
599 "Mixed use of synchronous and asynchronous API"); | 599 "Mixed use of synchronous and asynchronous API"); |
600 } | 600 } |
601 var decoder = _getDecoder(encoding); | 601 var decoder = _getDecoder(encoding); |
(...skipping 15 matching lines...) Expand all Loading... |
617 if (data != null) { | 617 if (data != null) { |
618 result.add(data); | 618 result.add(data); |
619 } | 619 } |
620 return result; | 620 return result; |
621 } | 621 } |
622 | 622 |
623 void readAsLines([String encoding = "UTF-8"]) { | 623 void readAsLines([String encoding = "UTF-8"]) { |
624 _asyncUsed = true; | 624 _asyncUsed = true; |
625 var decoder = _getDecoder(encoding); | 625 var decoder = _getDecoder(encoding); |
626 readAsBytes(); | 626 readAsBytes(); |
627 readAsBytesHandler = (bytes) { | 627 onReadAsBytes = (bytes) { |
628 if (_readAsLinesHandler != null) { | 628 if (_onReadAsLines != null) { |
629 try { | 629 try { |
630 decoder.write(bytes); | 630 decoder.write(bytes); |
631 } catch (var e) { | 631 } catch (var e) { |
632 if (_errorHandler != null) { | 632 if (_onError != null) { |
633 _errorHandler(e.toString()); | 633 _onError(e.toString()); |
634 return; | 634 return; |
635 } | 635 } |
636 } | 636 } |
637 _readAsLinesHandler(_getDecodedLines(decoder)); | 637 _onReadAsLines(_getDecodedLines(decoder)); |
638 } | 638 } |
639 }; | 639 }; |
640 } | 640 } |
641 | 641 |
642 List<String> readAsLinesSync([String encoding = "UTF-8"]) { | 642 List<String> readAsLinesSync([String encoding = "UTF-8"]) { |
643 if (_asyncUsed) { | 643 if (_asyncUsed) { |
644 throw new FileIOException( | 644 throw new FileIOException( |
645 "Mixed use of synchronous and asynchronous API"); | 645 "Mixed use of synchronous and asynchronous API"); |
646 } | 646 } |
647 var decoder = _getDecoder(encoding); | 647 var decoder = _getDecoder(encoding); |
648 List<int> bytes = readAsBytesSync(); | 648 List<int> bytes = readAsBytesSync(); |
649 decoder.write(bytes); | 649 decoder.write(bytes); |
650 return _getDecodedLines(decoder); | 650 return _getDecodedLines(decoder); |
651 } | 651 } |
652 | 652 |
653 String get name() => _name; | 653 String get name() => _name; |
654 | 654 |
655 void set existsHandler(void handler(bool exists)) { | 655 void set onExists(void handler(bool exists)) { |
656 _existsHandler = handler; | 656 _onExists = handler; |
657 } | 657 } |
658 | 658 |
659 void set createHandler(void handler()) { | 659 void set onCreate(void handler()) { |
660 _createHandler = handler; | 660 _onCreate = handler; |
661 } | 661 } |
662 | 662 |
663 void set deleteHandler(void handler()) { | 663 void set onDelete(void handler()) { |
664 _deleteHandler = handler; | 664 _onDelete = handler; |
665 } | 665 } |
666 | 666 |
667 void set directoryHandler(void handler(Directory directory)) { | 667 void set onDirectory(void handler(Directory directory)) { |
668 _directoryHandler = handler; | 668 _onDirectory = handler; |
669 } | 669 } |
670 | 670 |
671 void set openHandler(void handler(RandomAccessFile file)) { | 671 void set onOpen(void handler(RandomAccessFile file)) { |
672 _openHandler = handler; | 672 _onOpen = handler; |
673 } | 673 } |
674 | 674 |
675 void set readAsBytesHandler(void handler(List<int> bytes)) { | 675 void set onReadAsBytes(void handler(List<int> bytes)) { |
676 _readAsBytesHandler = handler; | 676 _onReadAsBytes = handler; |
677 } | 677 } |
678 | 678 |
679 void set readAsTextHandler(void handler(String text)) { | 679 void set onReadAsText(void handler(String text)) { |
680 _readAsTextHandler = handler; | 680 _onReadAsText = handler; |
681 } | 681 } |
682 | 682 |
683 void set readAsLinesHandler(void handler(List<String> lines)) { | 683 void set onReadAsLines(void handler(List<String> lines)) { |
684 _readAsLinesHandler = handler; | 684 _onReadAsLines = handler; |
685 } | 685 } |
686 | 686 |
687 void set fullPathHandler(void handler(String)) { | 687 void set onFullPath(void handler(String)) { |
688 _fullPathHandler = handler; | 688 _onFullPath = handler; |
689 } | 689 } |
690 | 690 |
691 void set errorHandler(void handler(String error)) { | 691 void set onError(void handler(String error)) { |
692 _errorHandler = handler; | 692 _onError = handler; |
693 } | 693 } |
694 | 694 |
695 void _ensureFileService() { | 695 void _ensureFileService() { |
696 if (_fileService == null) { | 696 if (_fileService == null) { |
697 _fileService = _FileUtils.newServicePort(); | 697 _fileService = _FileUtils.newServicePort(); |
698 } | 698 } |
699 } | 699 } |
700 | 700 |
701 String _name; | 701 String _name; |
702 bool _asyncUsed; | 702 bool _asyncUsed; |
703 | 703 |
704 SendPort _fileService; | 704 SendPort _fileService; |
705 | 705 |
706 Function _existsHandler; | 706 Function _onExists; |
707 Function _createHandler; | 707 Function _onCreate; |
708 Function _deleteHandler; | 708 Function _onDelete; |
709 Function _directoryHandler; | 709 Function _onDirectory; |
710 Function _openHandler; | 710 Function _onOpen; |
711 Function _readAsBytesHandler; | 711 Function _onReadAsBytes; |
712 Function _readAsTextHandler; | 712 Function _onReadAsText; |
713 Function _readAsLinesHandler; | 713 Function _onReadAsLines; |
714 Function _fullPathHandler; | 714 Function _onFullPath; |
715 Function _errorHandler; | 715 Function _onError; |
716 } | 716 } |
717 | 717 |
718 | 718 |
719 class _RandomAccessFile implements RandomAccessFile { | 719 class _RandomAccessFile implements RandomAccessFile { |
720 _RandomAccessFile(int this._id, String this._name) : _asyncUsed = false; | 720 _RandomAccessFile(int this._id, String this._name) : _asyncUsed = false; |
721 | 721 |
722 void close() { | 722 void close() { |
723 if (_id == 0) return; | 723 if (_id == 0) return; |
724 _ensureFileService(); | 724 _ensureFileService(); |
725 _asyncUsed = true; | 725 _asyncUsed = true; |
726 List request = new List(2); | 726 List request = new List(2); |
727 request[0] = _FileUtils.kCloseRequest; | 727 request[0] = _FileUtils.kCloseRequest; |
728 request[1] = _id; | 728 request[1] = _id; |
729 // Set the id_ to 0 (NULL) to ensure the no more async requests | 729 // Set the id_ to 0 (NULL) to ensure the no more async requests |
730 // can be issues for this file. | 730 // can be issues for this file. |
731 _id = 0; | 731 _id = 0; |
732 _fileService.call(request).receive((result, replyTo) { | 732 _fileService.call(request).receive((result, replyTo) { |
733 if (result != -1) { | 733 if (result != -1) { |
734 _id = result; | 734 _id = result; |
735 if (_closeHandler != null) _closeHandler(); | 735 if (_onClose != null) _onClose(); |
736 } else if (_errorHandler != null) { | 736 } else if (_onError != null) { |
737 _errorHandler("Cannot close file: $_name"); | 737 _onError("Cannot close file: $_name"); |
738 } | 738 } |
739 }); | 739 }); |
740 } | 740 } |
741 | 741 |
742 void closeSync() { | 742 void closeSync() { |
743 if (_asyncUsed) { | 743 if (_asyncUsed) { |
744 throw new FileIOException( | 744 throw new FileIOException( |
745 "Mixed use of synchronous and asynchronous API"); | 745 "Mixed use of synchronous and asynchronous API"); |
746 } | 746 } |
747 var id = _FileUtils.close(_id); | 747 var id = _FileUtils.close(_id); |
748 if (id == -1) { | 748 if (id == -1) { |
749 throw new FileIOException("Cannot close file: $_name"); | 749 throw new FileIOException("Cannot close file: $_name"); |
750 } | 750 } |
751 _id = id; | 751 _id = id; |
752 } | 752 } |
753 | 753 |
754 void readByte() { | 754 void readByte() { |
755 _ensureFileService(); | 755 _ensureFileService(); |
756 _asyncUsed = true; | 756 _asyncUsed = true; |
757 List request = new List(2); | 757 List request = new List(2); |
758 request[0] = _FileUtils.kReadByteRequest; | 758 request[0] = _FileUtils.kReadByteRequest; |
759 request[1] = _id; | 759 request[1] = _id; |
760 _fileService.call(request).receive((result, replyTo) { | 760 _fileService.call(request).receive((result, replyTo) { |
761 if (result != -1) { | 761 if (result != -1) { |
762 if (_readByteHandler != null) _readByteHandler(result); | 762 if (_onReadByte != null) _onReadByte(result); |
763 } else if (_errorHandler != null) { | 763 } else if (_onError != null) { |
764 _errorHandler("readByte failed"); | 764 _onError("readByte failed"); |
765 } | 765 } |
766 }); | 766 }); |
767 } | 767 } |
768 | 768 |
769 int readByteSync() { | 769 int readByteSync() { |
770 if (_asyncUsed) { | 770 if (_asyncUsed) { |
771 throw new FileIOException( | 771 throw new FileIOException( |
772 "Mixed use of synchronous and asynchronous API"); | 772 "Mixed use of synchronous and asynchronous API"); |
773 } | 773 } |
774 int result = _FileUtils.readByte(_id); | 774 int result = _FileUtils.readByte(_id); |
775 if (result == -1) { | 775 if (result == -1) { |
776 throw new FileIOException("readByte failed"); | 776 throw new FileIOException("readByte failed"); |
777 } | 777 } |
778 return result; | 778 return result; |
779 } | 779 } |
780 | 780 |
781 void readList(List<int> buffer, int offset, int bytes) { | 781 void readList(List<int> buffer, int offset, int bytes) { |
782 _ensureFileService(); | 782 _ensureFileService(); |
783 _asyncUsed = true; | 783 _asyncUsed = true; |
784 if (buffer is !List || offset is !int || bytes is !int) { | 784 if (buffer is !List || offset is !int || bytes is !int) { |
785 if (_errorHandler != null) { | 785 if (_onError != null) { |
786 _errorHandler("Invalid arguments to readList"); | 786 _onError("Invalid arguments to readList"); |
787 } | 787 } |
788 return; | 788 return; |
789 }; | 789 }; |
790 List request = new List(3); | 790 List request = new List(3); |
791 request[0] = _FileUtils.kReadListRequest; | 791 request[0] = _FileUtils.kReadListRequest; |
792 request[1] = _id; | 792 request[1] = _id; |
793 request[2] = bytes; | 793 request[2] = bytes; |
794 _fileService.call(request).receive((result, replyTo) { | 794 _fileService.call(request).receive((result, replyTo) { |
795 if (result is List && result.length == 2 && result[0] != -1) { | 795 if (result is List && result.length == 2 && result[0] != -1) { |
796 var read = result[0]; | 796 var read = result[0]; |
797 var data = result[1]; | 797 var data = result[1]; |
798 buffer.setRange(offset, read, data); | 798 buffer.setRange(offset, read, data); |
799 if (_readListHandler != null) _readListHandler(read); | 799 if (_onReadList != null) _onReadList(read); |
800 return; | 800 return; |
801 } else if (_errorHandler != null) { | 801 } else if (_onError != null) { |
802 _errorHandler(result is String ? result : "readList failed"); | 802 _onError(result is String ? result : "readList failed"); |
803 } | 803 } |
804 }); | 804 }); |
805 } | 805 } |
806 | 806 |
807 int readListSync(List<int> buffer, int offset, int bytes) { | 807 int readListSync(List<int> buffer, int offset, int bytes) { |
808 if (_asyncUsed) { | 808 if (_asyncUsed) { |
809 throw new FileIOException( | 809 throw new FileIOException( |
810 "Mixed use of synchronous and asynchronous API"); | 810 "Mixed use of synchronous and asynchronous API"); |
811 } | 811 } |
812 if (buffer is !List || offset is !int || bytes is !int) { | 812 if (buffer is !List || offset is !int || bytes is !int) { |
813 throw new FileIOException("Invalid arguments to readList"); | 813 throw new FileIOException("Invalid arguments to readList"); |
814 } | 814 } |
815 if (bytes == 0) return 0; | 815 if (bytes == 0) return 0; |
816 int index = | 816 int index = |
817 _FileUtils.checkReadWriteListArguments(buffer.length, offset, bytes); | 817 _FileUtils.checkReadWriteListArguments(buffer.length, offset, bytes); |
818 if (index != 0) { | 818 if (index != 0) { |
819 throw new IndexOutOfRangeException(index); | 819 throw new IndexOutOfRangeException(index); |
820 } | 820 } |
821 int result = _FileUtils.readList(_id, buffer, offset, bytes); | 821 int result = _FileUtils.readList(_id, buffer, offset, bytes); |
822 if (result == -1) { | 822 if (result == -1) { |
823 throw new FileIOException("readList failed"); | 823 throw new FileIOException("readList failed"); |
824 } | 824 } |
825 return result; | 825 return result; |
826 } | 826 } |
827 | 827 |
828 void writeByte(int value) { | 828 void writeByte(int value) { |
829 _ensureFileService(); | 829 _ensureFileService(); |
830 _asyncUsed = true; | 830 _asyncUsed = true; |
831 if (value is !int) { | 831 if (value is !int) { |
832 if (_errorHandler != null) { | 832 if (_onError != null) { |
833 _errorHandler("Invalid argument to writeByte"); | 833 _onError("Invalid argument to writeByte"); |
834 } | 834 } |
835 return; | 835 return; |
836 } | 836 } |
837 List request = new List(3); | 837 List request = new List(3); |
838 request[0] = _FileUtils.kWriteByteRequest; | 838 request[0] = _FileUtils.kWriteByteRequest; |
839 request[1] = _id; | 839 request[1] = _id; |
840 request[2] = value; | 840 request[2] = value; |
841 _writeEnqueued(); | 841 _writeEnqueued(); |
842 _fileService.call(request).receive((result, replyTo) { | 842 _fileService.call(request).receive((result, replyTo) { |
843 _writeCompleted(); | 843 _writeCompleted(); |
844 if (result == -1 && _errorHandler !== null) { | 844 if (result == -1 && _onError !== null) { |
845 _errorHandler("writeByte failed"); | 845 _onError("writeByte failed"); |
846 } | 846 } |
847 }); | 847 }); |
848 } | 848 } |
849 | 849 |
850 int writeByteSync(int value) { | 850 int writeByteSync(int value) { |
851 if (_asyncUsed) { | 851 if (_asyncUsed) { |
852 throw new FileIOException( | 852 throw new FileIOException( |
853 "Mixed use of synchronous and asynchronous API"); | 853 "Mixed use of synchronous and asynchronous API"); |
854 } | 854 } |
855 if (value is !int) { | 855 if (value is !int) { |
856 throw new FileIOException("Invalid argument to writeByte"); | 856 throw new FileIOException("Invalid argument to writeByte"); |
857 } | 857 } |
858 int result = _FileUtils.writeByte(_id, value); | 858 int result = _FileUtils.writeByte(_id, value); |
859 if (result == -1) { | 859 if (result == -1) { |
860 throw new FileIOException("writeByte failed"); | 860 throw new FileIOException("writeByte failed"); |
861 } | 861 } |
862 return result; | 862 return result; |
863 } | 863 } |
864 | 864 |
865 void writeList(List<int> buffer, int offset, int bytes) { | 865 void writeList(List<int> buffer, int offset, int bytes) { |
866 _ensureFileService(); | 866 _ensureFileService(); |
867 _asyncUsed = true; | 867 _asyncUsed = true; |
868 if (buffer is !List || offset is !int || bytes is !int) { | 868 if (buffer is !List || offset is !int || bytes is !int) { |
869 if (_errorHandler != null) { | 869 if (_onError != null) { |
870 _errorHandler("Invalid arguments to writeList"); | 870 _onError("Invalid arguments to writeList"); |
871 } | 871 } |
872 return; | 872 return; |
873 } | 873 } |
874 | 874 |
875 List result = | 875 List result = |
876 _FileUtils.ensureFastAndSerializableBuffer(buffer, offset, bytes); | 876 _FileUtils.ensureFastAndSerializableBuffer(buffer, offset, bytes); |
877 List outBuffer = result[0]; | 877 List outBuffer = result[0]; |
878 int outOffset = result[1]; | 878 int outOffset = result[1]; |
879 | 879 |
880 List request = new List(5); | 880 List request = new List(5); |
881 request[0] = _FileUtils.kWriteListRequest; | 881 request[0] = _FileUtils.kWriteListRequest; |
882 request[1] = _id; | 882 request[1] = _id; |
883 request[2] = outBuffer; | 883 request[2] = outBuffer; |
884 request[3] = outOffset; | 884 request[3] = outOffset; |
885 request[4] = bytes; | 885 request[4] = bytes; |
886 _writeEnqueued(); | 886 _writeEnqueued(); |
887 _fileService.call(request).receive((result, replyTo) { | 887 _fileService.call(request).receive((result, replyTo) { |
888 _writeCompleted(); | 888 _writeCompleted(); |
889 if (result == -1 && _errorHandler !== null) { | 889 if (result == -1 && _onError !== null) { |
890 _errorHandler("writeList failed"); | 890 _onError("writeList failed"); |
891 } | 891 } |
892 }); | 892 }); |
893 } | 893 } |
894 | 894 |
895 int writeListSync(List<int> buffer, int offset, int bytes) { | 895 int writeListSync(List<int> buffer, int offset, int bytes) { |
896 if (_asyncUsed) { | 896 if (_asyncUsed) { |
897 throw new FileIOException( | 897 throw new FileIOException( |
898 "Mixed use of synchronous and asynchronous API"); | 898 "Mixed use of synchronous and asynchronous API"); |
899 } | 899 } |
900 if (buffer is !List || offset is !int || bytes is !int) { | 900 if (buffer is !List || offset is !int || bytes is !int) { |
(...skipping 15 matching lines...) Expand all Loading... |
916 void writeString(String string) { | 916 void writeString(String string) { |
917 _ensureFileService(); | 917 _ensureFileService(); |
918 _asyncUsed = true; | 918 _asyncUsed = true; |
919 List request = new List(3); | 919 List request = new List(3); |
920 request[0] = _FileUtils.kWriteStringRequest; | 920 request[0] = _FileUtils.kWriteStringRequest; |
921 request[1] = _id; | 921 request[1] = _id; |
922 request[2] = string; | 922 request[2] = string; |
923 _writeEnqueued(); | 923 _writeEnqueued(); |
924 _fileService.call(request).receive((result, replyTo) { | 924 _fileService.call(request).receive((result, replyTo) { |
925 _writeCompleted(); | 925 _writeCompleted(); |
926 if (result == -1 && _errorHandler !== null) { | 926 if (result == -1 && _onError !== null) { |
927 _errorHandler("writeString failed"); | 927 _onError("writeString failed"); |
928 } | 928 } |
929 }); | 929 }); |
930 } | 930 } |
931 | 931 |
932 int writeStringSync(String string) { | 932 int writeStringSync(String string) { |
933 if (_asyncUsed) { | 933 if (_asyncUsed) { |
934 throw new FileIOException( | 934 throw new FileIOException( |
935 "Mixed use of synchronous and asynchronous API"); | 935 "Mixed use of synchronous and asynchronous API"); |
936 } | 936 } |
937 int result = _FileUtils.checkedWriteString(_id, string); | 937 int result = _FileUtils.checkedWriteString(_id, string); |
938 if (result == -1) { | 938 if (result == -1) { |
939 throw new FileIOException("writeString failed"); | 939 throw new FileIOException("writeString failed"); |
940 } | 940 } |
941 return result; | 941 return result; |
942 } | 942 } |
943 | 943 |
944 void position() { | 944 void position() { |
945 _ensureFileService(); | 945 _ensureFileService(); |
946 _asyncUsed = true; | 946 _asyncUsed = true; |
947 List request = new List(2); | 947 List request = new List(2); |
948 request[0] = _FileUtils.kPositionRequest; | 948 request[0] = _FileUtils.kPositionRequest; |
949 request[1] = _id; | 949 request[1] = _id; |
950 _fileService.call(request).receive((result, replyTo) { | 950 _fileService.call(request).receive((result, replyTo) { |
951 if (result != -1) { | 951 if (result != -1) { |
952 if (_positionHandler != null) _positionHandler(result); | 952 if (_onPosition != null) _onPosition(result); |
953 } else if (_errorHandler != null) { | 953 } else if (_onError != null) { |
954 _errorHandler("position failed"); | 954 _onError("position failed"); |
955 } | 955 } |
956 }); | 956 }); |
957 } | 957 } |
958 | 958 |
959 int positionSync() { | 959 int positionSync() { |
960 if (_asyncUsed) { | 960 if (_asyncUsed) { |
961 throw new FileIOException( | 961 throw new FileIOException( |
962 "Mixed use of synchronous and asynchronous API"); | 962 "Mixed use of synchronous and asynchronous API"); |
963 } | 963 } |
964 int result = _FileUtils.position(_id); | 964 int result = _FileUtils.position(_id); |
965 if (result == -1) { | 965 if (result == -1) { |
966 throw new FileIOException("position failed"); | 966 throw new FileIOException("position failed"); |
967 } | 967 } |
968 return result; | 968 return result; |
969 } | 969 } |
970 | 970 |
971 void setPosition(int position) { | 971 void setPosition(int position) { |
972 _ensureFileService(); | 972 _ensureFileService(); |
973 _asyncUsed = true; | 973 _asyncUsed = true; |
974 List request = new List(3); | 974 List request = new List(3); |
975 request[0] = _FileUtils.kSetPositionRequest; | 975 request[0] = _FileUtils.kSetPositionRequest; |
976 request[1] = _id; | 976 request[1] = _id; |
977 request[2] = position; | 977 request[2] = position; |
978 _fileService.call(request).receive((result, replyTo) { | 978 _fileService.call(request).receive((result, replyTo) { |
979 if (result) { | 979 if (result) { |
980 if (_setPositionHandler != null) _setPositionHandler(); | 980 if (_onSetPosition != null) _onSetPosition(); |
981 } else if (_errorHandler != null) { | 981 } else if (_onError != null) { |
982 _errorHandler("setPosition failed"); | 982 _onError("setPosition failed"); |
983 } | 983 } |
984 }); | 984 }); |
985 } | 985 } |
986 | 986 |
987 void setPositionSync(int position) { | 987 void setPositionSync(int position) { |
988 _ensureFileService(); | 988 _ensureFileService(); |
989 if (_asyncUsed) { | 989 if (_asyncUsed) { |
990 throw new FileIOException( | 990 throw new FileIOException( |
991 "Mixed use of synchronous and asynchronous API"); | 991 "Mixed use of synchronous and asynchronous API"); |
992 } | 992 } |
993 bool result = _FileUtils.setPosition(_id, position); | 993 bool result = _FileUtils.setPosition(_id, position); |
994 if (result == false) { | 994 if (result == false) { |
995 throw new FileIOException("setPosition failed"); | 995 throw new FileIOException("setPosition failed"); |
996 } | 996 } |
997 } | 997 } |
998 | 998 |
999 void truncate(int length) { | 999 void truncate(int length) { |
1000 _ensureFileService(); | 1000 _ensureFileService(); |
1001 _asyncUsed = true; | 1001 _asyncUsed = true; |
1002 List request = new List(3); | 1002 List request = new List(3); |
1003 request[0] = _FileUtils.kTruncateRequest; | 1003 request[0] = _FileUtils.kTruncateRequest; |
1004 request[1] = _id; | 1004 request[1] = _id; |
1005 request[2] = length; | 1005 request[2] = length; |
1006 _fileService.call(request).receive((result, replyTo) { | 1006 _fileService.call(request).receive((result, replyTo) { |
1007 if (result) { | 1007 if (result) { |
1008 if (_truncateHandler != null) _truncateHandler(); | 1008 if (_onTruncate != null) _onTruncate(); |
1009 } else if (_errorHandler != null) { | 1009 } else if (_onError != null) { |
1010 _errorHandler("truncate failed"); | 1010 _onError("truncate failed"); |
1011 } | 1011 } |
1012 }); | 1012 }); |
1013 } | 1013 } |
1014 | 1014 |
1015 void truncateSync(int length) { | 1015 void truncateSync(int length) { |
1016 if (_asyncUsed) { | 1016 if (_asyncUsed) { |
1017 throw new FileIOException( | 1017 throw new FileIOException( |
1018 "Mixed use of synchronous and asynchronous API"); | 1018 "Mixed use of synchronous and asynchronous API"); |
1019 } | 1019 } |
1020 bool result = _FileUtils.truncate(_id, length); | 1020 bool result = _FileUtils.truncate(_id, length); |
1021 if (result == false) { | 1021 if (result == false) { |
1022 throw new FileIOException("truncate failed"); | 1022 throw new FileIOException("truncate failed"); |
1023 } | 1023 } |
1024 } | 1024 } |
1025 | 1025 |
1026 void length() { | 1026 void length() { |
1027 _ensureFileService(); | 1027 _ensureFileService(); |
1028 _asyncUsed = true; | 1028 _asyncUsed = true; |
1029 List request = new List(2); | 1029 List request = new List(2); |
1030 request[0] = _FileUtils.kLengthRequest; | 1030 request[0] = _FileUtils.kLengthRequest; |
1031 request[1] = _id; | 1031 request[1] = _id; |
1032 _fileService.call(request).receive((result, replyTo) { | 1032 _fileService.call(request).receive((result, replyTo) { |
1033 if (result != -1) { | 1033 if (result != -1) { |
1034 if (_lengthHandler != null) _lengthHandler(result); | 1034 if (_onLength != null) _onLength(result); |
1035 } else if (_errorHandler != null) { | 1035 } else if (_onError != null) { |
1036 _errorHandler("length failed"); | 1036 _onError("length failed"); |
1037 } | 1037 } |
1038 }); | 1038 }); |
1039 } | 1039 } |
1040 | 1040 |
1041 int lengthSync() { | 1041 int lengthSync() { |
1042 if (_asyncUsed) { | 1042 if (_asyncUsed) { |
1043 throw new FileIOException( | 1043 throw new FileIOException( |
1044 "Mixed use of synchronous and asynchronous API"); | 1044 "Mixed use of synchronous and asynchronous API"); |
1045 } | 1045 } |
1046 int result = _FileUtils.length(_id); | 1046 int result = _FileUtils.length(_id); |
1047 if (result == -1) { | 1047 if (result == -1) { |
1048 throw new FileIOException("length failed"); | 1048 throw new FileIOException("length failed"); |
1049 } | 1049 } |
1050 return result; | 1050 return result; |
1051 } | 1051 } |
1052 | 1052 |
1053 void flush() { | 1053 void flush() { |
1054 _ensureFileService(); | 1054 _ensureFileService(); |
1055 _asyncUsed = true; | 1055 _asyncUsed = true; |
1056 List request = new List(2); | 1056 List request = new List(2); |
1057 request[0] = _FileUtils.kFlushRequest; | 1057 request[0] = _FileUtils.kFlushRequest; |
1058 request[1] = _id; | 1058 request[1] = _id; |
1059 _fileService.call(request).receive((result, replyTo) { | 1059 _fileService.call(request).receive((result, replyTo) { |
1060 if (result != -1) { | 1060 if (result != -1) { |
1061 if (_flushHandler != null) _flushHandler(); | 1061 if (_onFlush != null) _onFlush(); |
1062 } else if (_errorHandler != null) { | 1062 } else if (_onError != null) { |
1063 _errorHandler("flush failed"); | 1063 _onError("flush failed"); |
1064 } | 1064 } |
1065 }); | 1065 }); |
1066 } | 1066 } |
1067 | 1067 |
1068 void flushSync() { | 1068 void flushSync() { |
1069 if (_asyncUsed) { | 1069 if (_asyncUsed) { |
1070 throw new FileIOException( | 1070 throw new FileIOException( |
1071 "Mixed use of synchronous and asynchronous API"); | 1071 "Mixed use of synchronous and asynchronous API"); |
1072 } | 1072 } |
1073 int result = _FileUtils.flush(_id); | 1073 int result = _FileUtils.flush(_id); |
1074 if (result == -1) { | 1074 if (result == -1) { |
1075 throw new FileIOException("flush failed"); | 1075 throw new FileIOException("flush failed"); |
1076 } | 1076 } |
1077 } | 1077 } |
1078 | 1078 |
1079 String get name() => _name; | 1079 String get name() => _name; |
1080 | 1080 |
1081 void set errorHandler(void handler(String error)) { | 1081 void set onError(void handler(String error)) { |
1082 _errorHandler = handler; | 1082 _onError = handler; |
1083 } | 1083 } |
1084 | 1084 |
1085 void set closeHandler(void handler()) { | 1085 void set onClose(void handler()) { |
1086 _closeHandler = handler; | 1086 _onClose = handler; |
1087 } | 1087 } |
1088 | 1088 |
1089 void set readByteHandler(void handler(int byte)) { | 1089 void set onReadByte(void handler(int byte)) { |
1090 _readByteHandler = handler; | 1090 _onReadByte = handler; |
1091 } | 1091 } |
1092 | 1092 |
1093 void set readListHandler(void handler(int read)) { | 1093 void set onReadList(void handler(int read)) { |
1094 _readListHandler = handler; | 1094 _onReadList = handler; |
1095 } | 1095 } |
1096 | 1096 |
1097 void set noPendingWriteHandler(void handler()) { | 1097 void set onNoPendingWrite(void handler()) { |
1098 _noPendingWriteHandler = handler; | 1098 _onNoPendingWrite = handler; |
1099 if (_pendingWrites == 0) { | 1099 if (_pendingWrites == 0) { |
1100 _noPendingWriteTimer = new Timer((t) { | 1100 _noPendingWriteTimer = new Timer((t) { |
1101 if (_noPendingWriteHandler != null) _noPendingWriteHandler(); | 1101 if (_onNoPendingWrite != null) _onNoPendingWrite(); |
1102 }, 0); | 1102 }, 0); |
1103 } | 1103 } |
1104 } | 1104 } |
1105 | 1105 |
1106 void set positionHandler(void handler(int pos)) { | 1106 void set onPosition(void handler(int pos)) { |
1107 _positionHandler = handler; | 1107 _onPosition = handler; |
1108 } | 1108 } |
1109 | 1109 |
1110 void set setPositionHandler(void handler()) { | 1110 void set onSetPosition(void handler()) { |
1111 _setPositionHandler = handler; | 1111 _onSetPosition = handler; |
1112 } | 1112 } |
1113 | 1113 |
1114 void set truncateHandler(void handler()) { | 1114 void set onTruncate(void handler()) { |
1115 _truncateHandler = handler; | 1115 _onTruncate = handler; |
1116 } | 1116 } |
1117 | 1117 |
1118 void set lengthHandler(void handler(int length)) { | 1118 void set onLength(void handler(int length)) { |
1119 _lengthHandler = handler; | 1119 _onLength = handler; |
1120 } | 1120 } |
1121 | 1121 |
1122 void set flushHandler(void handler()) { | 1122 void set onFlush(void handler()) { |
1123 _flushHandler = handler; | 1123 _onFlush = handler; |
1124 } | 1124 } |
1125 | 1125 |
1126 void _ensureFileService() { | 1126 void _ensureFileService() { |
1127 if (_fileService == null) { | 1127 if (_fileService == null) { |
1128 _fileService = _FileUtils.newServicePort(); | 1128 _fileService = _FileUtils.newServicePort(); |
1129 } | 1129 } |
1130 } | 1130 } |
1131 | 1131 |
1132 void _writeEnqueued() { | 1132 void _writeEnqueued() { |
1133 _pendingWrites++; | 1133 _pendingWrites++; |
1134 if (_noPendingWriteTimer != null) { | 1134 if (_noPendingWriteTimer != null) { |
1135 _noPendingWriteTimer.cancel(); | 1135 _noPendingWriteTimer.cancel(); |
1136 _noPendingWriteTimer = null; | 1136 _noPendingWriteTimer = null; |
1137 } | 1137 } |
1138 } | 1138 } |
1139 | 1139 |
1140 void _writeCompleted() { | 1140 void _writeCompleted() { |
1141 _pendingWrites--; | 1141 _pendingWrites--; |
1142 if (_pendingWrites == 0 && _noPendingWriteHandler != null) { | 1142 if (_pendingWrites == 0 && _onNoPendingWrite != null) { |
1143 _noPendingWriteHandler(); | 1143 _onNoPendingWrite(); |
1144 } | 1144 } |
1145 } | 1145 } |
1146 | 1146 |
1147 | 1147 |
1148 String _name; | 1148 String _name; |
1149 int _id; | 1149 int _id; |
1150 bool _asyncUsed; | 1150 bool _asyncUsed; |
1151 int _pendingWrites = 0; | 1151 int _pendingWrites = 0; |
1152 | 1152 |
1153 SendPort _fileService; | 1153 SendPort _fileService; |
1154 | 1154 |
1155 Timer _noPendingWriteTimer; | 1155 Timer _noPendingWriteTimer; |
1156 | 1156 |
1157 Function _closeHandler; | 1157 Function _onClose; |
1158 Function _readByteHandler; | 1158 Function _onReadByte; |
1159 Function _readListHandler; | 1159 Function _onReadList; |
1160 Function _noPendingWriteHandler; | 1160 Function _onNoPendingWrite; |
1161 Function _positionHandler; | 1161 Function _onPosition; |
1162 Function _setPositionHandler; | 1162 Function _onSetPosition; |
1163 Function _truncateHandler; | 1163 Function _onTruncate; |
1164 Function _lengthHandler; | 1164 Function _onLength; |
1165 Function _flushHandler; | 1165 Function _onFlush; |
1166 Function _errorHandler; | 1166 Function _onError; |
1167 } | 1167 } |
OLD | NEW |