View Javadoc
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.FileFilter;
22  import org.apache.commons.vfs2.FileSelectInfo;
23  import org.apache.commons.vfs2.FileSystemException;
24  
25  /**
26   * This filter accepts {@code File}s that can be read.
27   * <p>
28   * Example, showing how to print out a list of the current directory's
29   * <i>readable</i> files:
30   * </p>
31   *
32   * <pre>
33   * FileSystemManager fsManager = VFS.getManager();
34   * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
35   * FileObject[] files = dir.findFiles(new FileFilterSelector(CanReadFileFilter.CAN_READ));
36   * for (int i = 0; i &lt; files.length; i++) {
37   *     System.out.println(files[i]);
38   * }
39   * </pre>
40   *
41   * <p>
42   * Example, showing how to print out a list of the current directory's
43   * <i>un-readable</i> files:
44   * </p>
45   *
46   * <pre>
47   * FileSystemManager fsManager = VFS.getManager();
48   * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
49   * FileObject[] files = dir.findFiles(new FileFilterSelector(CanReadFileFilter.CANNOT_READ));
50   * for (int i = 0; i &lt; files.length; i++) {
51   *     System.out.println(files[i]);
52   * }
53   * </pre>
54   *
55   * <p>
56   * Example, showing how to print out a list of the current directory's
57   * <i>read-only</i> files:
58   * </p>
59   *
60   * <pre>
61   * FileSystemManager fsManager = VFS.getManager();
62   * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
63   * FileObject[] files = dir.findFiles(new FileFilterSelector(CanReadFileFilter.READ_ONLY));
64   * for (int i = 0; i &lt; files.length; i++) {
65   *     System.out.println(files[i]);
66   * }
67   * </pre>
68   *
69   * @author This code was originally ported from Apache Commons IO File Filter
70   * @see "http://commons.apache.org/proper/commons-io/"
71   * @since 2.4
72   */
73  public class CanReadFileFilter implements FileFilter, Serializable {
74  
75      /** Singleton instance of <i>readable</i> filter. */
76      public static final FileFilter CAN_READ = new CanReadFileFilter();
77  
78      /** Singleton instance of not <i>readable</i> filter. */
79      public static final FileFilter CANNOT_READ = new NotFileFilter(CAN_READ);
80  
81      /** Singleton instance of <i>read-only</i> filter. */
82      public static final FileFilter READ_ONLY = new AndFileFilter(CAN_READ, CanWriteFileFilter.CANNOT_WRITE);
83  
84      private static final long serialVersionUID = 1L;
85  
86      /**
87       * Restrictive constructor.
88       */
89      protected CanReadFileFilter() {
90      }
91  
92      /**
93       * Checks to see if the file can be read.
94       *
95       * @param fileSelectInfo the File to check.
96       *
97       * @return {@code true} if the file can be read, otherwise {@code false}.
98       * @throws FileSystemException Thrown for file system errors.
99       */
100     @Override
101     public boolean accept(final FileSelectInfo fileSelectInfo) throws FileSystemException {
102         return fileSelectInfo.getFile().isReadable();
103     }
104 
105 }