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 package org.apache.commons.vfs2.filter;
18
19 import java.io.Serializable;
20
21 import org.apache.commons.vfs2.FileContent;
22 import org.apache.commons.vfs2.FileFilter;
23 import org.apache.commons.vfs2.FileObject;
24 import org.apache.commons.vfs2.FileSelectInfo;
25 import org.apache.commons.vfs2.FileSystemException;
26
27 /**
28 * Filters files based on size, can filter either smaller files or files equal
29 * to or larger than a given threshold.
30 * <p>
31 * For example, to print all files and directories in the current directory
32 * whose size is greater than 1 MB:
33 * </p>
34 *
35 * <pre>
36 * FileSystemManager fsManager = VFS.getManager();
37 * FileObject dir = fsManager.toFileObject(new File("."));
38 * SizeFileFilter filter = new SizeFileFilter(1024 * 1024);
39 * FileObject[] files = dir.findFiles(new FileFilterSelector(filter));
40 * for (int i = 0; i < files.length; i++) {
41 * System.out.println(files[i]);
42 * }
43 * </pre>
44 *
45 * @author This code was originally ported from Apache Commons IO File Filter
46 * @see "https://commons.apache.org/proper/commons-io/"
47 * @since 2.4
48 */
49 public class SizeFileFilter implements FileFilter, Serializable {
50
51 private static final long serialVersionUID = 1L;
52
53 /** Whether the files accepted will be larger or smaller. */
54 private final boolean acceptLarger;
55
56 /** The size threshold. */
57 private final long size;
58
59 /**
60 * Constructs a new size file filter for files equal to or larger than a certain
61 * size.
62 *
63 * @param size the threshold size of the files - Must be non-negative.
64 */
65 public SizeFileFilter(final long size) {
66 this(size, true);
67 }
68
69 /**
70 * Constructs a new size file filter for files based on a certain size
71 * threshold.
72 *
73 * @param size the threshold size of the files - Must be non-negative.
74 * @param acceptLarger if true, files equal to or larger are accepted, otherwise
75 * smaller ones (but not equal to)
76 */
77 public SizeFileFilter(final long size, final boolean acceptLarger) {
78 if (size < 0) {
79 throw new IllegalArgumentException("The size must be non-negative");
80 }
81 this.size = size;
82 this.acceptLarger = acceptLarger;
83 }
84
85 /**
86 * Checks to see if the size of the file is favorable.
87 * <p>
88 * If size equals threshold and smaller files are required, file <strong>IS NOT</strong>
89 * selected. If size equals threshold and larger files are required, file
90 * <strong>IS</strong> selected.
91 * </p>
92 * <p>
93 * Non-existing files return always false (will never be accepted).
94 * </p>
95 *
96 * @param fileSelectInfo the File to check
97 * @return true if the file name matches
98 * @throws FileSystemException Thrown for file system errors.
99 */
100 @Override
101 public boolean accept(final FileSelectInfo fileSelectInfo) throws FileSystemException {
102 try (FileObject file = fileSelectInfo.getFile()) {
103 if (!file.exists()) {
104 return false;
105 }
106 try (FileContent content = file.getContent()) {
107 final long length = content.getSize();
108 final boolean smaller = length < size;
109 return acceptLarger != smaller;
110 }
111 }
112 }
113
114 /**
115 * Provide a String representation of this file filter.
116 *
117 * @return a String representation
118 */
119 @Override
120 public String toString() {
121 final String condition = acceptLarger ? ">=" : "<";
122 return super.toString() + "(" + condition + size + ")";
123 }
124
125 }