| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library barback.utils.file_pool; | 5 library barback.utils.file_pool; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 | 10 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 /// substantial additional throughput. | 27 /// substantial additional throughput. |
| 28 final Pool _pool = new Pool(32, timeout: new Duration(seconds: 60)); | 28 final Pool _pool = new Pool(32, timeout: new Duration(seconds: 60)); |
| 29 | 29 |
| 30 /// Opens the file at [path] for reading. | 30 /// Opens the file at [path] for reading. |
| 31 /// | 31 /// |
| 32 /// When the returned stream is listened to, if there are too many files | 32 /// When the returned stream is listened to, if there are too many files |
| 33 /// open, this will wait for a previously opened file to be closed and then | 33 /// open, this will wait for a previously opened file to be closed and then |
| 34 /// try again. | 34 /// try again. |
| 35 Stream<List<int>> openRead(String path) { | 35 Stream<List<int>> openRead(String path) { |
| 36 return futureStream(_pool.request().then((resource) { | 36 return futureStream(_pool.request().then((resource) { |
| 37 return new File(path).openRead().transform( | 37 return new File(path) |
| 38 new StreamTransformer.fromHandlers(handleDone: (sink) { | 38 .openRead() |
| 39 .transform(new StreamTransformer.fromHandlers(handleDone: (sink) { |
| 39 sink.close(); | 40 sink.close(); |
| 40 resource.release(); | 41 resource.release(); |
| 41 })); | 42 })); |
| 42 })); | 43 })); |
| 43 } | 44 } |
| 44 | 45 |
| 45 /// Reads [path] as a string using [encoding]. | 46 /// Reads [path] as a string using [encoding]. |
| 46 /// | 47 /// |
| 47 /// If there are too many files open and the read fails, this will wait for | 48 /// If there are too many files open and the read fails, this will wait for |
| 48 /// a previously opened file to be closed and then try again. | 49 /// a previously opened file to be closed and then try again. |
| 49 Future<String> readAsString(String path, Encoding encoding) { | 50 Future<String> readAsString(String path, Encoding encoding) { |
| 50 return _readAsBytes(path).then(encoding.decode); | 51 return _readAsBytes(path).then(encoding.decode); |
| 51 } | 52 } |
| 52 | 53 |
| 53 /// Reads [path] as a list of bytes, using [openRead] to retry if there are | 54 /// Reads [path] as a list of bytes, using [openRead] to retry if there are |
| 54 /// failures. | 55 /// failures. |
| 55 Future<List<int>> _readAsBytes(String path) { | 56 Future<List<int>> _readAsBytes(String path) { |
| 56 var completer = new Completer<List<int>>(); | 57 var completer = new Completer<List<int>>(); |
| 57 var builder = new BytesBuilder(); | 58 var builder = new BytesBuilder(); |
| 58 | 59 |
| 59 openRead(path).listen(builder.add, onDone: () { | 60 openRead(path).listen(builder.add, onDone: () { |
| 60 completer.complete(builder.takeBytes()); | 61 completer.complete(builder.takeBytes()); |
| 61 }, onError: completer.completeError, cancelOnError: true); | 62 }, onError: completer.completeError, cancelOnError: true); |
| 62 | 63 |
| 63 return completer.future; | 64 return completer.future; |
| 64 } | 65 } |
| 65 } | 66 } |
| OLD | NEW |