CanReadFileFilter.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.Serializable;
  20. import java.nio.file.FileVisitResult;
  21. import java.nio.file.Files;
  22. import java.nio.file.Path;
  23. import java.nio.file.attribute.BasicFileAttributes;

  24. /**
  25.  * This filter accepts {@link File}s that can be read.
  26.  * <p>
  27.  * Example, showing how to print out a list of the current directory's <em>readable</em> files:
  28.  * </p>
  29.  * <h2>Using Classic IO</h2>
  30.  * <pre>
  31.  * File dir = FileUtils.current();
  32.  * String[] files = dir.list(CanReadFileFilter.CAN_READ);
  33.  * for (String file : files) {
  34.  *     System.out.println(file);
  35.  * }
  36.  * </pre>
  37.  *
  38.  * <p>
  39.  * Example, showing how to print out a list of the current directory's <em>un-readable</em> files:
  40.  *
  41.  * <pre>
  42.  * File dir = FileUtils.current();
  43.  * String[] files = dir.list(CanReadFileFilter.CANNOT_READ);
  44.  * for (String file : files) {
  45.  *     System.out.println(file);
  46.  * }
  47.  * </pre>
  48.  *
  49.  * <p>
  50.  * Example, showing how to print out a list of the current directory's <em>read-only</em> files:
  51.  *
  52.  * <pre>
  53.  * File dir = FileUtils.current();
  54.  * String[] files = dir.list(CanReadFileFilter.READ_ONLY);
  55.  * for (String file : files) {
  56.  *     System.out.println(file);
  57.  * }
  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.3
  65.  */
  66. public class CanReadFileFilter extends AbstractFileFilter implements Serializable {

  67.     /** Singleton instance of <em>readable</em> filter */
  68.     public static final IOFileFilter CAN_READ = new CanReadFileFilter();

  69.     /** Singleton instance of not <em>readable</em> filter */
  70.     public static final IOFileFilter CANNOT_READ = CAN_READ.negate();

  71.     /** Singleton instance of <em>read-only</em> filter */
  72.     public static final IOFileFilter READ_ONLY = CAN_READ.and(CanWriteFileFilter.CANNOT_WRITE);

  73.     private static final long serialVersionUID = 3179904805251622989L;

  74.     /**
  75.      * Restrictive constructor.
  76.      */
  77.     protected CanReadFileFilter() {
  78.     }

  79.     /**
  80.      * Checks to see if the file can be read.
  81.      *
  82.      * @param file the File to check.
  83.      * @return {@code true} if the file can be read, otherwise {@code false}.
  84.      */
  85.     @Override
  86.     public boolean accept(final File file) {
  87.         return file != null && file.canRead();
  88.     }

  89.     /**
  90.      * Checks to see if the file can be read.
  91.      *
  92.      * @param file the File to check.
  93.      * @param attributes the path's basic attributes (may be null).
  94.      * @return {@code true} if the file can be read, otherwise {@code false}.
  95.      * @since 2.9.0
  96.      */
  97.     @Override
  98.     public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
  99.         return toFileVisitResult(file != null && Files.isReadable(file));
  100.     }

  101. }