| 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 var file = new File(name); | 7 var file = new File(name); |
| 8 _data = []; | 8 _data = []; |
| 9 _position = 0; | 9 _position = 0; |
| 10 file.onError = _reportError; | 10 file.onError = _reportError; |
| 11 file.open(FileMode.READ, (openedFile) { | 11 file.open(FileMode.READ, (openedFile) { |
| 12 _readDataFromFile(openedFile); | 12 _readDataFromFile(openedFile); |
| 13 }); | 13 }); |
| 14 } | 14 } |
| 15 | 15 |
| 16 _FileInputStream.fromStdio(int fd) { | 16 _FileInputStream.fromStdio(int fd) { |
| 17 assert(fd == 0); | 17 assert(fd == 0); |
| 18 var file = _File._openStdioSync(fd); | 18 var file = _File._openStdioSync(fd); |
| 19 _data = []; | 19 _data = []; |
| 20 _position = 0; | 20 _position = 0; |
| 21 _readDataFromFile(file); | 21 _readDataFromFile(file); |
| 22 } | 22 } |
| 23 | 23 |
| 24 void _readDataFromFile(RandomAccessFile openedFile) { | 24 void _readDataFromFile(RandomAccessFile openedFile) { |
| 25 openedFile.onError = _reportError; | 25 openedFile.onError = _reportError; |
| 26 openedFile.length((length) { | 26 openedFile.length((length) { |
| 27 var contents = new ByteArray(length); | 27 var contents = new Uint8List(length); |
| 28 if (length != 0) { | 28 if (length != 0) { |
| 29 openedFile.readList(contents, 0, length, (read) { | 29 openedFile.readList(contents, 0, length, (read) { |
| 30 if (read != length) { | 30 if (read != length) { |
| 31 _reportError(new FileIOException( | 31 _reportError(new FileIOException( |
| 32 'Failed reading file contents in FileInputStream')); | 32 'Failed reading file contents in FileInputStream')); |
| 33 } else { | 33 } else { |
| 34 _data = contents; | 34 _data = contents; |
| 35 } | 35 } |
| 36 openedFile.close(() { | 36 openedFile.close(() { |
| 37 _streamMarkedClosed = true; | 37 _streamMarkedClosed = true; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 49 | 49 |
| 50 int available() { | 50 int available() { |
| 51 return _closed ? 0 : _data.length - _position; | 51 return _closed ? 0 : _data.length - _position; |
| 52 } | 52 } |
| 53 | 53 |
| 54 void pipe(OutputStream output, [bool close = true]) { | 54 void pipe(OutputStream output, [bool close = true]) { |
| 55 _pipe(this, output, close: close); | 55 _pipe(this, output, close: close); |
| 56 } | 56 } |
| 57 | 57 |
| 58 List<int> _read(int bytesToRead) { | 58 List<int> _read(int bytesToRead) { |
| 59 ByteArray result = new ByteArray(bytesToRead); | 59 List<int> result = new Uint8List(bytesToRead); |
| 60 result.setRange(0, bytesToRead, _data, _position); | 60 result.setRange(0, bytesToRead, _data, _position); |
| 61 _position += bytesToRead; | 61 _position += bytesToRead; |
| 62 _checkScheduleCallbacks(); | 62 _checkScheduleCallbacks(); |
| 63 return result; | 63 return result; |
| 64 } | 64 } |
| 65 | 65 |
| 66 int _readInto(List<int> buffer, int offset, int len) { | 66 int _readInto(List<int> buffer, int offset, int len) { |
| 67 buffer.setRange(offset, len, _data, _position); | 67 buffer.setRange(offset, len, _data, _position); |
| 68 _position += len; | 68 _position += len; |
| 69 _checkScheduleCallbacks(); | 69 _checkScheduleCallbacks(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 if (!_streamMarkedClosed && _onNoPendingWrites != null) { | 106 if (!_streamMarkedClosed && _onNoPendingWrites != null) { |
| 107 _onNoPendingWrites(); | 107 _onNoPendingWrites(); |
| 108 } | 108 } |
| 109 }; | 109 }; |
| 110 } | 110 } |
| 111 | 111 |
| 112 bool write(List<int> buffer, [bool copyBuffer = false]) { | 112 bool write(List<int> buffer, [bool copyBuffer = false]) { |
| 113 var data = buffer; | 113 var data = buffer; |
| 114 if (copyBuffer) { | 114 if (copyBuffer) { |
| 115 var length = buffer.length; | 115 var length = buffer.length; |
| 116 data = new ByteArray(length); | 116 data = new Uint8List(length); |
| 117 data.setRange(0, length, buffer, 0); | 117 data.setRange(0, length, buffer, 0); |
| 118 } | 118 } |
| 119 if (_file == null) { | 119 if (_file == null) { |
| 120 _pendingOperations.add(data); | 120 _pendingOperations.add(data); |
| 121 } else { | 121 } else { |
| 122 _write(data, 0, data.length); | 122 _write(data, 0, data.length); |
| 123 } | 123 } |
| 124 return false; | 124 return false; |
| 125 } | 125 } |
| 126 | 126 |
| 127 bool writeFrom(List<int> buffer, [int offset = 0, int len]) { | 127 bool writeFrom(List<int> buffer, [int offset = 0, int len]) { |
| 128 // A copy is required by the interface. | 128 // A copy is required by the interface. |
| 129 var length = buffer.length - offset; | 129 var length = buffer.length - offset; |
| 130 if (len != null) { | 130 if (len != null) { |
| 131 if (len > length) throw new IndexOutOfRangeException(len); | 131 if (len > length) throw new IndexOutOfRangeException(len); |
| 132 length = len; | 132 length = len; |
| 133 } | 133 } |
| 134 var copy = new ByteArray(length); | 134 var copy = new Uint8List(length); |
| 135 copy.setRange(0, length, buffer, offset); | 135 copy.setRange(0, length, buffer, offset); |
| 136 return write(copy); | 136 return write(copy); |
| 137 } | 137 } |
| 138 | 138 |
| 139 void close() { | 139 void close() { |
| 140 if (_file == null) { | 140 if (_file == null) { |
| 141 _pendingOperations.add(null); | 141 _pendingOperations.add(null); |
| 142 } else if (!_streamMarkedClosed) { | 142 } else if (!_streamMarkedClosed) { |
| 143 _file.close(() { | 143 _file.close(() { |
| 144 if (_onClosed != null) _onClosed(); | 144 if (_onClosed != null) _onClosed(); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 static final kOSErrorResponseErrorCode = 1; | 215 static final kOSErrorResponseErrorCode = 1; |
| 216 static final kOSErrorResponseMessage = 2; | 216 static final kOSErrorResponseMessage = 2; |
| 217 | 217 |
| 218 static List ensureFastAndSerializableBuffer( | 218 static List ensureFastAndSerializableBuffer( |
| 219 List buffer, int offset, int bytes) { | 219 List buffer, int offset, int bytes) { |
| 220 // When using the Dart C API to access raw data, using a ByteArray is | 220 // When using the Dart C API to access raw data, using a ByteArray is |
| 221 // currently much faster. This function will make a copy of the | 221 // currently much faster. This function will make a copy of the |
| 222 // supplied List to a ByteArray if it isn't already. | 222 // supplied List to a ByteArray if it isn't already. |
| 223 List outBuffer; | 223 List outBuffer; |
| 224 int outOffset = offset; | 224 int outOffset = offset; |
| 225 if (buffer is ByteArray || buffer is ObjectArray) { | 225 if (buffer is Uint8List || buffer is ObjectArray) { |
| 226 outBuffer = buffer; | 226 outBuffer = buffer; |
| 227 } else { | 227 } else { |
| 228 outBuffer = new ByteArray(bytes); | 228 outBuffer = new Uint8List(bytes); |
| 229 outOffset = 0; | 229 outOffset = 0; |
| 230 int j = offset; | 230 int j = offset; |
| 231 for (int i = 0; i < bytes; i++) { | 231 for (int i = 0; i < bytes; i++) { |
| 232 int value = buffer[j]; | 232 int value = buffer[j]; |
| 233 if (value is! int) { | 233 if (value is! int) { |
| 234 throw new FileIOException( | 234 throw new FileIOException( |
| 235 "List element is not an integer at index $j"); | 235 "List element is not an integer at index $j"); |
| 236 } | 236 } |
| 237 outBuffer[i] = value; | 237 outBuffer[i] = value; |
| 238 j++; | 238 j++; |
| (...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 594 stream.onData = () { | 594 stream.onData = () { |
| 595 var chunk = stream.read(); | 595 var chunk = stream.read(); |
| 596 chunks.add(chunk); | 596 chunks.add(chunk); |
| 597 }; | 597 }; |
| 598 stream.onError = (e) => _reportError(e); | 598 stream.onError = (e) => _reportError(e); |
| 599 } | 599 } |
| 600 | 600 |
| 601 List<int> readAsBytesSync() { | 601 List<int> readAsBytesSync() { |
| 602 var opened = openSync(); | 602 var opened = openSync(); |
| 603 var length = opened.lengthSync(); | 603 var length = opened.lengthSync(); |
| 604 var result = new ByteArray(length); | 604 var result = new Uint8List(length); |
| 605 var read = opened.readListSync(result, 0, length); | 605 var read = opened.readListSync(result, 0, length); |
| 606 if (read != length) { | 606 if (read != length) { |
| 607 throw new FileIOException("Failed to read file"); | 607 throw new FileIOException("Failed to read file"); |
| 608 } | 608 } |
| 609 opened.closeSync(); | 609 opened.closeSync(); |
| 610 return result; | 610 return result; |
| 611 } | 611 } |
| 612 | 612 |
| 613 void readAsText(Encoding encoding, void callback(String text)) { | 613 void readAsText(Encoding encoding, void callback(String text)) { |
| 614 _ensureFileService(); | 614 _ensureFileService(); |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1042 String _name; | 1042 String _name; |
| 1043 int _id; | 1043 int _id; |
| 1044 int _pendingWrites = 0; | 1044 int _pendingWrites = 0; |
| 1045 | 1045 |
| 1046 SendPort _fileService; | 1046 SendPort _fileService; |
| 1047 | 1047 |
| 1048 Timer _noPendingWriteTimer; | 1048 Timer _noPendingWriteTimer; |
| 1049 | 1049 |
| 1050 Function _onNoPendingWrites; | 1050 Function _onNoPendingWrites; |
| 1051 } | 1051 } |
| OLD | NEW |