1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.any23.util;
19
20 import java.io.BufferedInputStream;
21 import java.io.BufferedOutputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.FileOutputStream;
27 import java.io.FilenameFilter;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.OutputStreamWriter;
31 import java.io.PrintWriter;
32 import java.io.Writer;
33 import java.nio.charset.StandardCharsets;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Locale;
37
38
39
40
41
42
43 public class FileUtils {
44
45
46
47
48
49
50
51
52
53
54
55 public static File mv(File target, File dest) {
56 if (!dest.isDirectory()) {
57 throw new IllegalArgumentException("destination must be a directory.");
58 }
59
60 final File newFile = new File(dest, target.getName());
61 boolean success = target.renameTo(newFile);
62 if (!success) {
63 throw new IllegalStateException(
64 String.format(Locale.ROOT, "Cannot move target file [%s] to destination [%s]", target, newFile));
65 }
66 return newFile;
67 }
68
69
70
71
72
73
74
75
76
77 public static void cp(InputStream is, File dest) {
78 if (dest.exists()) {
79 throw new IllegalArgumentException("Destination must not exist.");
80 }
81 BufferedInputStream bis = null;
82 BufferedOutputStream bos = null;
83 try {
84 bis = new BufferedInputStream(is);
85 FileOutputStream fos = new FileOutputStream(dest);
86 bos = new BufferedOutputStream(fos);
87 final byte[] buffer = new byte[1024 * 4];
88 int read;
89 while (true) {
90 read = bis.read(buffer);
91 if (read == -1) {
92 break;
93 }
94 bos.write(buffer, 0, read);
95 }
96 } catch (Exception e) {
97 throw new RuntimeException("Error while copying stream into file.", e);
98 } finally {
99 StreamUtils.closeGracefully(bis);
100 StreamUtils.closeGracefully(bos);
101 }
102 }
103
104
105
106
107
108
109
110
111
112
113
114
115 public static void cp(File src, File dest) throws FileNotFoundException {
116 FileInputStream fis = null;
117 try {
118 fis = new FileInputStream(src);
119 cp(fis, dest);
120 } finally {
121 StreamUtils.closeGracefully(fis);
122 }
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136 public static void dumpContent(File f, String content) throws IOException {
137 Writer fw = null;
138 try {
139 fw = new OutputStreamWriter(new FileOutputStream(f), StandardCharsets.UTF_8);
140 fw.write(content);
141 } finally {
142 StreamUtils.closeGracefully(fw);
143 }
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157 public static void dumpContent(File f, Throwable t) throws IOException {
158 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
159 final PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8), true);
160 t.printStackTrace(pw);
161 pw.close();
162 dumpContent(f, baos.toString("UTF-8"));
163 }
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178 public static String readResourceContent(Class clazz, String resource) throws IOException {
179 return StreamUtils.asString(clazz.getResourceAsStream(resource));
180 }
181
182
183
184
185
186
187
188
189
190
191
192
193 public static String readResourceContent(String resource) throws IOException {
194 return readResourceContent(FileUtils.class, resource);
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208 public static String readFileContent(File f) throws IOException {
209 FileInputStream fis = new FileInputStream(f);
210 return StreamUtils.asString(fis, true);
211 }
212
213
214
215
216
217
218
219
220
221
222
223
224 public static String[] readFileLines(File f) throws IOException {
225 FileInputStream fis = new FileInputStream(f);
226 return StreamUtils.asLines(fis);
227 }
228
229
230
231
232
233
234
235
236
237
238
239 public static File[] listFilesRecursively(File dir, FilenameFilter filenameFilter) {
240 if (!dir.isDirectory()) {
241 throw new IllegalArgumentException(dir.getAbsolutePath() + " must be a directory.");
242 }
243 final List<File> result = new ArrayList<File>();
244 visitFilesRecursively(dir, filenameFilter, result);
245 return result.toArray(new File[result.size()]);
246 }
247
248
249
250
251
252
253
254
255
256
257
258 private static void visitFilesRecursively(File dir, FilenameFilter filenameFilter, List<File> result) {
259 for (File file : dir.listFiles()) {
260 if (!file.isDirectory()) {
261 if (filenameFilter == null || filenameFilter.accept(dir, file.getName())) {
262 result.add(file);
263 }
264 } else {
265 visitFilesRecursively(file, filenameFilter, result);
266 }
267 }
268 }
269
270
271
272
273 private FileUtils() {
274 }
275
276 }