blob: f8a99da352a70d9f5a3791a794b3b058c3ecb876 [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.lib.io;
2
3import java.io.*;
4import java.net.*;
5import java.nio.*;
6import java.security.*;
7import java.util.*;
8
9public class IO {
10
11 public static void copy(Reader r, Writer w) throws IOException {
12 try {
13 char buffer[] = new char[8000];
14 int size = r.read(buffer);
15 while (size > 0) {
16 w.write(buffer, 0, size);
17 size = r.read(buffer);
18 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000019 }
20 finally {
Stuart McCullochbb014372012-06-07 21:57:32 +000021 r.close();
22 w.flush();
23 }
24 }
25
26 public static void copy(InputStream r, Writer w) throws IOException {
27 copy(r, w, "UTF-8");
28 }
29
Stuart McCulloch2286f232012-06-15 13:27:53 +000030 public static void copy(byte[] r, Writer w) throws IOException {
31 copy(new ByteArrayInputStream(r), w, "UTF-8");
Stuart McCullochbb014372012-06-07 21:57:32 +000032 }
33
Stuart McCulloch2286f232012-06-15 13:27:53 +000034 public static void copy(byte[] r, OutputStream w) throws IOException {
35 copy(new ByteArrayInputStream(r), w);
Stuart McCullochbb014372012-06-07 21:57:32 +000036 }
37
38 public static void copy(InputStream r, Writer w, String charset) throws IOException {
39 try {
40 InputStreamReader isr = new InputStreamReader(r, charset);
41 copy(isr, w);
Stuart McCulloch2286f232012-06-15 13:27:53 +000042 }
43 finally {
Stuart McCullochbb014372012-06-07 21:57:32 +000044 r.close();
45 }
46 }
47
48 public static void copy(Reader r, OutputStream o) throws IOException {
49 copy(r, o, "UTF-8");
50 }
51
52 public static void copy(Reader r, OutputStream o, String charset) throws IOException {
53 try {
54 OutputStreamWriter osw = new OutputStreamWriter(o, charset);
55 copy(r, osw);
Stuart McCulloch2286f232012-06-15 13:27:53 +000056 }
57 finally {
Stuart McCullochbb014372012-06-07 21:57:32 +000058 r.close();
59 }
60 }
61
62 public static void copy(InputStream in, OutputStream out) throws IOException {
63 DataOutputStream dos = new DataOutputStream(out);
64 copy(in, (DataOutput) dos);
65 out.flush();
66 }
67
68 public static void copy(InputStream in, DataOutput out) throws IOException {
69 byte[] buffer = new byte[10000];
70 try {
71 int size = in.read(buffer);
72 while (size > 0) {
73 out.write(buffer, 0, size);
74 size = in.read(buffer);
75 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000076 }
77 finally {
Stuart McCullochbb014372012-06-07 21:57:32 +000078 in.close();
79 }
80 }
81
82 public static void copy(InputStream in, ByteBuffer bb) throws IOException {
83 byte[] buffer = new byte[10000];
84 try {
85 int size = in.read(buffer);
86 while (size > 0) {
87 bb.put(buffer, 0, size);
88 size = in.read(buffer);
89 }
Stuart McCulloch2286f232012-06-15 13:27:53 +000090 }
91 finally {
Stuart McCullochbb014372012-06-07 21:57:32 +000092 in.close();
93 }
94 }
95
96 public static void copy(URL in, MessageDigest md) throws IOException {
97 copy(in.openStream(), md);
98 }
99
100 public static void copy(File in, MessageDigest md) throws IOException {
101 copy(new FileInputStream(in), md);
102 }
103
104 public static void copy(URLConnection in, MessageDigest md) throws IOException {
105 copy(in.getInputStream(), md);
106 }
107
Stuart McCullochbb014372012-06-07 21:57:32 +0000108 public static void copy(InputStream in, MessageDigest md) throws IOException {
109 byte[] buffer = new byte[10000];
110 try {
111 int size = in.read(buffer);
112 while (size > 0) {
113 md.update(buffer, 0, size);
114 size = in.read(buffer);
115 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000116 }
117 finally {
Stuart McCullochbb014372012-06-07 21:57:32 +0000118 in.close();
119 }
120 }
121
122 public static void copy(URL url, File file) throws IOException {
123 URLConnection c = url.openConnection();
124 copy(c, file);
125 }
126
127 public static void copy(URLConnection c, File file) throws IOException {
128 copy(c.getInputStream(), file);
129 }
130
131 public static void copy(InputStream in, URL out) throws IOException {
132 copy(in, out, null);
133 }
134
135 public static void copy(InputStream in, URL out, String method) throws IOException {
136 URLConnection c = out.openConnection();
137 if (c instanceof HttpURLConnection && method != null) {
138 HttpURLConnection http = (HttpURLConnection) c;
139 http.setRequestMethod(method);
140 }
141 c.setDoOutput(true);
142 copy(in, c.getOutputStream());
143 }
144
145 public static void copy(File a, File b) throws IOException {
146 if (a.isFile()) {
147 FileOutputStream out = new FileOutputStream(b);
148 try {
149 copy(new FileInputStream(a), out);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000150 }
151 finally {
Stuart McCullochbb014372012-06-07 21:57:32 +0000152 out.close();
153 }
154 } else if (a.isDirectory()) {
Stuart McCulloch55d4dfe2012-08-07 10:57:21 +0000155 if (!b.exists() && !b.mkdirs()) {
156 throw new IOException("Could not create directory " + b);
157 }
Stuart McCullochbb014372012-06-07 21:57:32 +0000158 if (!b.isDirectory())
Stuart McCulloch2286f232012-06-15 13:27:53 +0000159 throw new IllegalArgumentException("target directory for a directory must be a directory: " + b);
Stuart McCullochbb014372012-06-07 21:57:32 +0000160 File subs[] = a.listFiles();
161 for (File sub : subs) {
162 copy(sub, new File(b, sub.getName()));
163 }
164 } else
165 throw new FileNotFoundException("During copy: " + a.toString());
166 }
167
168 public static void copy(InputStream a, File b) throws IOException {
169 FileOutputStream out = new FileOutputStream(b);
170 try {
171 copy(a, out);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000172 }
173 finally {
Stuart McCullochbb014372012-06-07 21:57:32 +0000174 out.close();
175 }
176 }
177
178 public static void copy(File a, OutputStream b) throws IOException {
179 copy(new FileInputStream(a), b);
180 }
181
182 public static String collect(File a, String encoding) throws IOException {
183 ByteArrayOutputStream out = new ByteArrayOutputStream();
184 copy(a, out);
185 return new String(out.toByteArray(), encoding);
186 }
187
188 public static String collect(URL a, String encoding) throws IOException {
189 ByteArrayOutputStream out = new ByteArrayOutputStream();
190 copy(a.openStream(), out);
191 return new String(out.toByteArray(), encoding);
192 }
193
194 public static String collect(URL a) throws IOException {
195 return collect(a, "UTF-8");
196 }
197
198 public static String collect(File a) throws IOException {
199 return collect(a, "UTF-8");
200 }
201
202 public static String collect(String a) throws IOException {
203 return collect(new File(a), "UTF-8");
204 }
205
206 public static String collect(InputStream a, String encoding) throws IOException {
207 ByteArrayOutputStream out = new ByteArrayOutputStream();
208 copy(a, out);
209 return new String(out.toByteArray(), encoding);
210 }
211
212 public static String collect(InputStream a) throws IOException {
213 return collect(a, "UTF-8");
214 }
215
216 public static String collect(Reader a) throws IOException {
217 StringWriter sw = new StringWriter();
218 char[] buffer = new char[10000];
219 int size = a.read(buffer);
220 while (size > 0) {
221 sw.write(buffer, 0, size);
222 size = a.read(buffer);
223 }
224 return sw.toString();
225 }
226
Stuart McCullocha21b9e82012-08-02 13:26:25 +0000227 /**
228 * Create a temporary file.
229 *
230 * @param directory
231 * the directory in which to create the file. Can be null, in
232 * which case the system TMP directory is used
233 * @param pattern
234 * the filename prefix pattern. Must be at least 3 characters
235 * long
236 * @param suffix
237 * the filename suffix. Can be null, in which case (system)
238 * default suffix is used
239 * @return
240 * @throws IllegalArgumentException
241 * when pattern is null or too short
242 * @throws IOException
243 * when the specified (non-null) directory is not a directory
244 */
245 public static File createTempFile(File directory, String pattern, String suffix) throws IllegalArgumentException,
246 IOException {
247 if ((pattern == null) || (pattern.length() < 3)) {
248 throw new IllegalArgumentException("Pattern must be at least 3 characters long, got "
249 + ((pattern == null) ? "null" : pattern.length()));
250 }
251
252 if ((directory != null) && !directory.isDirectory()) {
253 throw new FileNotFoundException("Directory " + directory + " is not a directory");
254 }
255
256 return File.createTempFile(pattern, suffix, directory);
257 }
258
Stuart McCulloch2286f232012-06-15 13:27:53 +0000259 public static File getFile(String filename) {
260 return new File(filename.replace("/", File.separator));
261 }
262
Stuart McCullochbb014372012-06-07 21:57:32 +0000263 public static File getFile(File base, String file) {
264 File f = new File(file);
265 if (f.isAbsolute())
266 return f;
267 int n;
268
269 f = base.getAbsoluteFile();
270 while ((n = file.indexOf('/')) > 0) {
271 String first = file.substring(0, n);
272 file = file.substring(n + 1);
273 if (first.equals(".."))
274 f = f.getParentFile();
275 else
276 f = new File(f, first);
277 }
278 if (file.equals(".."))
279 return f.getParentFile();
280 return new File(f, file).getAbsoluteFile();
281 }
282
Stuart McCulloch2286f232012-06-15 13:27:53 +0000283 /** Deletes the specified file.
284 * Folders are recursively deleted.<br>
285 * If file(s) cannot be deleted, no feedback is provided (fail silently).
286 * @param f file to be deleted
287 */
Stuart McCullochbb014372012-06-07 21:57:32 +0000288 public static void delete(File f) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000289 try {
290 deleteWithException(f);
291 } catch (IOException e) {
292 // Ignore a failed delete
293 }
294 }
295
296 /** Deletes the specified file.
297 * Folders are recursively deleted.<br>
298 * Throws exception if any of the files could not be deleted.
299 * @param f file to be deleted
300 * @throws IOException if the file (or contents of a folder) could not be deleted
301 */
302 public static void deleteWithException(File f) throws IOException {
Stuart McCullochbb014372012-06-07 21:57:32 +0000303 f = f.getAbsoluteFile();
Stuart McCulloch2286f232012-06-15 13:27:53 +0000304 if (!f.exists()) return;
Stuart McCullochbb014372012-06-07 21:57:32 +0000305 if (f.getParentFile() == null)
306 throw new IllegalArgumentException("Cannot recursively delete root for safety reasons");
307
Stuart McCulloch2286f232012-06-15 13:27:53 +0000308 boolean wasDeleted = true;
Stuart McCullochbb014372012-06-07 21:57:32 +0000309 if (f.isDirectory()) {
310 File[] subs = f.listFiles();
Stuart McCulloch2286f232012-06-15 13:27:53 +0000311 for (File sub : subs) {
312 try {
313 deleteWithException(sub);
314 } catch (IOException e) {
315 wasDeleted = false;
316 }
317 }
Stuart McCullochbb014372012-06-07 21:57:32 +0000318 }
319
Stuart McCulloch2286f232012-06-15 13:27:53 +0000320 boolean fDeleted = f.delete();
321 if (!fDeleted || !wasDeleted) {
322 throw new IOException("Failed to delete " + f.getAbsoluteFile());
323 }
Stuart McCullochbb014372012-06-07 21:57:32 +0000324 }
325
Stuart McCulloch2286f232012-06-15 13:27:53 +0000326 /** Deletes <code>to</code> file if it exists, and renames <code>from</code> file to <code>to</code>.<br>
327 * Throws exception the rename operation fails.
328 * @param from source file
329 * @param to destination file
330 * @throws IOException if the rename operation fails
331 */
332 public static void rename(File from, File to) throws IOException {
333 IO.deleteWithException(to);
334
335 boolean renamed = from.renameTo(to);
336 if (!renamed) throw new IOException("Could not rename " + from.getAbsoluteFile() + " to " + to.getAbsoluteFile());
337 }
338
339
Stuart McCullochbb014372012-06-07 21:57:32 +0000340 public static long drain(InputStream in) throws IOException {
341 long result = 0;
342 byte[] buffer = new byte[10000];
343 try {
344 int size = in.read(buffer);
345 while (size >= 0) {
346 result += size;
347 size = in.read(buffer);
348 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000349 }
350 finally {
Stuart McCullochbb014372012-06-07 21:57:32 +0000351 in.close();
352 }
353 return result;
354 }
355
Stuart McCulloch2286f232012-06-15 13:27:53 +0000356 public void copy(Collection< ? > c, OutputStream out) throws IOException {
357 Writer w = new OutputStreamWriter(out, "UTF-8");
Stuart McCullochbb014372012-06-07 21:57:32 +0000358 PrintWriter ps = new PrintWriter(w);
359 for (Object o : c) {
360 ps.println(o);
361 }
362 ps.flush();
363 w.flush();
364 }
365
366 public static Throwable close(Closeable in) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000367 if (in == null)
Stuart McCullochbb014372012-06-07 21:57:32 +0000368 return null;
Stuart McCulloch2286f232012-06-15 13:27:53 +0000369
Stuart McCullochbb014372012-06-07 21:57:32 +0000370 try {
371 in.close();
372 return null;
Stuart McCulloch2286f232012-06-15 13:27:53 +0000373 }
374 catch (Throwable e) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000375 return e;
376 }
377 }
378
379 public static URL toURL(String s, File base) throws MalformedURLException {
380 int n = s.indexOf(':');
381 if (n > 0 && n < 10) {
382 // is url
383 return new URL(s);
384 }
385 return getFile(base, s).toURI().toURL();
386 }
387
388 public static void store(Object o, File out) throws IOException {
389 store(o, out, "UTF-8");
390 }
391
392 public static void store(Object o, File out, String encoding) throws IOException {
393 FileOutputStream fout = new FileOutputStream(out);
394 try {
395 store(o, fout, encoding);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000396 }
397 finally {
Stuart McCullochbb014372012-06-07 21:57:32 +0000398 fout.close();
399 }
400 }
401
Stuart McCulloch2286f232012-06-15 13:27:53 +0000402 public static void store(Object o, OutputStream fout) throws UnsupportedEncodingException, IOException {
Stuart McCullochbb014372012-06-07 21:57:32 +0000403 store(o, fout, "UTF-8");
404 }
405
Stuart McCulloch2286f232012-06-15 13:27:53 +0000406 public static void store(Object o, OutputStream fout, String encoding) throws UnsupportedEncodingException,
407 IOException {
Stuart McCullochbb014372012-06-07 21:57:32 +0000408 String s;
409
410 if (o == null)
411 s = "";
412 else
413 s = o.toString();
414
415 try {
416 fout.write(s.getBytes(encoding));
Stuart McCulloch2286f232012-06-15 13:27:53 +0000417 }
418 finally {
Stuart McCullochbb014372012-06-07 21:57:32 +0000419 fout.close();
420 }
421 }
422
423 public static InputStream stream(String s) {
424 try {
425 return new ByteArrayInputStream(s.getBytes("UTF-8"));
Stuart McCulloch2286f232012-06-15 13:27:53 +0000426 }
427 catch (Exception e) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000428 // Ignore
429 return null;
430 }
431 }
432
433 public static InputStream stream(String s, String encoding) throws UnsupportedEncodingException {
434 return new ByteArrayInputStream(s.getBytes(encoding));
435 }
436
437 public static InputStream stream(File s) throws FileNotFoundException {
438 return new FileInputStream(s);
439 }
440
441 public static InputStream stream(URL s) throws IOException {
442 return s.openStream();
443 }
444
445 public static Reader reader(String s) {
446 return new StringReader(s);
447 }
448
Stuart McCullochbb014372012-06-07 21:57:32 +0000449 public static BufferedReader reader(File f, String encoding) throws IOException {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000450 return reader(new FileInputStream(f), encoding);
Stuart McCullochbb014372012-06-07 21:57:32 +0000451 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000452
Stuart McCullochbb014372012-06-07 21:57:32 +0000453 public static BufferedReader reader(File f) throws IOException {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000454 return reader(f, "UTF-8");
Stuart McCullochbb014372012-06-07 21:57:32 +0000455 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000456
Stuart McCullochbb014372012-06-07 21:57:32 +0000457 public static PrintWriter writer(File f, String encoding) throws IOException {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000458 return writer(new FileOutputStream(f), encoding);
Stuart McCullochbb014372012-06-07 21:57:32 +0000459 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000460
Stuart McCullochbb014372012-06-07 21:57:32 +0000461 public static PrintWriter writer(File f) throws IOException {
462 return writer(f, "UTF-8");
463 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000464
Stuart McCullochbb014372012-06-07 21:57:32 +0000465 public static PrintWriter writer(OutputStream out, String encoding) throws IOException {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000466 return new PrintWriter(new OutputStreamWriter(out, encoding));
Stuart McCullochbb014372012-06-07 21:57:32 +0000467 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000468
Stuart McCullochbb014372012-06-07 21:57:32 +0000469 public static BufferedReader reader(InputStream in, String encoding) throws IOException {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000470 return new BufferedReader(new InputStreamReader(in, encoding));
Stuart McCullochbb014372012-06-07 21:57:32 +0000471 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000472
Stuart McCullochbb014372012-06-07 21:57:32 +0000473 public static BufferedReader reader(InputStream in) throws IOException {
474 return reader(in, "UTF-8");
475 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000476
Stuart McCullochbb014372012-06-07 21:57:32 +0000477 public static PrintWriter writer(OutputStream out) throws IOException {
478 return writer(out, "UTF-8");
479 }
480}