Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(196)

Side by Side Diff: runtime/bin/file_impl.dart

Issue 8440037: Refactor async file implementation. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 = new File(file.name);
8 _file.openSync(); 8 _file.openSync();
9 } 9 }
10 10
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 return true; 107 return true;
108 } else { 108 } else {
109 throw "FileOutputStream: write error"; 109 throw "FileOutputStream: write error";
110 } 110 }
111 } 111 }
112 112
113 File _file; 113 File _file;
114 } 114 }
115 115
116 116
117 class _FileOperation {
118 abstract void execute(ReceivePort port);
119
120 SendPort set replyPort(SendPort port) {
121 _replyPort = port;
122 }
123
124 bool isWrite() => false;
125
126 SendPort _replyPort;
127 }
128
129
130 class _ExistsOperation extends _FileOperation {
131 _ExistsOperation(String this._name);
132
133 void execute(ReceivePort port) {
134 _replyPort.send(_File._exists(_name), port.toSendPort());
135 }
136
137 String _name;
138 }
139
140
141 class _OpenOperation extends _FileOperation {
142 _OpenOperation(String this._name, bool this._writable);
143
144 void execute(ReceivePort port) {
145 _replyPort.send(_File._open(_name, _writable), port.toSendPort());
146 }
147
148 String _name;
149 bool _writable;
150 }
151
152
153 class _CloseOperation extends _FileOperation {
154 _CloseOperation(int this._id);
155
156 void execute(ReceivePort port) {
157 _replyPort.send(_File._close(_id), port.toSendPort());
158 }
159
160 int _id;
161 }
162
163
164 class _ReadByteOperation extends _FileOperation {
165 _ReadByteOperation(int this._id);
166
167 void execute(ReceivePort port) {
168 _replyPort.send(_File._readByte(_id), port.toSendPort());
169 }
170
171 int _id;
172 }
173
174
175 class _ReadListResult {
176 _ReadListResult(this.read, this.buffer);
177 int read;
178 List buffer;
179 }
180
181
182 class _ReadListOperation extends _FileOperation {
183 _ReadListOperation(int this._id,
184 int this._length,
185 int this._offset,
186 int this._bytes);
187
188 void execute(ReceivePort port) {
189 if (_bytes == 0) {
190 _replyPort.send(0, port.toSendPort());
191 return;
192 }
193 int index = _File._checkReadWriteListArguments(_length, _offset, _bytes);
194 if (index != 0) {
195 _replyPort.send("index out of range in readList: $index",
196 port.toSendPort());
197 return;
198 }
199 var buffer = new List(_bytes);
200 var result =
201 new _ReadListResult(_File._readList(_id, buffer, 0, _bytes), buffer);
202 _replyPort.send(result, port.toSendPort());
203 }
204
205 int _id;
206 int _length;
207 int _offset;
208 int _bytes;
209 }
210
211
212 class _WriteByteOperation extends _FileOperation {
213 _WriteByteOperation(int this._id, int this._value);
214
215 void execute(ReceivePort port) {
216 _replyPort.send(_File._writeByte(_id, _value), port.toSendPort());
217 }
218
219 bool isWrite() => true;
220
221 int _id;
222 int _value;
223 }
224
225
226 class _WriteListOperation extends _FileOperation {
227 _WriteListOperation(int this._id,
228 List this._buffer,
229 int this._offset,
230 int this._bytes);
231
232 void execute(ReceivePort port) {
233 if (_bytes == 0) {
234 _replyPort.send(0, port.toSendPort());
235 return;
236 }
237 int index =
238 _File._checkReadWriteListArguments(_buffer.length, _offset, _bytes);
239 if (index != 0) {
240 _replyPort.send("index out of range in writeList: $index",
241 port.toSendPort());
242 return;
243 }
244 var result = _File._writeList(_id, _buffer, _offset, _bytes);
245 _replyPort.send(result, port.toSendPort());
246 }
247
248 bool isWrite() => true;
249
250 int _id;
251 List _buffer;
252 int _offset;
253 int _bytes;
254 }
255
256
257 class _WriteStringOperation extends _FileOperation {
258 _WriteStringOperation(int this._id, String this._string);
259
260 void execute(ReceivePort port) {
261 _replyPort.send(_File._writeString(_id, _string), port.toSendPort());
262 }
263
264 bool isWrite() => true;
265
266 int _id;
267 String _string;
268 }
269
270
271 class _PositionOperation extends _FileOperation {
272 _PositionOperation(int this._id);
273
274 void execute(ReceivePort port) {
275 _replyPort.send(_File._position(_id), port.toSendPort());
276 }
277
278 int _id;
279 }
280
281
282 class _LengthOperation extends _FileOperation {
283 _LengthOperation(int this._id);
284
285 void execute(ReceivePort port) {
286 _replyPort.send(_File._length(_id), port.toSendPort());
287 }
288
289 int _id;
290 }
291
292
293 class _FlushOperation extends _FileOperation {
294 _FlushOperation(int this._id);
295
296 void execute(ReceivePort port) {
297 _replyPort.send(_File._flush(_id), port.toSendPort());
298 }
299
300 int _id;
301 }
302
303
304 class _ExitOperation extends _FileOperation {
305 void execute(ReceivePort port) {
306 port.close();
307 }
308 }
309
310
117 class _FileOperationIsolate extends Isolate { 311 class _FileOperationIsolate extends Isolate {
118 static int EXISTS = 0;
119 static int OPEN = 1;
120 static int CLOSE = 2;
121 static int READ_BYTE = 3;
122 static int READ_LIST = 4;
123 static int WRITE_BYTE = 5;
124 static int WRITE_LIST = 6;
125 static int WRITE_STRING = 7;
126 static int POSITION = 8;
127 static int LENGTH = 9;
128 static int FLUSH = 10;
129 static int EXIT = 11;
130
131 _FileOperationIsolate() : super.heavy(); 312 _FileOperationIsolate() : super.heavy();
132 313
133 void handleOperation(Map message, SendPort ignored) { 314 void handleOperation(_FileOperation message, SendPort ignored) {
134 switch (message["type"]) { 315 message.execute(port);
135 case EXISTS:
136 message["reply"].send(_File._exists(message["name"]),
137 port.toSendPort());
138 break;
139 case OPEN:
140 var name = message["name"];
141 var writable = message["writable"];
142 message["reply"].send(_File._open(name, writable),
143 port.toSendPort());
144 break;
145 case CLOSE:
146 message["reply"].send(_File._close(message["id"]),
147 port.toSendPort());
148 break;
149 case READ_BYTE:
150 message["reply"].send(_File._readByte(message["id"]),
151 port.toSendPort());
152 break;
153 case READ_LIST:
154 var replyPort = message["reply"];
155 var bytes = message["bytes"];
156 var offset = message["offset"];
157 var length = message["length"];
158 var id = message["id"];
159 if (bytes == 0) {
160 replyPort.send(0, port.toSendPort());
161 return;
162 }
163 int index = _File._checkReadWriteListArguments(length, offset, bytes);
164 if (index != 0) {
165 replyPort.send("index out of range in readList: $index",
166 port.toSendPort());
167 return;
168 }
169 var buffer = new List(bytes);
170 var result = { "read": _File._readList(id, buffer, 0, bytes),
171 "buffer": buffer };
172 replyPort.send(result, port.toSendPort());
173 break;
174 case WRITE_BYTE:
175 message["reply"].send(_File._writeByte(message["id"], message["value"]),
176 port.toSendPort());
177 break;
178 case WRITE_LIST:
179 var replyPort = message["reply"];
180 var buffer = message["buffer"];
181 var bytes = message["bytes"];
182 var offset = message["offset"];
183 var id = message["id"];
184 if (bytes == 0) {
185 replyPort.send(0, port.toSendPort());
186 return;
187 }
188 int index =
189 _File._checkReadWriteListArguments(buffer.length, offset, bytes);
190 if (index != 0) {
191 replyPort.send("index out of range in writeList: $index",
192 port.toSendPort());
193 return;
194 }
195 var result = _File._writeList(id, buffer, offset, bytes);
196 replyPort.send(result, port.toSendPort());
197 break;
198 case WRITE_STRING:
199 var id = message["id"];
200 var string = message["string"];
201 message["reply"].send(_File._writeString(id, string),
202 port.toSendPort());
203 break;
204 case POSITION:
205 message["reply"].send(_File._position(message["id"]),
206 port.toSendPort());
207 break;
208 case LENGTH:
209 message["reply"].send(_File._length(message["id"]),
210 port.toSendPort());
211 break;
212 case FLUSH:
213 message["reply"].send(_File._flush(message["id"]),
214 port.toSendPort());
215 break;
216 case EXIT:
217 port.close();
218 return;
219 }
220 port.receive(handleOperation); 316 port.receive(handleOperation);
221 } 317 }
222 318
223 void main() { 319 void main() {
224 port.receive(handleOperation); 320 port.receive(handleOperation);
225 } 321 }
226 } 322 }
227 323
228 324
229 class _FileOperationScheduler { 325 class _FileOperationScheduler {
230 _FileOperationScheduler() : _queue = new Queue(); 326 _FileOperationScheduler() : _queue = new Queue();
231 327
232 void schedule(SendPort port) { 328 void schedule(SendPort port) {
233 assert(_isolate != null); 329 assert(_isolate != null);
234 if (_queue.isEmpty()) { 330 if (_queue.isEmpty()) {
235 port.send({ "type": _FileOperationIsolate.EXIT }); 331 port.send(new _ExitOperation());
236 _isolate = null; 332 _isolate = null;
237 } else { 333 } else {
238 port.send(_queue.removeFirst()); 334 port.send(_queue.removeFirst());
239 } 335 }
240 } 336 }
241 337
242 void scheduleWrap(void callback(result, ignored)) { 338 void scheduleWrap(void callback(result, ignored)) {
243 return (result, replyTo) { 339 return (result, replyTo) {
244 callback(result, replyTo); 340 callback(result, replyTo);
245 schedule(replyTo); 341 schedule(replyTo);
246 }; 342 };
247 } 343 }
248 344
249 void enqueue(Map params, void callback(result, ignored)) { 345 void enqueue(_FileOperation operation, void callback(result, ignored)) {
250 ReceivePort replyPort = new ReceivePort.singleShot(); 346 ReceivePort replyPort = new ReceivePort.singleShot();
251 replyPort.receive(scheduleWrap(callback)); 347 replyPort.receive(scheduleWrap(callback));
252 params["reply"] = replyPort.toSendPort(); 348 operation.replyPort = replyPort.toSendPort();
253 _queue.addLast(params); 349 _queue.addLast(operation);
254 if (_isolate == null) { 350 if (_isolate == null) {
255 _isolate = new _FileOperationIsolate(); 351 _isolate = new _FileOperationIsolate();
256 _isolate.spawn().then((port) { 352 _isolate.spawn().then((port) {
257 schedule(port); 353 schedule(port);
258 }); 354 });
259 } 355 }
260 } 356 }
261 357
262 bool noPendingWrite() { 358 bool noPendingWrite() {
263 int queuedWrites = 0; 359 int queuedWrites = 0;
264 _queue.forEach((map) { 360 _queue.forEach((operation) {
265 if (_isWriteOperation(map["type"])) { 361 if (operation.isWrite()) {
266 queuedWrites++; 362 queuedWrites++;
267 } 363 }
268 }); 364 });
269 return queuedWrites == 0; 365 return queuedWrites == 0;
270 } 366 }
271 367
272 bool _isWriteOperation(int type) { 368 Queue<_FileOperation> _queue;
273 return (type == _FileOperationIsolate.WRITE_BYTE) ||
274 (type == _FileOperationIsolate.WRITE_LIST) ||
275 (type == _FileOperationIsolate.WRITE_STRING);
276 }
277
278 Queue<Map> _queue;
279 _FileOperationIsolate _isolate; 369 _FileOperationIsolate _isolate;
280 } 370 }
281 371
282 372
283 // Class for encapsulating the native implementation of files. 373 // Class for encapsulating the native implementation of files.
284 class _File implements File { 374 class _File implements File {
285 // Constructor for file. 375 // Constructor for file.
286 _File(String this._name) 376 _File(String this._name)
287 : _scheduler = new _FileOperationScheduler(), 377 : _scheduler = new _FileOperationScheduler(),
288 _asyncUsed = false; 378 _asyncUsed = false;
(...skipping 16 matching lines...) Expand all
305 if (offset < 0) return offset; 395 if (offset < 0) return offset;
306 if (bytes < 0) return bytes; 396 if (bytes < 0) return bytes;
307 if ((offset + bytes) > length) return offset + bytes; 397 if ((offset + bytes) > length) return offset + bytes;
308 return 0; 398 return 0;
309 } 399 }
310 400
311 void exists() { 401 void exists() {
312 _asyncUsed = true; 402 _asyncUsed = true;
313 var handler = 403 var handler =
314 (_existsHandler != null) ? _existsHandler : (result) => null; 404 (_existsHandler != null) ? _existsHandler : (result) => null;
315 Map params = { 405 var operation = new _ExistsOperation(_name);
316 "type": _FileOperationIsolate.EXISTS, 406 _scheduler.enqueue(operation, (result, ignored) { _existsHandler(result); }) ;
317 "name": _name
318 };
319 _scheduler.enqueue(params, (result, ignored) { _existsHandler(result); });
320 } 407 }
321 408
322 bool existsSync() { 409 bool existsSync() {
323 if (_asyncUsed) { 410 if (_asyncUsed) {
324 throw new FileIOException( 411 throw new FileIOException(
325 "Mixed use of synchronous and asynchronous API"); 412 "Mixed use of synchronous and asynchronous API");
326 } 413 }
327 return _exists(_name); 414 return _exists(_name);
328 } 415 }
329 416
(...skipping 14 matching lines...) Expand all
344 _asyncUsed = true; 431 _asyncUsed = true;
345 var handler = (_openHandler != null) ? _openHandler : () => null; 432 var handler = (_openHandler != null) ? _openHandler : () => null;
346 var handleOpenResult = (result, ignored) { 433 var handleOpenResult = (result, ignored) {
347 if (result != 0) { 434 if (result != 0) {
348 _id = result; 435 _id = result;
349 handler(); 436 handler();
350 } else if (_errorHandler != null) { 437 } else if (_errorHandler != null) {
351 _errorHandler("Cannot open file: $_name"); 438 _errorHandler("Cannot open file: $_name");
352 } 439 }
353 }; 440 };
354 Map params = { 441 var operation = new _OpenOperation(_name, writable);
355 "type": _FileOperationIsolate.OPEN, 442 _scheduler.enqueue(operation, handleOpenResult);
356 "name": _name,
357 "writable": writable
358 };
359 _scheduler.enqueue(params, handleOpenResult);
360 } 443 }
361 444
362 void openSync([bool writable = false]) { 445 void openSync([bool writable = false]) {
363 if (_asyncUsed) { 446 if (_asyncUsed) {
364 throw new FileIOException( 447 throw new FileIOException(
365 "Mixed use of synchronous and asynchronous API"); 448 "Mixed use of synchronous and asynchronous API");
366 } 449 }
367 _id = _open(_name, writable); 450 _id = _open(_name, writable);
368 if (_id == 0) { 451 if (_id == 0) {
369 throw new FileIOException("Cannot open file: $_name"); 452 throw new FileIOException("Cannot open file: $_name");
370 } 453 }
371 } 454 }
372 455
373 void close() { 456 void close() {
374 _asyncUsed = true; 457 _asyncUsed = true;
375 var handler = (_closeHandler != null) ? _closeHandler : () => null; 458 var handler = (_closeHandler != null) ? _closeHandler : () => null;
376 var handleOpenResult = (result, ignored) { 459 var handleOpenResult = (result, ignored) {
377 if (result != -1) { 460 if (result != -1) {
378 _id = result; 461 _id = result;
379 handler(); 462 handler();
380 } else if (_errorHandler != null) { 463 } else if (_errorHandler != null) {
381 _errorHandler("Cannot open file: $_name"); 464 _errorHandler("Cannot open file: $_name");
382 } 465 }
383 }; 466 };
384 Map params = { 467 var operation = new _CloseOperation(_id);
385 "type": _FileOperationIsolate.CLOSE, 468 _scheduler.enqueue(operation, handleOpenResult);
386 "id": _id
387 };
388 _scheduler.enqueue(params, handleOpenResult);
389 } 469 }
390 470
391 void closeSync() { 471 void closeSync() {
392 if (_asyncUsed) { 472 if (_asyncUsed) {
393 throw new FileIOException( 473 throw new FileIOException(
394 "Mixed use of synchronous and asynchronous API"); 474 "Mixed use of synchronous and asynchronous API");
395 } 475 }
396 _id = _close(_id); 476 _id = _close(_id);
397 if (_id == -1) { 477 if (_id == -1) {
398 throw new FileIOException("Cannot close file: $_name"); 478 throw new FileIOException("Cannot close file: $_name");
399 } 479 }
400 } 480 }
401 481
402 void readByte() { 482 void readByte() {
403 _asyncUsed = true; 483 _asyncUsed = true;
404 var handler = 484 var handler =
405 (_readByteHandler != null) ? _readByteHandler : (byte) => null; 485 (_readByteHandler != null) ? _readByteHandler : (byte) => null;
406 var handleReadByteResult = (result, ignored) { 486 var handleReadByteResult = (result, ignored) {
407 if (result != -1) { 487 if (result != -1) {
408 handler(result); 488 handler(result);
409 } else if (_errorHandler != null) { 489 } else if (_errorHandler != null) {
410 _errorHandler("readByte failed"); 490 _errorHandler("readByte failed");
411 } 491 }
412 }; 492 };
413 Map params = { 493 var operation = new _ReadByteOperation(_id);
414 "type": _FileOperationIsolate.READ_BYTE, 494 _scheduler.enqueue(operation, handleReadByteResult);
415 "id": _id
416 };
417 _scheduler.enqueue(params, handleReadByteResult);
418 } 495 }
419 496
420 int readByteSync() { 497 int readByteSync() {
421 if (_asyncUsed) { 498 if (_asyncUsed) {
422 throw new FileIOException( 499 throw new FileIOException(
423 "Mixed use of synchronous and asynchronous API"); 500 "Mixed use of synchronous and asynchronous API");
424 } 501 }
425 int result = _readByte(_id); 502 int result = _readByte(_id);
426 if (result == -1) { 503 if (result == -1) {
427 throw new FileIOException("readByte failed"); 504 throw new FileIOException("readByte failed");
428 } 505 }
429 return result; 506 return result;
430 } 507 }
431 508
432 void readList(List<int> buffer, int offset, int bytes) { 509 void readList(List<int> buffer, int offset, int bytes) {
433 _asyncUsed = true; 510 _asyncUsed = true;
434 var handler = 511 var handler =
435 (_readListHandler != null) ? _readListHandler : (result) => null; 512 (_readListHandler != null) ? _readListHandler : (result) => null;
436 var handleReadListResult = (result, ignored) { 513 var handleReadListResult = (result, ignored) {
437 if (result is Map && result["read"] != -1) { 514 if (result is _ReadListResult && result.read != -1) {
438 var read = result["read"]; 515 var read = result.read;
439 buffer.setRange(offset, read, result["buffer"]); 516 buffer.setRange(offset, read, result.buffer);
440 handler(read); 517 handler(read);
441 return; 518 return;
442 } 519 }
443 if (_errorHandler != null) { 520 if (_errorHandler != null) {
444 _errorHandler(result is String ? result : "readList failed"); 521 _errorHandler(result is String ? result : "readList failed");
445 } 522 }
446 }; 523 };
447 Map params = { 524 var operation = new _ReadListOperation(_id, buffer.length, offset, bytes);
448 "type": _FileOperationIsolate.READ_LIST, 525 _scheduler.enqueue(operation, handleReadListResult);
449 "length": buffer.length,
450 "offset": offset,
451 "bytes": bytes,
452 "id": _id
453 };
454 _scheduler.enqueue(params, handleReadListResult);
455 } 526 }
456 527
457 int readListSync(List<int> buffer, int offset, int bytes) { 528 int readListSync(List<int> buffer, int offset, int bytes) {
458 if (_asyncUsed) { 529 if (_asyncUsed) {
459 throw new FileIOException( 530 throw new FileIOException(
460 "Mixed use of synchronous and asynchronous API"); 531 "Mixed use of synchronous and asynchronous API");
461 } 532 }
462 if (bytes == 0) return 0; 533 if (bytes == 0) return 0;
463 int index = _checkReadWriteListArguments(buffer.length, offset, bytes); 534 int index = _checkReadWriteListArguments(buffer.length, offset, bytes);
464 if (index != 0) { 535 if (index != 0) {
(...skipping 14 matching lines...) Expand all
479 550
480 void writeByte(int value) { 551 void writeByte(int value) {
481 _asyncUsed = true; 552 _asyncUsed = true;
482 var handleReadByteResult = (result, ignored) { 553 var handleReadByteResult = (result, ignored) {
483 if (result == -1 &&_errorHandler != null) { 554 if (result == -1 &&_errorHandler != null) {
484 _errorHandler("writeByte failed"); 555 _errorHandler("writeByte failed");
485 return; 556 return;
486 } 557 }
487 _checkPendingWrites(); 558 _checkPendingWrites();
488 }; 559 };
489 Map params = { 560 var operation = new _WriteByteOperation(_id, value);
490 "type": _FileOperationIsolate.WRITE_BYTE, 561 _scheduler.enqueue(operation, handleReadByteResult);
491 "value": value,
492 "id": _id
493 };
494 _scheduler.enqueue(params, handleReadByteResult);
495 } 562 }
496 563
497 int writeByteSync(int value) { 564 int writeByteSync(int value) {
498 if (_asyncUsed) { 565 if (_asyncUsed) {
499 throw new FileIOException( 566 throw new FileIOException(
500 "Mixed use of synchronous and asynchronous API"); 567 "Mixed use of synchronous and asynchronous API");
501 } 568 }
502 int result = _writeByte(_id, value); 569 int result = _writeByte(_id, value);
503 if (result == -1) { 570 if (result == -1) {
504 throw new FileIOException("writeByte failed"); 571 throw new FileIOException("writeByte failed");
505 } 572 }
506 return result; 573 return result;
507 } 574 }
508 575
509 void writeList(List<int> buffer, int offset, int bytes) { 576 void writeList(List<int> buffer, int offset, int bytes) {
510 _asyncUsed = true; 577 _asyncUsed = true;
511 var handleWriteListResult = (result, ignored) { 578 var handleWriteListResult = (result, ignored) {
512 if (result is !String && result != -1) { 579 if (result is !String && result != -1) {
513 if (result < bytes) { 580 if (result < bytes) {
514 writeList(buffer, offset + result, bytes - result); 581 writeList(buffer, offset + result, bytes - result);
515 } else { 582 } else {
516 _checkPendingWrites(); 583 _checkPendingWrites();
517 } 584 }
518 return; 585 return;
519 } 586 }
520 if (_errorHandler != null) { 587 if (_errorHandler != null) {
521 _errorHandler(result is String ? result : "writeList failed"); 588 _errorHandler(result is String ? result : "writeList failed");
522 } 589 }
523 }; 590 };
524 Map params = { 591 var operation = new _WriteListOperation(_id, buffer, offset, bytes);
525 "type": _FileOperationIsolate.WRITE_LIST, 592 _scheduler.enqueue(operation, handleWriteListResult);
526 "buffer": buffer,
527 "offset": offset,
528 "bytes": bytes,
529 "id": _id
530 };
531 _scheduler.enqueue(params, handleWriteListResult);
532 } 593 }
533 594
534 int writeListSync(List<int> buffer, int offset, int bytes) { 595 int writeListSync(List<int> buffer, int offset, int bytes) {
535 if (_asyncUsed) { 596 if (_asyncUsed) {
536 throw new FileIOException( 597 throw new FileIOException(
537 "Mixed use of synchronous and asynchronous API"); 598 "Mixed use of synchronous and asynchronous API");
538 } 599 }
539 if (bytes == 0) return 0; 600 if (bytes == 0) return 0;
540 int index = _checkReadWriteListArguments(buffer.length, offset, bytes); 601 int index = _checkReadWriteListArguments(buffer.length, offset, bytes);
541 if (index != 0) { 602 if (index != 0) {
(...skipping 12 matching lines...) Expand all
554 if (result == -1 &&_errorHandler != null) { 615 if (result == -1 &&_errorHandler != null) {
555 _errorHandler("writeString failed"); 616 _errorHandler("writeString failed");
556 return; 617 return;
557 } 618 }
558 if (result < string.length) { 619 if (result < string.length) {
559 writeString(string.substring(result)); 620 writeString(string.substring(result));
560 } else { 621 } else {
561 _checkPendingWrites(); 622 _checkPendingWrites();
562 } 623 }
563 }; 624 };
564 Map params = { 625 var operation = new _WriteStringOperation(_id, string);
565 "type": _FileOperationIsolate.WRITE_STRING, 626 _scheduler.enqueue(operation, handleWriteStringResult);
566 "string": string,
567 "id": _id
568 };
569 _scheduler.enqueue(params, handleWriteStringResult);
570 } 627 }
571 628
572 int writeStringSync(String string) { 629 int writeStringSync(String string) {
573 if (_asyncUsed) { 630 if (_asyncUsed) {
574 throw new FileIOException( 631 throw new FileIOException(
575 "Mixed use of synchronous and asynchronous API"); 632 "Mixed use of synchronous and asynchronous API");
576 } 633 }
577 int result = _writeString(_id, string); 634 int result = _writeString(_id, string);
578 if (result == -1) { 635 if (result == -1) {
579 throw new FileIOException("Error: writeString failed"); 636 throw new FileIOException("Error: writeString failed");
580 } 637 }
581 return result; 638 return result;
582 } 639 }
583 640
584 void position() { 641 void position() {
585 _asyncUsed = true; 642 _asyncUsed = true;
586 var handler = (_positionHandler != null) ? _positionHandler : (pos) => null; 643 var handler = (_positionHandler != null) ? _positionHandler : (pos) => null;
587 var handlePositionResult = (result, ignored) { 644 var handlePositionResult = (result, ignored) {
588 if (result == -1 && _errorHandler != null) { 645 if (result == -1 && _errorHandler != null) {
589 _errorHandler("position failed"); 646 _errorHandler("position failed");
590 return; 647 return;
591 } 648 }
592 handler(result); 649 handler(result);
593 }; 650 };
594 Map params = { 651 var operation = new _PositionOperation(_id);
595 "type": _FileOperationIsolate.POSITION, 652 _scheduler.enqueue(operation, handlePositionResult);
596 "id": _id
597 };
598 _scheduler.enqueue(params, handlePositionResult);
599 } 653 }
600 654
601 int positionSync() { 655 int positionSync() {
602 if (_asyncUsed) { 656 if (_asyncUsed) {
603 throw new FileIOException( 657 throw new FileIOException(
604 "Mixed use of synchronous and asynchronous API"); 658 "Mixed use of synchronous and asynchronous API");
605 } 659 }
606 int result = _position(_id); 660 int result = _position(_id);
607 if (result == -1) { 661 if (result == -1) {
608 throw new FileIOException("position failed"); 662 throw new FileIOException("position failed");
609 } 663 }
610 return result; 664 return result;
611 } 665 }
612 666
613 void length() { 667 void length() {
614 _asyncUsed = true; 668 _asyncUsed = true;
615 var handler = (_lengthHandler != null) ? _lengthHandler : (pos) => null; 669 var handler = (_lengthHandler != null) ? _lengthHandler : (pos) => null;
616 var handleLengthResult = (result, ignored) { 670 var handleLengthResult = (result, ignored) {
617 if (result == -1 && _errorHandler != null) { 671 if (result == -1 && _errorHandler != null) {
618 _errorHandler("length failed"); 672 _errorHandler("length failed");
619 return; 673 return;
620 } 674 }
621 handler(result); 675 handler(result);
622 }; 676 };
623 Map params = { 677 var operation = new _LengthOperation(_id);
624 "type": _FileOperationIsolate.LENGTH, 678 _scheduler.enqueue(operation, handleLengthResult);
625 "id": _id
626 };
627 _scheduler.enqueue(params, handleLengthResult);
628 } 679 }
629 680
630 int lengthSync() { 681 int lengthSync() {
631 if (_asyncUsed) { 682 if (_asyncUsed) {
632 throw new FileIOException( 683 throw new FileIOException(
633 "Mixed use of synchronous and asynchronous API"); 684 "Mixed use of synchronous and asynchronous API");
634 } 685 }
635 int result = _length(_id); 686 int result = _length(_id);
636 if (result == -1) { 687 if (result == -1) {
637 throw new FileIOException("length failed"); 688 throw new FileIOException("length failed");
638 } 689 }
639 return result; 690 return result;
640 } 691 }
641 692
642 void flush() { 693 void flush() {
643 _asyncUsed = true; 694 _asyncUsed = true;
644 var handler = (_flushHandler != null) ? _flushHandler : (pos) => null; 695 var handler = (_flushHandler != null) ? _flushHandler : (pos) => null;
645 var handleFlushResult = (result, ignored) { 696 var handleFlushResult = (result, ignored) {
646 if (result == -1 && _errorHandler != null) { 697 if (result == -1 && _errorHandler != null) {
647 _errorHandler("flush failed"); 698 _errorHandler("flush failed");
648 return; 699 return;
649 } 700 }
650 handler(); 701 handler();
651 }; 702 };
652 Map params = { 703 var operation = new _FlushOperation(_id);
653 "type": _FileOperationIsolate.FLUSH, 704 _scheduler.enqueue(operation, handleFlushResult);
654 "id": _id
655 };
656 _scheduler.enqueue(params, handleFlushResult);
657 } 705 }
658 706
659 void flushSync() { 707 void flushSync() {
660 if (_asyncUsed) { 708 if (_asyncUsed) {
661 throw new FileIOException( 709 throw new FileIOException(
662 "Mixed use of synchronous and asynchronous API"); 710 "Mixed use of synchronous and asynchronous API");
663 } 711 }
664 int result = _flush(_id); 712 int result = _flush(_id);
665 if (result == -1) { 713 if (result == -1) {
666 throw new FileIOException("flush failed"); 714 throw new FileIOException("flush failed");
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
734 var _openHandler; 782 var _openHandler;
735 var _closeHandler; 783 var _closeHandler;
736 var _readByteHandler; 784 var _readByteHandler;
737 var _readListHandler; 785 var _readListHandler;
738 var _noPendingWriteHandler; 786 var _noPendingWriteHandler;
739 var _positionHandler; 787 var _positionHandler;
740 var _lengthHandler; 788 var _lengthHandler;
741 var _flushHandler; 789 var _flushHandler;
742 var _errorHandler; 790 var _errorHandler;
743 } 791 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698