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 : _data = const [], | 7 : _data = const [], |
8 _position = 0, | 8 _position = 0, |
9 _filePosition = 0 { | 9 _filePosition = 0 { |
10 var file = new File(name); | 10 var file = new File(name); |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 | 328 |
329 static const SUCCESS_RESPONSE = 0; | 329 static const SUCCESS_RESPONSE = 0; |
330 static const ILLEGAL_ARGUMENT_RESPONSE = 1; | 330 static const ILLEGAL_ARGUMENT_RESPONSE = 1; |
331 static const OSERROR_RESPONSE = 2; | 331 static const OSERROR_RESPONSE = 2; |
332 static const FILE_CLOSED_RESPONSE = 3; | 332 static const FILE_CLOSED_RESPONSE = 3; |
333 | 333 |
334 static const ERROR_RESPONSE_ERROR_TYPE = 0; | 334 static const ERROR_RESPONSE_ERROR_TYPE = 0; |
335 static const OSERROR_RESPONSE_ERROR_CODE = 1; | 335 static const OSERROR_RESPONSE_ERROR_CODE = 1; |
336 static const OSERROR_RESPONSE_MESSAGE = 2; | 336 static const OSERROR_RESPONSE_MESSAGE = 2; |
337 | 337 |
338 static List ensureFastAndSerializableBuffer( | |
339 List buffer, int offset, int bytes) { | |
340 // When using the Dart C API to access raw data, using a ByteArray is | |
341 // currently much faster. This function will make a copy of the | |
342 // supplied List to a ByteArray if it isn't already. | |
343 List outBuffer; | |
344 int outOffset = offset; | |
345 if (buffer is Uint8List || buffer is ObjectArray) { | |
346 outBuffer = buffer; | |
347 } else { | |
348 outBuffer = new Uint8List(bytes); | |
349 outOffset = 0; | |
350 int j = offset; | |
351 for (int i = 0; i < bytes; i++) { | |
352 int value = buffer[j]; | |
353 if (value is! int) { | |
354 throw new FileIOException( | |
355 "List element is not an integer at index $j"); | |
356 } | |
357 outBuffer[i] = value; | |
358 j++; | |
359 } | |
360 } | |
361 return [outBuffer, outOffset]; | |
362 } | |
363 | |
364 static exists(String name) native "File_Exists"; | 338 static exists(String name) native "File_Exists"; |
365 static open(String name, int mode) native "File_Open"; | 339 static open(String name, int mode) native "File_Open"; |
366 static create(String name) native "File_Create"; | 340 static create(String name) native "File_Create"; |
367 static delete(String name) native "File_Delete"; | 341 static delete(String name) native "File_Delete"; |
368 static fullPath(String name) native "File_FullPath"; | 342 static fullPath(String name) native "File_FullPath"; |
369 static directory(String name) native "File_Directory"; | 343 static directory(String name) native "File_Directory"; |
370 static lengthFromName(String name) native "File_LengthFromName"; | 344 static lengthFromName(String name) native "File_LengthFromName"; |
371 static lastModified(String name) native "File_LastModified"; | 345 static lastModified(String name) native "File_LastModified"; |
372 static int close(int id) native "File_Close"; | 346 static int close(int id) native "File_Close"; |
373 static readByte(int id) native "File_ReadByte"; | 347 static readByte(int id) native "File_ReadByte"; |
374 static readList(int id, List<int> buffer, int offset, int bytes) | 348 static readList(int id, List<int> buffer, int offset, int bytes) |
375 native "File_ReadList"; | 349 native "File_ReadList"; |
376 static writeByte(int id, int value) native "File_WriteByte"; | 350 static writeByte(int id, int value) native "File_WriteByte"; |
377 static writeList(int id, List<int> buffer, int offset, int bytes) { | 351 static writeList(int id, List<int> buffer, int offset, int bytes) { |
378 List result = | 352 List result = _ensureFastAndSerializableBuffer(buffer, offset, bytes); |
379 _FileUtils.ensureFastAndSerializableBuffer(buffer, offset, bytes); | |
380 List outBuffer = result[0]; | 353 List outBuffer = result[0]; |
381 int outOffset = result[1]; | 354 int outOffset = result[1]; |
382 return writeListNative(id, outBuffer, outOffset, bytes); | 355 return writeListNative(id, outBuffer, outOffset, bytes); |
383 } | 356 } |
384 static writeListNative(int id, List<int> buffer, int offset, int bytes) | 357 static writeListNative(int id, List<int> buffer, int offset, int bytes) |
385 native "File_WriteList"; | 358 native "File_WriteList"; |
386 static writeString(int id, String string) native "File_WriteString"; | 359 static writeString(int id, String string) native "File_WriteString"; |
387 static position(int id) native "File_Position"; | 360 static position(int id) native "File_Position"; |
388 static setPosition(int id, int position) native "File_SetPosition"; | 361 static setPosition(int id, int position) native "File_SetPosition"; |
389 static truncate(int id, int length) native "File_Truncate"; | 362 static truncate(int id, int length) native "File_Truncate"; |
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
939 new Timer(0, (t) { | 912 new Timer(0, (t) { |
940 completer.completeException(new FileIOException( | 913 completer.completeException(new FileIOException( |
941 "Invalid arguments to writeList for file '$_name'")); | 914 "Invalid arguments to writeList for file '$_name'")); |
942 }); | 915 }); |
943 return completer.future; | 916 return completer.future; |
944 } | 917 } |
945 if (closed) return _completeWithClosedException(completer); | 918 if (closed) return _completeWithClosedException(completer); |
946 | 919 |
947 List result; | 920 List result; |
948 try { | 921 try { |
949 result = | 922 result = _ensureFastAndSerializableBuffer(buffer, offset, bytes); |
950 _FileUtils.ensureFastAndSerializableBuffer(buffer, offset, bytes); | |
951 } catch (e) { | 923 } catch (e) { |
952 // Complete asynchronously so the user has a chance to setup | 924 // Complete asynchronously so the user has a chance to setup |
953 // handlers without getting exceptions when registering the | 925 // handlers without getting exceptions when registering the |
954 // then handler. | 926 // then handler. |
955 new Timer(0, (t) => completer.completeException(e)); | 927 new Timer(0, (t) => completer.completeException(e)); |
956 return completer.future; | 928 return completer.future; |
957 } | 929 } |
958 List outBuffer = result[0]; | 930 List outBuffer = result[0]; |
959 int outOffset = result[1]; | 931 int outOffset = result[1]; |
960 | 932 |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1165 new FileIOException("File closed '$_name'")); | 1137 new FileIOException("File closed '$_name'")); |
1166 }); | 1138 }); |
1167 return completer.future; | 1139 return completer.future; |
1168 } | 1140 } |
1169 | 1141 |
1170 final String _name; | 1142 final String _name; |
1171 int _id; | 1143 int _id; |
1172 | 1144 |
1173 SendPort _fileService; | 1145 SendPort _fileService; |
1174 } | 1146 } |
OLD | NEW |