001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.vfs2.filter; 018 019import java.io.Serializable; 020 021import org.apache.commons.vfs2.FileContent; 022import org.apache.commons.vfs2.FileFilter; 023import org.apache.commons.vfs2.FileObject; 024import org.apache.commons.vfs2.FileSelectInfo; 025import org.apache.commons.vfs2.FileSystemException; 026 027/** 028 * Filters files based on size, can filter either smaller files or files equal 029 * to or larger than a given threshold. 030 * <p> 031 * For example, to print all files and directories in the current directory 032 * whose size is greater than 1 MB: 033 * </p> 034 * 035 * <pre> 036 * FileSystemManager fsManager = VFS.getManager(); 037 * FileObject dir = fsManager.toFileObject(new File(".")); 038 * SizeFileFilter filter = new SizeFileFilter(1024 * 1024); 039 * FileObject[] files = dir.findFiles(new FileFilterSelector(filter)); 040 * for (int i = 0; i < files.length; i++) { 041 * System.out.println(files[i]); 042 * } 043 * </pre> 044 * 045 * @author This code was originally ported from Apache Commons IO File Filter 046 * @see "https://commons.apache.org/proper/commons-io/" 047 * @since 2.4 048 */ 049public class SizeFileFilter implements FileFilter, Serializable { 050 051 private static final long serialVersionUID = 1L; 052 053 /** Whether the files accepted will be larger or smaller. */ 054 private final boolean acceptLarger; 055 056 /** The size threshold. */ 057 private final long size; 058 059 /** 060 * Constructs a new size file filter for files equal to or larger than a certain 061 * size. 062 * 063 * @param size the threshold size of the files - Must be non-negative. 064 */ 065 public SizeFileFilter(final long size) { 066 this(size, true); 067 } 068 069 /** 070 * Constructs a new size file filter for files based on a certain size 071 * threshold. 072 * 073 * @param size the threshold size of the files - Must be non-negative. 074 * @param acceptLarger if true, files equal to or larger are accepted, otherwise 075 * smaller ones (but not equal to) 076 */ 077 public SizeFileFilter(final long size, final boolean acceptLarger) { 078 if (size < 0) { 079 throw new IllegalArgumentException("The size must be non-negative"); 080 } 081 this.size = size; 082 this.acceptLarger = acceptLarger; 083 } 084 085 /** 086 * Checks to see if the size of the file is favorable. 087 * <p> 088 * If size equals threshold and smaller files are required, file <strong>IS NOT</strong> 089 * selected. If size equals threshold and larger files are required, file 090 * <strong>IS</strong> selected. 091 * </p> 092 * <p> 093 * Non-existing files return always false (will never be accepted). 094 * </p> 095 * 096 * @param fileSelectInfo the File to check 097 * @return true if the file name matches 098 * @throws FileSystemException Thrown for file system errors. 099 */ 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}