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 * https://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
19 import java.io.File;
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 /**
27 * This filter accepts {@link File}s that can be read.
28 * <p>
29 * Example, showing how to print out a list of the current directory's <em>readable</em> files:
30 * </p>
31 * <h2>Using Classic IO</h2>
32 * <pre>
33 * File dir = FileUtils.current();
34 * String[] files = dir.list(CanReadFileFilter.CAN_READ);
35 * for (String file : files) {
36 * System.out.println(file);
37 * }
38 * </pre>
39 *
40 * <p>
41 * Example, showing how to print out a list of the current directory's <em>un-readable</em> files:
42 *
43 * <pre>
44 * File dir = FileUtils.current();
45 * String[] files = dir.list(CanReadFileFilter.CANNOT_READ);
46 * for (String file : files) {
47 * System.out.println(file);
48 * }
49 * </pre>
50 *
51 * <p>
52 * Example, showing how to print out a list of the current directory's <em>read-only</em> files:
53 *
54 * <pre>
55 * File dir = FileUtils.current();
56 * String[] files = dir.list(CanReadFileFilter.READ_ONLY);
57 * for (String file : files) {
58 * System.out.println(file);
59 * }
60 * </pre>
61 * <h2>Deprecating Serialization</h2>
62 * <p>
63 * <em>Serialization is deprecated and will be removed in 3.0.</em>
64 * </p>
65 *
66 * @since 1.3
67 */
68 public class CanReadFileFilter extends AbstractFileFilter implements Serializable {
69
70 /** Singleton instance of <em>readable</em> filter */
71 public static final IOFileFilter CAN_READ = new CanReadFileFilter();
72
73 /** Singleton instance of not <em>readable</em> filter */
74 public static final IOFileFilter CANNOT_READ = CAN_READ.negate();
75
76 /** Singleton instance of <em>read-only</em> filter */
77 public static final IOFileFilter READ_ONLY = CAN_READ.and(CanWriteFileFilter.CANNOT_WRITE);
78
79 private static final long serialVersionUID = 3179904805251622989L;
80
81 /**
82 * Restrictive constructor.
83 */
84 protected CanReadFileFilter() {
85 }
86
87 /**
88 * Tests to see if the file can be read.
89 *
90 * @param file the File to check.
91 * @return {@code true} if the file can be read, otherwise {@code false}.
92 */
93 @Override
94 public boolean accept(final File file) {
95 return file != null && file.canRead();
96 }
97
98 /**
99 * Tests to see if the file can be read.
100 *
101 * @param file the File to check.
102 * @param attributes the path's basic attributes (may be null).
103 * @return {@code true} if the file can be read, otherwise {@code false}.
104 * @since 2.9.0
105 */
106 @Override
107 public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
108 return toFileVisitResult(file != null && Files.isReadable(file));
109 }
110
111 }