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