1 package org.catacomb.util;
2
3 import org.catacomb.report.E;
4
5 import java.io.*;
6 import java.util.zip.GZIPInputStream;
7 import java.util.zip.GZIPOutputStream;
8
9 import java.util.ArrayList;
10
11
12 public abstract class FileUtil {
13
14
15
16 public static byte[] readHeader(File f, int n) {
17 byte[] ret = null;
18 try {
19 FileInputStream ins = new FileInputStream(f);
20 ret = new byte[n];
21 int nread = ins.read(ret);
22 if (nread != n) {
23 E.error("readNBytes wanted " + n + " but got " + nread);
24 }
25 ins.close();
26 } catch (Exception ex) {
27 E.error("readNBytes problem " + ex);
28 }
29 return ret;
30 }
31
32
33
34 public static byte[] readBytes(File f) {
35 byte[] ret = null;
36 try {
37 FileInputStream fis = new FileInputStream(f);
38 BufferedInputStream bis = new BufferedInputStream(fis);
39 ByteArrayOutputStream baos = new ByteArrayOutputStream();
40
41 byte[] bb = new byte[4096];
42 int nread = bis.read(bb);
43 while (nread > 0) {
44 baos.write(bb, 0, nread);
45 nread = bis.read(bb);
46 }
47 ret = baos.toByteArray();
48
49 } catch (Exception ex) {
50 E.error("readNBytes problem " + ex);
51 }
52 return ret;
53 }
54
55
56
57 public static String readStringFromFile(File f) {
58 String sdat = "null";
59 if (f != null) {
60 try {
61 boolean dogz = (f.getName().endsWith(".gz"));
62 InputStream ins = new FileInputStream(f);
63 if (dogz) {
64 ins = new GZIPInputStream(ins);
65 }
66 InputStreamReader insr = new InputStreamReader(ins);
67 BufferedReader fr = new BufferedReader(insr);
68
69 StringBuffer sb = new StringBuffer();
70 while (fr.ready()) {
71 sb.append(fr.readLine());
72 sb.append("\n");
73 }
74 fr.close();
75 sdat = sb.toString();
76
77 } catch (IOException ex) {
78 E.error("Cant read file " + f);
79 }
80 }
81 return sdat;
82 }
83
84
85
86 public static boolean writeStringToFile(String sdat, File f) {
87 String fnm = f.getName();
88 boolean ok = false;
89 if (f != null) {
90 boolean dogz = (fnm.endsWith(".gz"));
91 try {
92 OutputStream fos = new FileOutputStream(f);
93 if (dogz) {
94 fos = new GZIPOutputStream(fos);
95 }
96 OutputStreamWriter osw = new OutputStreamWriter(fos);
97
98 osw.write(sdat, 0, sdat.length());
99 osw.close();
100 ok = true;
101
102 } catch (IOException ex) {
103 E.error("file writing error, trying to write file " + fnm);
104 ex.printStackTrace();
105 }
106 }
107 return ok;
108 }
109
110
111
112 public static String getRootName(File f) {
113 String fnm = f.getName();
114 String root = fnm.substring(0, fnm.lastIndexOf("."));
115 return root;
116 }
117
118
119
120 public static void writeBytes(byte[] ba, File f) {
121 writeByteArrayToFile(ba, f);
122 }
123
124
125 public static void writeByteArrayToFile(byte[] ba, File f) {
126 if (f == null) {
127 return;
128 }
129 try {
130 OutputStream os = new BufferedOutputStream(new FileOutputStream(f));
131 os.write(ba);
132 os.flush();
133 } catch (Exception e) {
134 E.error("cant write byte array " + ba + " to " + f);
135 }
136 }
137
138
139
140 public static void copyFile(File fsrc, File fdest) {
141 if (fsrc.exists()) {
142 try {
143 InputStream in = new FileInputStream(fsrc);
144 OutputStream out = new FileOutputStream(fdest);
145
146
147 byte[] buf = new byte[1024];
148 int len;
149 while ((len = in.read(buf)) > 0) {
150 out.write(buf, 0, len);
151 }
152 in.close();
153 out.close();
154 } catch (Exception ex) {
155 E.error("file copy exception");
156 }
157
158 } else {
159 E.warning("copy - missing file " + fsrc);
160 }
161 }
162
163
164
165 public static String findPath(File f, String name) {
166 String ret = null;
167
168 for (File fs : f.listFiles()) {
169 if (fs.getName().equals(name)) {
170 ret = "";
171 break;
172 }
173 }
174
175 if (ret == null) {
176 for (File fd : f.listFiles()) {
177 if (fd.isDirectory()) {
178 String s = findPath(fd, name);
179 if (s != null) {
180 if (s.equals("")) {
181 ret = fd.getName();
182 } else {
183 ret = fd.getName() + "/" + s;
184 }
185 break;
186 }
187 }
188 }
189 }
190 return ret;
191 }
192
193
194
195 public static String readFirstLine(File f) {
196
197 String ret = null;
198 if (f != null) {
199 try {
200 InputStream ins = new FileInputStream(f);
201 InputStreamReader insr = new InputStreamReader(ins);
202 BufferedReader fr = new BufferedReader(insr);
203 ret = fr.readLine();
204 fr.close();
205
206 } catch (IOException ex) {
207 E.error("file read error ");
208 ex.printStackTrace();
209 }
210 }
211 return ret;
212 }
213
214
215
216 public static String getRelativeDirectory(File ftgt, File rtFolder) {
217 File fpar = ftgt.getParentFile();
218 int ns = 0;
219
220 String sret = null;
221
222 while (fpar != null && !(fpar.equals(rtFolder))) {
223 if (sret == null) {
224 sret = fpar.getName();
225 } else {
226 sret = fpar.getName() + "/" + sret;
227 }
228 fpar = fpar.getParentFile();
229
230 ns += 1;
231 if (ns > 8) {
232 E.error("too many steps trying to get relative files ? " + ftgt.getAbsolutePath() + " "
233 + rtFolder.getAbsolutePath());
234 break;
235 }
236 }
237
238 return sret;
239 }
240
241
242
243 public static ArrayList<File> matchingFiles(String srcPattern) {
244 ArrayList<File> ret = new ArrayList<File>();
245 if (srcPattern.indexOf("*") < 0) {
246 File fd = new File(srcPattern);
247 if (fd.exists() && fd.isDirectory()) {
248 for (File f : fd.listFiles()) {
249 ret.add(f);
250 }
251 }
252
253 } else {
254 int istar = srcPattern.indexOf("*");
255 String sa = srcPattern.substring(0, istar);
256 String sb = srcPattern.substring(istar + 1, srcPattern.length());
257 File ftop = new File(sa);
258 for (File fg : ftop.listFiles()) {
259 File fp = new File(fg, sb);
260 if (fp.exists()) {
261 ret.add(fp);
262 }
263 }
264 }
265 return ret;
266 }
267
268
269
270 public static void deleteDir(File fdir) {
271 for (File f : fdir.listFiles()) {
272 if (f.isDirectory()) {
273 deleteDir(f);
274 } else {
275 f.delete();
276 }
277 }
278 fdir.delete();
279 }
280
281
282
283 public static File getTempFolder() {
284 String s = System.getProperty("java.io.tmpdir");
285 File fsystmp = new File(s);
286 long l = System.currentTimeMillis();
287 File ftmp = new File(fsystmp, "ccmb" + l);
288 ftmp.mkdir();
289 return ftmp;
290 }
291
292
293
294 public static void clearCache(File fparam) {
295 String fnm = fparam.getName();
296
297 int ild = fnm.lastIndexOf(".");
298 if (ild > 0) {
299 File fcls = new File(fparam.getParent(), fnm.substring(0, ild) + ".class");
300 if (fcls.exists()) {
301 fcls.delete();
302 } else {
303
304 }
305 }
306
307 }
308
309
310
311 public static File getSiblingFile(File fme, String ext) {
312 String fnm = fme.getName();
313 int ild = fnm.lastIndexOf(".");
314 if (ild > 1) {
315 fnm = fnm.substring(0, ild);
316 }
317 File fret = new File(fme.getParentFile(), fnm + ext);
318 return fret;
319
320 }
321
322
323
324 public static ArrayList<String> getPathElements(File rootDir, File ftgt) {
325
326 ArrayList<String> elts = new ArrayList<String>();
327 File fpar = ftgt.getParentFile();
328 int ns = 0;
329
330 while (fpar != null && !(fpar.equals(rootDir))) {
331 elts.add(0, fpar.getName());
332 fpar = fpar.getParentFile();
333 ns += 1;
334 if (ns > 8) {
335 E.error("too many steps trying to get relative files ? " + ftgt.getAbsolutePath() + " "
336 + rootDir.getAbsolutePath());
337 break;
338 }
339 }
340
341 return elts;
342 }
343
344
345
346
347
348 }