1 /*
2 * Fsgrep is a simple Java application which allows a user to
3 * search all files in a directory structure for lines matching
4 * a given pattern. Its functionality is a combination of the
5 * Unix 'find' and 'grep' utilities.
6 * Visit [http://fsgrep.sourceforge.net/] for more information.
7 *
8 * Copyright (C) 2003-2006 Murali Krishnan [murali_ca_us@users.sourceforge.net]
9 *
10 * Fsgrep is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
13 *
14 * Fsgrep is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Fsgrep (see the file named LICENSE.txt); if not, write
21 * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22 * Boston, MA 02111-1307 USA
23 */
24
25 package mk.fsgrep.find;
26
27
28 import java.io.*;
29
30
31 /***
32 * Determine whether the given file is a text file, and accept it if it is.
33 * This is done by reading the first 8 bytes of the file and seeing if they
34 * are valid text characters by checking if they fall withing the range
35 * [1, 255]. This only works for single-byte character sets.
36 *
37 * @author Murali Krishnan
38 *
39 */
40 public class TextFileFilter implements FilenameFilter {
41
42 //------------------------------------------------------------
43 //- Class Variables
44
45
46
47 //------------------------------------------------------------
48 //- Class Functions
49
50
51
52 //------------------------------------------------------------
53 //- Instance Variables
54
55
56
57 //------------------------------------------------------------
58 //- Constructors
59
60
61
62 //------------------------------------------------------------
63 //- Accessors
64
65
66
67 //------------------------------------------------------------
68 //- Settors
69
70
71
72 //------------------------------------------------------------
73 //- Private/Protected Utility Functions
74
75
76
77 //------------------------------------------------------------
78 //- Public Interface Functions
79
80
81
82 //------------------------------------------------------------
83 //- Class Interface Functions
84
85 public boolean accept(File dir, String name) {
86 boolean result = true; // Optimistic.
87
88 File file = new File(dir, name);
89 if (file.isFile()) {
90 long segmentToRead = Math.min(file.length(), 8);
91
92 try {
93 FileInputStream fis = new FileInputStream(file);
94 DataInputStream dis = new DataInputStream(fis);
95 for (long numChars=segmentToRead; ((numChars>0) && result); numChars--) {
96 byte bt = dis.readByte();
97 result = (bt > 0) && (bt < 256);
98 }
99 dis.close();
100 fis.close();
101 } catch (IOException ioe) {
102 System.err.println("Problem reading file [" + file + "].");
103 ioe.printStackTrace();
104 }
105 } else {
106 // Not a plain file; reject it.
107 result = false;
108 }
109
110 return result;
111 }
112
113
114
115 //------------------------------------------------------------
116 //- Inner Classes
117
118
119
120 //------------------------------------------------------------
121 //- Main
122
123
124
125 }