| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
| 4 | 4 |
| 5 library fasta.scanner.io; | 5 library fasta.scanner.io; |
| 6 | 6 |
| 7 import 'dart:async' show Future; | 7 import 'dart:async' show Future; |
| 8 | 8 |
| 9 import 'dart:io' show File, RandomAccessFile; | 9 import 'dart:io' show File, RandomAccessFile; |
| 10 | 10 |
| 11 import 'dart:typed_data' show Uint8List; | 11 import 'dart:typed_data' show Uint8List; |
| 12 | 12 |
| 13 List<int> readBytesFromFileSync(Uri uri) { | 13 List<int> readBytesFromFileSync(Uri uri) { |
| 14 RandomAccessFile file = new File.fromUri(uri).openSync(); | 14 RandomAccessFile file = new File.fromUri(uri).openSync(); |
| 15 Uint8List list; | 15 Uint8List list; |
| 16 try { | 16 try { |
| 17 int length = file.lengthSync(); | 17 int length = file.lengthSync(); |
| 18 // +1 to have a 0 terminated list, see [Scanner]. | 18 // +1 to have a 0 terminated list, see [Scanner]. |
| 19 list = new Uint8List(length + 1); | 19 list = new Uint8List(length + 1); |
| 20 file.readIntoSync(list, 0, length); | 20 file.readIntoSync(list, 0, length); |
| 21 } finally { | 21 } finally { |
| 22 file.closeSync(); | 22 file.closeSync(); |
| 23 } | 23 } |
| 24 return list; | 24 return list; |
| 25 } | 25 } |
| 26 | 26 |
| 27 Future<List<int>> readBytesFromFile(Uri uri) async { | 27 Future<List<int>> readBytesFromFile(Uri uri, |
| 28 {bool ensureZeroTermination: true}) async { |
| 28 RandomAccessFile file = await new File.fromUri(uri).open(); | 29 RandomAccessFile file = await new File.fromUri(uri).open(); |
| 29 Uint8List list; | 30 Uint8List list; |
| 30 try { | 31 try { |
| 31 int length = await file.length(); | 32 int length = await file.length(); |
| 32 // +1 to have a 0 terminated list, see [Scanner]. | 33 // +1 to have a 0 terminated list, see [Scanner]. |
| 33 list = new Uint8List(length + 1); | 34 list = new Uint8List(ensureZeroTermination ? length + 1 : length); |
| 34 int read = await file.readInto(list); | 35 int read = await file.readInto(list); |
| 35 if (read != length) { | 36 if (read != length) { |
| 36 throw "Error reading file: ${uri}"; | 37 throw "Error reading file: ${uri}"; |
| 37 } | 38 } |
| 38 } finally { | 39 } finally { |
| 39 await file.close(); | 40 await file.close(); |
| 40 } | 41 } |
| 41 return list; | 42 return list; |
| 42 } | 43 } |
| OLD | NEW |