SizeFileFilter.java

  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.io.filefilter;

  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.io.Serializable;
  21. import java.nio.file.FileVisitResult;
  22. import java.nio.file.Files;
  23. import java.nio.file.Path;
  24. import java.nio.file.attribute.BasicFileAttributes;

  25. /**
  26.  * Filters files based on size, can filter either smaller files or
  27.  * files equal to or larger than a given threshold.
  28.  * <p>
  29.  * For example, to print all files and directories in the
  30.  * current directory whose size is greater than 1 MB:
  31.  * </p>
  32.  * <h2>Using Classic IO</h2>
  33.  * <pre>
  34.  * File dir = FileUtils.current();
  35.  * String[] files = dir.list(new SizeFileFilter(1024 * 1024));
  36.  * for (String file : files) {
  37.  *     System.out.println(file);
  38.  * }
  39.  * </pre>
  40.  *
  41.  * <h2>Using NIO</h2>
  42.  * <pre>
  43.  * final Path dir = PathUtils.current();
  44.  * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new SizeFileFilter(1024 * 1024));
  45.  * //
  46.  * // Walk one directory
  47.  * Files.<strong>walkFileTree</strong>(dir, Collections.emptySet(), 1, visitor);
  48.  * System.out.println(visitor.getPathCounters());
  49.  * System.out.println(visitor.getFileList());
  50.  * //
  51.  * visitor.getPathCounters().reset();
  52.  * //
  53.  * // Walk directory tree
  54.  * Files.<strong>walkFileTree</strong>(dir, visitor);
  55.  * System.out.println(visitor.getPathCounters());
  56.  * System.out.println(visitor.getDirList());
  57.  * System.out.println(visitor.getFileList());
  58.  * </pre>
  59.  * <h2>Deprecating Serialization</h2>
  60.  * <p>
  61.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  62.  * </p>
  63.  *
  64.  * @since 1.2
  65.  * @see FileFilterUtils#sizeFileFilter(long)
  66.  * @see FileFilterUtils#sizeFileFilter(long, boolean)
  67.  * @see FileFilterUtils#sizeRangeFileFilter(long, long)
  68.  */
  69. public class SizeFileFilter extends AbstractFileFilter implements Serializable {

  70.     private static final long serialVersionUID = 7388077430788600069L;

  71.     /** Whether the files accepted will be larger or smaller. */
  72.     private final boolean acceptLarger;

  73.     /** The size threshold. */
  74.     private final long size;

  75.     /**
  76.      * Constructs a new size file filter for files equal to or
  77.      * larger than a certain size.
  78.      *
  79.      * @param size  the threshold size of the files
  80.      * @throws IllegalArgumentException if the size is negative
  81.      */
  82.     public SizeFileFilter(final long size) {
  83.         this(size, true);
  84.     }

  85.     /**
  86.      * Constructs a new size file filter for files based on a certain size
  87.      * threshold.
  88.      *
  89.      * @param size  the threshold size of the files
  90.      * @param acceptLarger  if true, files equal to or larger are accepted,
  91.      * otherwise smaller ones (but not equal to)
  92.      * @throws IllegalArgumentException if the size is negative
  93.      */
  94.     public SizeFileFilter(final long size, final boolean acceptLarger) {
  95.         if (size < 0) {
  96.             throw new IllegalArgumentException("The size must be non-negative");
  97.         }
  98.         this.size = size;
  99.         this.acceptLarger = acceptLarger;
  100.     }

  101.     /**
  102.      * Checks to see if the size of the file is favorable.
  103.      * <p>
  104.      * If size equals threshold and smaller files are required,
  105.      * file <strong>IS NOT</strong> selected.
  106.      * If size equals threshold and larger files are required,
  107.      * file <strong>IS</strong> selected.
  108.      * </p>
  109.      *
  110.      * @param file  the File to check
  111.      * @return true if the file name matches
  112.      */
  113.     @Override
  114.     public boolean accept(final File file) {
  115.         return accept(file != null ? file.length() : 0);
  116.     }

  117.     private boolean accept(final long length) {
  118.         return acceptLarger != length < size;
  119.     }

  120.     /**
  121.      * Checks to see if the size of the file is favorable.
  122.      * <p>
  123.      * If size equals threshold and smaller files are required, file <strong>IS NOT</strong> selected. If size equals threshold and larger files are required,
  124.      * file <strong>IS</strong> selected.
  125.      * </p>
  126.      *
  127.      * @param file       the File to check
  128.      * @param attributes the path's basic attributes (may be null).
  129.      * @return true if the file name matches
  130.      */
  131.     @Override
  132.     public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
  133.         return get(() -> toFileVisitResult(accept(Files.size(file))));
  134.     }

  135.     /**
  136.      * Provide a String representation of this file filter.
  137.      *
  138.      * @return a String representation
  139.      */
  140.     @Override
  141.     public String toString() {
  142.         final String condition = acceptLarger ? ">=" : "<";
  143.         return super.toString() + "(" + condition + size + ")";
  144.     }

  145.     @Override
  146.     public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
  147.         return toFileVisitResult(accept(Files.size(file)));
  148.     }

  149. }