| 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(File file) { | 6 _FileInputStream(File file) { |
| 7 _file = file.openSync(); | 7 _file = file.openSync(); |
| 8 _length = _file.lengthSync(); | 8 _length = _file.lengthSync(); |
| 9 _streamMarkedClosed = true; | 9 _streamMarkedClosed = true; |
| 10 _checkScheduleCallbacks(); | 10 _checkScheduleCallbacks(); |
| 11 } | 11 } |
| 12 | 12 |
| 13 int available() { | 13 int available() { |
| 14 return _closed ? 0 : _length - _file.positionSync(); | 14 return _closed ? 0 : _length - _file.positionSync(); |
| 15 } | 15 } |
| 16 | 16 |
| 17 void pipe(OutputStream output, [bool close = true]) { | 17 void pipe(OutputStream output, [bool close = true]) { |
| 18 _pipe(this, output, close: close); | 18 _pipe(this, output, close: close); |
| 19 } | 19 } |
| 20 | 20 |
| 21 List<int> _read(int bytesToRead) { | 21 List<int> _read(int bytesToRead) { |
| 22 List<int> result = new List<int>(bytesToRead); | 22 ByteArray result = new ByteArray(bytesToRead); |
| 23 int bytesRead = _file.readListSync(result, 0, bytesToRead); | 23 int bytesRead = _file.readListSync(result, 0, bytesToRead); |
| 24 if (bytesRead < bytesToRead) { | 24 if (bytesRead < bytesToRead) { |
| 25 List<int> buffer = new List<int>(bytesRead); | 25 ByteArray buffer = new ByteArray(bytesRead); |
| 26 buffer.copyFrom(result, 0, 0, bytesRead); | 26 buffer.setRange(0, bytesRead, result); |
| 27 result = buffer; | 27 result = buffer; |
| 28 } | 28 } |
| 29 _checkScheduleCallbacks(); | 29 _checkScheduleCallbacks(); |
| 30 return result; | 30 return result; |
| 31 } | 31 } |
| 32 | 32 |
| 33 int _readInto(List<int> buffer, int offset, int len) { | 33 int _readInto(List<int> buffer, int offset, int len) { |
| 34 int result = _file.readListSync(buffer, offset, len); | 34 int result = _file.readListSync(buffer, offset, len); |
| 35 _checkScheduleCallbacks(); | 35 _checkScheduleCallbacks(); |
| 36 return result; | 36 return result; |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 _replyPort.send(0, port.toSendPort()); | 168 _replyPort.send(0, port.toSendPort()); |
| 169 return; | 169 return; |
| 170 } | 170 } |
| 171 int index = | 171 int index = |
| 172 _FileUtils.checkReadWriteListArguments(_length, _offset, _bytes); | 172 _FileUtils.checkReadWriteListArguments(_length, _offset, _bytes); |
| 173 if (index != 0) { | 173 if (index != 0) { |
| 174 _replyPort.send("index out of range in readList: $index", | 174 _replyPort.send("index out of range in readList: $index", |
| 175 port.toSendPort()); | 175 port.toSendPort()); |
| 176 return; | 176 return; |
| 177 } | 177 } |
| 178 var buffer = new List(_bytes); | 178 ByteArray buffer = new ByteArray(_bytes); |
| 179 var result = | 179 var result = |
| 180 new _ReadListResult(_FileUtils.readList(_id, buffer, 0, _bytes), | 180 new _ReadListResult(_FileUtils.readList(_id, buffer, 0, _bytes), |
| 181 buffer); | 181 buffer); |
| 182 _replyPort.send(result, port.toSendPort()); | 182 _replyPort.send(result, port.toSendPort()); |
| 183 } | 183 } |
| 184 | 184 |
| 185 int _id; | 185 int _id; |
| 186 int _length; | 186 int _length; |
| 187 int _offset; | 187 int _offset; |
| 188 int _bytes; | 188 int _bytes; |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 static int open(String name, int mode) native "File_Open"; | 414 static int open(String name, int mode) native "File_Open"; |
| 415 static bool create(String name) native "File_Create"; | 415 static bool create(String name) native "File_Create"; |
| 416 static bool delete(String name) native "File_Delete"; | 416 static bool delete(String name) native "File_Delete"; |
| 417 static String fullPath(String name) native "File_FullPath"; | 417 static String fullPath(String name) native "File_FullPath"; |
| 418 static int close(int id) native "File_Close"; | 418 static int close(int id) native "File_Close"; |
| 419 static int readByte(int id) native "File_ReadByte"; | 419 static int readByte(int id) native "File_ReadByte"; |
| 420 static int readList(int id, List<int> buffer, int offset, int bytes) | 420 static int readList(int id, List<int> buffer, int offset, int bytes) |
| 421 native "File_ReadList"; | 421 native "File_ReadList"; |
| 422 static int writeByte(int id, int value) native "File_WriteByte"; | 422 static int writeByte(int id, int value) native "File_WriteByte"; |
| 423 static int writeList(int id, List<int> buffer, int offset, int bytes) { | 423 static int writeList(int id, List<int> buffer, int offset, int bytes) { |
| 424 // When using the Dart C API access to ObjectArray by index is | 424 // When using the Dart C API to access raw data, using a ByteArray is |
| 425 // currently much faster. This function will make a copy of the | 425 // currently much faster. This function will make a copy of the |
| 426 // supplied List to an ObjectArray if it isn't already. | 426 // supplied List to a ByteArray if it isn't already. |
| 427 ObjectArray outBuffer; | 427 List outBuffer; |
| 428 int outOffset = offset; | 428 int outOffset = offset; |
| 429 if (buffer is ObjectArray) { | 429 if (buffer is ByteArray || buffer is ObjectArray) { |
| 430 outBuffer = buffer; | 430 outBuffer = buffer; |
| 431 } else { | 431 } else { |
| 432 outBuffer = new ObjectArray(bytes); | 432 outBuffer = new ByteArray(bytes); |
| 433 outOffset = 0; | 433 outOffset = 0; |
| 434 int j = offset; | 434 int j = offset; |
| 435 for (int i = 0; i < bytes; i++) { | 435 for (int i = 0; i < bytes; i++) { |
| 436 outBuffer[i] = buffer[j]; | 436 int value = buffer[j]; |
| 437 if (value is! int) { |
| 438 throw new FileIOException( |
| 439 "List element is not an integer at index $j"); |
| 440 } |
| 441 outBuffer[i] = value; |
| 437 j++; | 442 j++; |
| 438 } | 443 } |
| 439 } | 444 } |
| 440 return writeListNative(id, outBuffer, outOffset, bytes); | 445 return writeListNative(id, outBuffer, outOffset, bytes); |
| 441 } | 446 } |
| 442 static int writeListNative(int id, List<int> buffer, int offset, int bytes) | 447 static int writeListNative(int id, List<int> buffer, int offset, int bytes) |
| 443 native "File_WriteList"; | 448 native "File_WriteList"; |
| 444 static int writeString(int id, String string) native "File_WriteString"; | 449 static int writeString(int id, String string) native "File_WriteString"; |
| 445 static int position(int id) native "File_Position"; | 450 static int position(int id) native "File_Position"; |
| 446 static bool setPosition(int id, int position) native "File_SetPosition"; | 451 static bool setPosition(int id, int position) native "File_SetPosition"; |
| (...skipping 635 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1082 var _readByteHandler; | 1087 var _readByteHandler; |
| 1083 var _readListHandler; | 1088 var _readListHandler; |
| 1084 var _noPendingWriteHandler; | 1089 var _noPendingWriteHandler; |
| 1085 var _positionHandler; | 1090 var _positionHandler; |
| 1086 var _setPositionHandler; | 1091 var _setPositionHandler; |
| 1087 var _truncateHandler; | 1092 var _truncateHandler; |
| 1088 var _lengthHandler; | 1093 var _lengthHandler; |
| 1089 var _flushHandler; | 1094 var _flushHandler; |
| 1090 var _errorHandler; | 1095 var _errorHandler; |
| 1091 } | 1096 } |
| OLD | NEW |