2
2
3
3
import java .io .BufferedReader ;
4
4
import java .io .File ;
5
- import java .io .FileFilter ;
6
5
import java .io .IOException ;
7
6
import java .io .InputStreamReader ;
8
7
import java .net .URL ;
8
+ import java .nio .file .DirectoryStream ;
9
+ import java .nio .file .Files ;
10
+ import java .nio .file .Path ;
9
11
import java .util .ArrayList ;
10
12
import java .util .Collections ;
11
13
import java .util .Enumeration ;
@@ -108,11 +110,13 @@ private void findPathPlugins() {
108
110
109
111
private void findPluginInPath (File dir , final Set <String > names ) {
110
112
if (dir .isDirectory () && dir .canRead ()){
111
- File [] matches = dir .listFiles (new FileFilter (){
112
- @ Override
113
- public boolean accept (File f ) {
114
- if (f .isFile () && f .canExecute () && f .getName ().toLowerCase ().startsWith ("ceylon-" )){
115
- String name = f .getName ().substring (7 );
113
+ // listing /usr/bin with >2k entries takes about 100ms using File.listFiles(Filter) and 39ms with NIO2
114
+ // and checking for file name before file type
115
+ DirectoryStream .Filter <Path > filter = new DirectoryStream .Filter <Path >() {
116
+ public boolean accept (Path f ) throws IOException {
117
+ String fileName = f .getFileName ().toString ();
118
+ if (fileName .toLowerCase ().startsWith ("ceylon-" ) && Files .isRegularFile (f ) && Files .isExecutable (f )){
119
+ String name = fileName .substring (7 );
116
120
if (OSUtil .isWindows ()){
117
121
// script must end with ".bat"
118
122
if (!name .toLowerCase ().endsWith (".bat" ))
@@ -131,10 +135,16 @@ public boolean accept(File f) {
131
135
}
132
136
return false ;
133
137
}
134
- });
135
- for (File sub : matches ){
136
- String name = SCRIPT_PREFIX +sub .getAbsolutePath ();
137
- pathPlugins .add (name );
138
+ };
139
+
140
+ try (DirectoryStream <Path > stream = Files .newDirectoryStream (dir .toPath (), filter )){
141
+ for (Path sub : stream ){
142
+ String name = SCRIPT_PREFIX +sub .toAbsolutePath ().toString ();
143
+ pathPlugins .add (name );
144
+ }
145
+ } catch (IOException e ) {
146
+ e .printStackTrace ();
147
+ // too bad, give up
138
148
}
139
149
}
140
150
}
0 commit comments