Skip to content

Commit fad92c0

Browse files
committed
NET-244 Add low-level support for FTPFileFilter filters
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/net/branches/NET_2_0@963317 13f79535-47bb-0310-9956-ffa450edef68
1 parent 2e4f332 commit fad92c0

File tree

3 files changed

+115
-3
lines changed

3 files changed

+115
-3
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.commons.net.ftp;
20+
21+
/**
22+
* Perform filtering on FTPFile entries.
23+
*/
24+
public interface FTPFileFilter {
25+
/**
26+
* Checks if an FTPFile entry should be included or not.
27+
*
28+
* @param file entry to be checked for inclusion. May be <code>null</code>.
29+
* @return <code>true</code> if the file is to be included, <code>false</code> otherwise
30+
*/
31+
public boolean accept(FTPFile file);
32+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.commons.net.ftp;
20+
21+
/**
22+
* Implements some simple FTPFileFilter classes.
23+
*/
24+
public class FTPFileFilters {
25+
26+
/**
27+
* Accepts all FTPFile entries, including null
28+
*/
29+
public static final FTPFileFilter ALL = new FTPFileFilter() {
30+
public boolean accept(FTPFile file) {
31+
return true;
32+
}
33+
};
34+
35+
/**
36+
* Accepts all non-null FTPFile entries
37+
*/
38+
public static final FTPFileFilter NON_NULL = new FTPFileFilter() {
39+
public boolean accept(FTPFile file) {
40+
return file != null;
41+
}
42+
};
43+
44+
/**
45+
* Accepts all (non-null) FTPFile directory entries
46+
*/
47+
public static final FTPFileFilter DIRECTORIES = new FTPFileFilter() {
48+
public boolean accept(FTPFile file) {
49+
return file != null && file.isDirectory();
50+
}
51+
};
52+
53+
}

src/main/java/org/apache/commons/net/ftp/FTPListParseEngine.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.io.InputStream;
2323
import java.io.InputStreamReader;
24+
import java.util.ArrayList;
2425
import java.util.Iterator;
2526
import java.util.LinkedList;
2627
import java.util.List;
@@ -247,14 +248,40 @@ public FTPFile[] getPrevious(int quantityRequested) {
247248
public FTPFile[] getFiles()
248249
throws IOException
249250
{
250-
List<FTPFile> tmpResults = new LinkedList<FTPFile>();
251+
return getFiles(FTPFileFilters.ALL);
252+
}
253+
254+
/**
255+
* Returns an array of FTPFile objects containing the whole list of
256+
* files returned by the server as read by this object's parser.
257+
* The files are filtered before being added to the array.
258+
*
259+
* @param filter FTPFileFilter, must not be <code>null</code>.
260+
*
261+
* @return an array of FTPFile objects containing the whole list of
262+
* files returned by the server as read by this object's parser.
263+
* <p><b>
264+
* NOTE:</b> This array may contain null members if any of the
265+
* individual file listings failed to parse. The caller should
266+
* check each entry for null before referencing it, or use the
267+
* a filter such as {@link FTPFileFilters#NON_NULL} which does not
268+
* allow null entries.
269+
*
270+
* @exception IOException
271+
*/
272+
public FTPFile[] getFiles(FTPFileFilter filter)
273+
throws IOException
274+
{
275+
List<FTPFile> tmpResults = new ArrayList<FTPFile>();
251276
Iterator<String> iter = this.entries.iterator();
252277
while (iter.hasNext()) {
253278
String entry = iter.next();
254279
FTPFile temp = this.parser.parseFTPEntry(entry);
255-
tmpResults.add(temp);
280+
if (filter.accept(temp)){
281+
tmpResults.add(temp);
282+
}
256283
}
257-
return tmpResults.toArray(new FTPFile[0]);
284+
return tmpResults.toArray(new FTPFile[tmpResults.size()]);
258285

259286
}
260287

0 commit comments

Comments
 (0)