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.file_pool; | 5 library barback.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 |
11 import 'pool.dart'; | 11 import 'pool.dart'; |
12 import 'utils.dart'; | 12 import 'utils.dart'; |
13 | 13 |
14 /// Manages a pool of files that are opened for reading to cope with maximum | 14 /// Manages a pool of files that are opened for reading to cope with maximum |
15 /// file descriptor limits. | 15 /// file descriptor limits. |
16 /// | 16 /// |
17 /// If a file cannot be opened because too many files are already open, this | 17 /// If a file cannot be opened because too many files are already open, this |
18 /// will defer the open until a previously opened file is closed and then try | 18 /// will defer the open until a previously opened file is closed and then try |
19 /// again. If this doesn't succeed after a certain amount of time, the open | 19 /// again. If this doesn't succeed after a certain amount of time, the open |
20 /// will fail and the original "too many files" exception will be thrown. | 20 /// will fail and the original "too many files" exception will be thrown. |
21 class FilePool { | 21 class FilePool { |
22 /// The underlying pool. | 22 /// The underlying pool. |
23 /// | 23 /// |
24 /// The maximum number of allocated descriptors is based on empirical tests | 24 /// The maximum number of allocated descriptors is based on empirical tests |
25 /// that indicate that beyond 32, additional file reads don't provide | 25 /// that indicate that beyond 32, additional file reads don't provide |
26 /// substantial additional throughput. | 26 /// substantial additional throughput. |
27 final Pool _pool = new Pool(32, timeout: new Duration(seconds: 60)); | 27 final Pool _pool = new Pool(32, timeout: new Duration(seconds: 60)); |
28 | 28 |
29 /// Opens [file] for reading. | 29 /// Opens the file at [path] for reading. |
30 /// | 30 /// |
31 /// When the returned stream is listened to, if there are too many files | 31 /// When the returned stream is listened to, if there are too many files |
32 /// open, this will wait for a previously opened file to be closed and then | 32 /// open, this will wait for a previously opened file to be closed and then |
33 /// try again. | 33 /// try again. |
34 Stream<List<int>> openRead(File file) { | 34 Stream<List<int>> openRead(String path) { |
35 return futureStream(_pool.request().then((resource) { | 35 return futureStream(_pool.request().then((resource) { |
36 return file.openRead().transform(new StreamTransformer.fromHandlers( | 36 return new File(path).openRead().transform( |
37 handleDone: (sink) { | 37 new StreamTransformer.fromHandlers(handleDone: (sink) { |
38 sink.close(); | 38 sink.close(); |
39 resource.release(); | 39 resource.release(); |
40 })); | 40 })); |
41 })); | 41 })); |
42 } | 42 } |
43 | 43 |
44 /// Reads [file] as a string using [encoding]. | 44 /// Reads [path] as a string using [encoding]. |
45 /// | 45 /// |
46 /// If there are too many files open and the read fails, this will wait for | 46 /// If there are too many files open and the read fails, this will wait for |
47 /// a previously opened file to be closed and then try again. | 47 /// a previously opened file to be closed and then try again. |
48 Future<String> readAsString(File file, Encoding encoding) { | 48 Future<String> readAsString(String path, Encoding encoding) { |
49 return _readAsBytes(file).then(encoding.decode); | 49 return _readAsBytes(path).then(encoding.decode); |
50 } | 50 } |
51 | 51 |
52 /// Reads [file] as a list of bytes, using [openRead] to retry if there are | 52 /// Reads [path] as a list of bytes, using [openRead] to retry if there are |
53 /// failures. | 53 /// failures. |
54 Future<List<int>> _readAsBytes(File file) { | 54 Future<List<int>> _readAsBytes(String path) { |
55 var completer = new Completer<List<int>>(); | 55 var completer = new Completer<List<int>>(); |
56 var builder = new BytesBuilder(); | 56 var builder = new BytesBuilder(); |
57 | 57 |
58 openRead(file).listen(builder.add, onDone: () { | 58 openRead(path).listen(builder.add, onDone: () { |
59 completer.complete(builder.takeBytes()); | 59 completer.complete(builder.takeBytes()); |
60 }, onError: completer.completeError, cancelOnError: true); | 60 }, onError: completer.completeError, cancelOnError: true); |
61 | 61 |
62 return completer.future; | 62 return completer.future; |
63 } | 63 } |
64 } | 64 } |
OLD | NEW |