| 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.onError = (e) { | 10 _file.onError = _reportError; |
| 11 if (_clientErrorHandler != null) { | |
| 12 _clientErrorHandler(e); | |
| 13 } | |
| 14 }; | |
| 15 _file.open(FileMode.READ, (openedFile) { | 11 _file.open(FileMode.READ, (openedFile) { |
| 16 _readDataFromFile(openedFile); | 12 _readDataFromFile(openedFile); |
| 17 }); | 13 }); |
| 18 } | 14 } |
| 19 | 15 |
| 20 _FileInputStream.fromStdio(int fd) { | 16 _FileInputStream.fromStdio(int fd) { |
| 21 assert(fd == 0); | 17 assert(fd == 0); |
| 22 _file = _File._openStdioSync(fd); | 18 _file = _File._openStdioSync(fd); |
| 23 _data = []; | 19 _data = []; |
| 24 _position = 0; | 20 _position = 0; |
| 25 _readDataFromFile(_file); | 21 _readDataFromFile(_file); |
| 26 } | 22 } |
| 27 | 23 |
| 28 void _readDataFromFile(RandomAccessFile openedFile) { | 24 void _readDataFromFile(RandomAccessFile openedFile) { |
| 29 openedFile.onError = (e) { | 25 openedFile.onError = _reportError; |
| 30 if (_clientErrorHandler != null) { | |
| 31 _clientErrorHandler(e); | |
| 32 } | |
| 33 }; | |
| 34 openedFile.length((length) { | 26 openedFile.length((length) { |
| 35 var contents = new ByteArray(length); | 27 var contents = new ByteArray(length); |
| 36 if (length != 0) { | 28 if (length != 0) { |
| 37 openedFile.readList(contents, 0, length, (read) { | 29 openedFile.readList(contents, 0, length, (read) { |
| 38 if (read != length) { | 30 if (read != length) { |
| 39 if (_clientErrorHandler != null) { | 31 _reportError(new FileIOException( |
| 40 _clientErrorHandler(); | 32 'Failed reading file contents in FileInputStream')); |
| 41 } | |
| 42 } else { | 33 } else { |
| 43 _data = contents; | 34 _data = contents; |
| 44 } | 35 } |
| 45 openedFile.close(() { | 36 openedFile.close(() { |
| 46 _streamMarkedClosed = true; | 37 _streamMarkedClosed = true; |
| 47 _checkScheduleCallbacks(); | 38 _checkScheduleCallbacks(); |
| 48 }); | 39 }); |
| 49 }); | 40 }); |
| 50 } else { | 41 } else { |
| 51 openedFile.close(() { | 42 openedFile.close(() { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 | 84 |
| 94 class _FileOutputStream extends _BaseOutputStream implements OutputStream { | 85 class _FileOutputStream extends _BaseOutputStream implements OutputStream { |
| 95 _FileOutputStream(String name, FileMode mode) { | 86 _FileOutputStream(String name, FileMode mode) { |
| 96 _pendingOperations = new List<List<int>>(); | 87 _pendingOperations = new List<List<int>>(); |
| 97 var f = new File(name); | 88 var f = new File(name); |
| 98 f.open(mode, (openedFile) { | 89 f.open(mode, (openedFile) { |
| 99 _file = openedFile; | 90 _file = openedFile; |
| 100 _setupFileHandlers(); | 91 _setupFileHandlers(); |
| 101 _processPendingOperations(); | 92 _processPendingOperations(); |
| 102 }); | 93 }); |
| 103 f.onError = (e) => _reportError(e); | 94 f.onError = _reportError; |
| 104 } | 95 } |
| 105 | 96 |
| 106 _FileOutputStream.fromStdio(int fd) { | 97 _FileOutputStream.fromStdio(int fd) { |
| 107 assert(1 <= fd && fd <= 2); | 98 assert(1 <= fd && fd <= 2); |
| 108 _file = _File._openStdioSync(fd); | 99 _file = _File._openStdioSync(fd); |
| 109 _setupFileHandlers(); | 100 _setupFileHandlers(); |
| 110 } | 101 } |
| 111 | 102 |
| 112 | 103 |
| 113 void _setupFileHandlers() { | 104 void _setupFileHandlers() { |
| 114 _file.onError = (e) => _reportError(e); | 105 _file.onError = _reportError; |
| 115 _file.onNoPendingWrites = () { | 106 _file.onNoPendingWrites = () { |
| 116 if (!_streamMarkedClosed && _onNoPendingWrites != null) { | 107 if (!_streamMarkedClosed && _onNoPendingWrites != null) { |
| 117 _onNoPendingWrites(); | 108 _onNoPendingWrites(); |
| 118 } | 109 } |
| 119 }; | 110 }; |
| 120 } | 111 } |
| 121 | 112 |
| 122 bool write(List<int> buffer, [bool copyBuffer = false]) { | 113 bool write(List<int> buffer, [bool copyBuffer = false]) { |
| 123 var data = buffer; | 114 var data = buffer; |
| 124 if (copyBuffer) { | 115 if (copyBuffer) { |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 } | 282 } |
| 292 return result; | 283 return result; |
| 293 } | 284 } |
| 294 | 285 |
| 295 static int checkedOpen(String name, int mode) { | 286 static int checkedOpen(String name, int mode) { |
| 296 if (name is !String || mode is !int) { | 287 if (name is !String || mode is !int) { |
| 297 throw new IllegalArgumentException(); | 288 throw new IllegalArgumentException(); |
| 298 }; | 289 }; |
| 299 var result = open(name, mode); | 290 var result = open(name, mode); |
| 300 if (result is OSError) { | 291 if (result is OSError) { |
| 301 throw new FileIOException("Cannot open file", result); | 292 throw new FileIOException("Cannot open file $name", result); |
| 302 } | 293 } |
| 303 return result; | 294 return result; |
| 304 } | 295 } |
| 305 | 296 |
| 306 static bool checkedCreate(String name) { | 297 static bool checkedCreate(String name) { |
| 307 if (name is !String) { | 298 if (name is !String) { |
| 308 throw new IllegalArgumentException(); | 299 throw new IllegalArgumentException(); |
| 309 }; | 300 }; |
| 310 var result = create(name); | 301 var result = create(name); |
| 311 if (result is OSError) { | 302 if (result is OSError) { |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 // Constructor for file. | 409 // Constructor for file. |
| 419 _File(String this._name); | 410 _File(String this._name); |
| 420 | 411 |
| 421 void exists(void callback(bool exists)) { | 412 void exists(void callback(bool exists)) { |
| 422 _ensureFileService(); | 413 _ensureFileService(); |
| 423 List request = new List(2); | 414 List request = new List(2); |
| 424 request[0] = _FileUtils.kExistsRequest; | 415 request[0] = _FileUtils.kExistsRequest; |
| 425 request[1] = _name; | 416 request[1] = _name; |
| 426 _fileService.call(request).then((response) { | 417 _fileService.call(request).then((response) { |
| 427 if (_isErrorResponse(response)) { | 418 if (_isErrorResponse(response)) { |
| 428 _handleErrorResponse(response, "Cannot open file"); | 419 _handleErrorResponse(response, "Cannot open file $_name"); |
| 429 } else { | 420 } else { |
| 430 callback(response); | 421 callback(response); |
| 431 } | 422 } |
| 432 }); | 423 }); |
| 433 } | 424 } |
| 434 | 425 |
| 435 bool existsSync() { | 426 bool existsSync() { |
| 436 return _FileUtils.checkedExists(_name); | 427 return _FileUtils.checkedExists(_name); |
| 437 } | 428 } |
| 438 | 429 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 mode != FileMode.APPEND) { | 492 mode != FileMode.APPEND) { |
| 502 _reportError(new IllegalArgumentException()); | 493 _reportError(new IllegalArgumentException()); |
| 503 return; | 494 return; |
| 504 } | 495 } |
| 505 List request = new List(3); | 496 List request = new List(3); |
| 506 request[0] = _FileUtils.kOpenRequest; | 497 request[0] = _FileUtils.kOpenRequest; |
| 507 request[1] = _name; | 498 request[1] = _name; |
| 508 request[2] = mode._mode; // Direct int value for serialization. | 499 request[2] = mode._mode; // Direct int value for serialization. |
| 509 _fileService.call(request).then((response) { | 500 _fileService.call(request).then((response) { |
| 510 if (_isErrorResponse(response)) { | 501 if (_isErrorResponse(response)) { |
| 511 _handleErrorResponse(response, "Cannot open file"); | 502 _handleErrorResponse(response, "Cannot open file $_name"); |
| 512 } else { | 503 } else { |
| 513 callback(new _RandomAccessFile(response, _name)); | 504 callback(new _RandomAccessFile(response, _name)); |
| 514 } | 505 } |
| 515 }); | 506 }); |
| 516 } | 507 } |
| 517 | 508 |
| 518 void length(void callback(int length)) { | 509 void length(void callback(int length)) { |
| 519 _ensureFileService(); | 510 _ensureFileService(); |
| 520 List request = new List(2); | 511 List request = new List(2); |
| 521 request[0] = _FileUtils.kLengthFromNameRequest; | 512 request[0] = _FileUtils.kLengthFromNameRequest; |
| (...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1038 String _name; | 1029 String _name; |
| 1039 int _id; | 1030 int _id; |
| 1040 int _pendingWrites = 0; | 1031 int _pendingWrites = 0; |
| 1041 | 1032 |
| 1042 SendPort _fileService; | 1033 SendPort _fileService; |
| 1043 | 1034 |
| 1044 Timer _noPendingWriteTimer; | 1035 Timer _noPendingWriteTimer; |
| 1045 | 1036 |
| 1046 Function _onNoPendingWrites; | 1037 Function _onNoPendingWrites; |
| 1047 } | 1038 } |
| OLD | NEW |