blob: 0cb7e1bef598d9dfea79877752f5b092b5f0b29a [file] [log] [blame]
Stuart McCullochf3173222012-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 {
Stuart McCulloch2a0afd62012-09-06 18:28:06 +000010 static public File work = new File(System.getProperty("user.dir"));
11 static public File home = new File(System.getProperty("user.home"));
Stuart McCullochf3173222012-06-07 21:57:32 +000012
13 public static void copy(Reader r, Writer w) throws IOException {
14 try {
15 char buffer[] = new char[8000];
16 int size = r.read(buffer);
17 while (size > 0) {
18 w.write(buffer, 0, size);
19 size = r.read(buffer);
20 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000021 }
22 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +000023 r.close();
24 w.flush();
25 }
26 }
27
28 public static void copy(InputStream r, Writer w) throws IOException {
29 copy(r, w, "UTF-8");
30 }
31
Stuart McCulloch4482c702012-06-15 13:27:53 +000032 public static void copy(byte[] r, Writer w) throws IOException {
33 copy(new ByteArrayInputStream(r), w, "UTF-8");
Stuart McCullochf3173222012-06-07 21:57:32 +000034 }
35
Stuart McCulloch4482c702012-06-15 13:27:53 +000036 public static void copy(byte[] r, OutputStream w) throws IOException {
37 copy(new ByteArrayInputStream(r), w);
Stuart McCullochf3173222012-06-07 21:57:32 +000038 }
39
40 public static void copy(InputStream r, Writer w, String charset) throws IOException {
41 try {
42 InputStreamReader isr = new InputStreamReader(r, charset);
43 copy(isr, w);
Stuart McCulloch4482c702012-06-15 13:27:53 +000044 }
45 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +000046 r.close();
47 }
48 }
49
50 public static void copy(Reader r, OutputStream o) throws IOException {
51 copy(r, o, "UTF-8");
52 }
53
54 public static void copy(Reader r, OutputStream o, String charset) throws IOException {
55 try {
56 OutputStreamWriter osw = new OutputStreamWriter(o, charset);
57 copy(r, osw);
Stuart McCulloch4482c702012-06-15 13:27:53 +000058 }
59 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +000060 r.close();
61 }
62 }
63
64 public static void copy(InputStream in, OutputStream out) throws IOException {
65 DataOutputStream dos = new DataOutputStream(out);
66 copy(in, (DataOutput) dos);
67 out.flush();
68 }
69
70 public static void copy(InputStream in, DataOutput out) throws IOException {
71 byte[] buffer = new byte[10000];
72 try {
73 int size = in.read(buffer);
74 while (size > 0) {
75 out.write(buffer, 0, size);
76 size = in.read(buffer);
77 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000078 }
79 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +000080 in.close();
81 }
82 }
83
84 public static void copy(InputStream in, ByteBuffer bb) throws IOException {
85 byte[] buffer = new byte[10000];
86 try {
87 int size = in.read(buffer);
88 while (size > 0) {
89 bb.put(buffer, 0, size);
90 size = in.read(buffer);
91 }
Stuart McCulloch4482c702012-06-15 13:27:53 +000092 }
93 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +000094 in.close();
95 }
96 }
97
98 public static void copy(URL in, MessageDigest md) throws IOException {
99 copy(in.openStream(), md);
100 }
101
102 public static void copy(File in, MessageDigest md) throws IOException {
103 copy(new FileInputStream(in), md);
104 }
105
106 public static void copy(URLConnection in, MessageDigest md) throws IOException {
107 copy(in.getInputStream(), md);
108 }
109
Stuart McCullochf3173222012-06-07 21:57:32 +0000110 public static void copy(InputStream in, MessageDigest md) throws IOException {
111 byte[] buffer = new byte[10000];
112 try {
113 int size = in.read(buffer);
114 while (size > 0) {
115 md.update(buffer, 0, size);
116 size = in.read(buffer);
117 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000118 }
119 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +0000120 in.close();
121 }
122 }
123
124 public static void copy(URL url, File file) throws IOException {
125 URLConnection c = url.openConnection();
126 copy(c, file);
127 }
128
129 public static void copy(URLConnection c, File file) throws IOException {
130 copy(c.getInputStream(), file);
131 }
132
133 public static void copy(InputStream in, URL out) throws IOException {
134 copy(in, out, null);
135 }
136
137 public static void copy(InputStream in, URL out, String method) throws IOException {
138 URLConnection c = out.openConnection();
139 if (c instanceof HttpURLConnection && method != null) {
140 HttpURLConnection http = (HttpURLConnection) c;
141 http.setRequestMethod(method);
142 }
143 c.setDoOutput(true);
144 copy(in, c.getOutputStream());
145 }
146
147 public static void copy(File a, File b) throws IOException {
148 if (a.isFile()) {
149 FileOutputStream out = new FileOutputStream(b);
150 try {
151 copy(new FileInputStream(a), out);
Stuart McCulloch4482c702012-06-15 13:27:53 +0000152 }
153 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +0000154 out.close();
155 }
156 } else if (a.isDirectory()) {
Stuart McCulloch2929e2d2012-08-07 10:57:21 +0000157 if (!b.exists() && !b.mkdirs()) {
158 throw new IOException("Could not create directory " + b);
159 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000160 if (!b.isDirectory())
Stuart McCulloch4482c702012-06-15 13:27:53 +0000161 throw new IllegalArgumentException("target directory for a directory must be a directory: " + b);
Stuart McCullochf3173222012-06-07 21:57:32 +0000162 File subs[] = a.listFiles();
163 for (File sub : subs) {
164 copy(sub, new File(b, sub.getName()));
165 }
166 } else
167 throw new FileNotFoundException("During copy: " + a.toString());
168 }
169
170 public static void copy(InputStream a, File b) throws IOException {
171 FileOutputStream out = new FileOutputStream(b);
172 try {
173 copy(a, out);
Stuart McCulloch4482c702012-06-15 13:27:53 +0000174 }
175 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +0000176 out.close();
177 }
178 }
179
180 public static void copy(File a, OutputStream b) throws IOException {
181 copy(new FileInputStream(a), b);
182 }
183
184 public static String collect(File a, String encoding) throws IOException {
185 ByteArrayOutputStream out = new ByteArrayOutputStream();
186 copy(a, out);
187 return new String(out.toByteArray(), encoding);
188 }
189
190 public static String collect(URL a, String encoding) throws IOException {
191 ByteArrayOutputStream out = new ByteArrayOutputStream();
192 copy(a.openStream(), out);
193 return new String(out.toByteArray(), encoding);
194 }
195
196 public static String collect(URL a) throws IOException {
197 return collect(a, "UTF-8");
198 }
199
200 public static String collect(File a) throws IOException {
201 return collect(a, "UTF-8");
202 }
203
204 public static String collect(String a) throws IOException {
205 return collect(new File(a), "UTF-8");
206 }
207
208 public static String collect(InputStream a, String encoding) throws IOException {
209 ByteArrayOutputStream out = new ByteArrayOutputStream();
210 copy(a, out);
211 return new String(out.toByteArray(), encoding);
212 }
213
214 public static String collect(InputStream a) throws IOException {
215 return collect(a, "UTF-8");
216 }
217
218 public static String collect(Reader a) throws IOException {
219 StringWriter sw = new StringWriter();
220 char[] buffer = new char[10000];
221 int size = a.read(buffer);
222 while (size > 0) {
223 sw.write(buffer, 0, size);
224 size = a.read(buffer);
225 }
226 return sw.toString();
227 }
228
Stuart McCulloch55fbda52012-08-02 13:26:25 +0000229 /**
230 * Create a temporary file.
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000231 *
Stuart McCulloch55fbda52012-08-02 13:26:25 +0000232 * @param directory
233 * the directory in which to create the file. Can be null, in
234 * which case the system TMP directory is used
235 * @param pattern
236 * the filename prefix pattern. Must be at least 3 characters
237 * long
238 * @param suffix
239 * the filename suffix. Can be null, in which case (system)
240 * default suffix is used
241 * @return
242 * @throws IllegalArgumentException
243 * when pattern is null or too short
244 * @throws IOException
245 * when the specified (non-null) directory is not a directory
246 */
247 public static File createTempFile(File directory, String pattern, String suffix) throws IllegalArgumentException,
248 IOException {
249 if ((pattern == null) || (pattern.length() < 3)) {
250 throw new IllegalArgumentException("Pattern must be at least 3 characters long, got "
251 + ((pattern == null) ? "null" : pattern.length()));
252 }
253
254 if ((directory != null) && !directory.isDirectory()) {
255 throw new FileNotFoundException("Directory " + directory + " is not a directory");
256 }
257
258 return File.createTempFile(pattern, suffix, directory);
259 }
260
Stuart McCulloch4482c702012-06-15 13:27:53 +0000261 public static File getFile(String filename) {
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000262 return getFile(work, filename);
Stuart McCulloch4482c702012-06-15 13:27:53 +0000263 }
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000264
Stuart McCullochf3173222012-06-07 21:57:32 +0000265 public static File getFile(File base, String file) {
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000266
267 if (file.startsWith("~/")) {
268 file = file.substring(2);
269 if (!file.startsWith("~/")) {
270 return getFile(home, file);
271 }
272 }
273 if (file.startsWith("~")) {
274 file = file.substring(1);
275 return getFile(home.getParentFile(), file);
276 }
277
Stuart McCullochf3173222012-06-07 21:57:32 +0000278 File f = new File(file);
279 if (f.isAbsolute())
280 return f;
281 int n;
282
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000283 if (base == null)
284 base = work;
285
Stuart McCullochf3173222012-06-07 21:57:32 +0000286 f = base.getAbsoluteFile();
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000287
Stuart McCullochf3173222012-06-07 21:57:32 +0000288 while ((n = file.indexOf('/')) > 0) {
289 String first = file.substring(0, n);
290 file = file.substring(n + 1);
291 if (first.equals(".."))
292 f = f.getParentFile();
293 else
294 f = new File(f, first);
295 }
296 if (file.equals(".."))
297 return f.getParentFile();
298 return new File(f, file).getAbsoluteFile();
299 }
300
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000301 /**
302 * Deletes the specified file. Folders are recursively deleted.<br>
Stuart McCulloch4482c702012-06-15 13:27:53 +0000303 * If file(s) cannot be deleted, no feedback is provided (fail silently).
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000304 *
305 * @param f
306 * file to be deleted
Stuart McCulloch4482c702012-06-15 13:27:53 +0000307 */
Stuart McCullochf3173222012-06-07 21:57:32 +0000308 public static void delete(File f) {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000309 try {
310 deleteWithException(f);
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000311 }
312 catch (IOException e) {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000313 // Ignore a failed delete
314 }
315 }
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000316
317 /**
318 * Deletes the specified file. Folders are recursively deleted.<br>
Stuart McCulloch4482c702012-06-15 13:27:53 +0000319 * Throws exception if any of the files could not be deleted.
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000320 *
321 * @param f
322 * file to be deleted
323 * @throws IOException
324 * if the file (or contents of a folder) could not be deleted
Stuart McCulloch4482c702012-06-15 13:27:53 +0000325 */
326 public static void deleteWithException(File f) throws IOException {
Stuart McCullochf3173222012-06-07 21:57:32 +0000327 f = f.getAbsoluteFile();
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000328 if (!f.exists())
329 return;
Stuart McCullochf3173222012-06-07 21:57:32 +0000330 if (f.getParentFile() == null)
331 throw new IllegalArgumentException("Cannot recursively delete root for safety reasons");
332
Stuart McCulloch4482c702012-06-15 13:27:53 +0000333 boolean wasDeleted = true;
Stuart McCullochf3173222012-06-07 21:57:32 +0000334 if (f.isDirectory()) {
335 File[] subs = f.listFiles();
Stuart McCulloch4482c702012-06-15 13:27:53 +0000336 for (File sub : subs) {
337 try {
338 deleteWithException(sub);
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000339 }
340 catch (IOException e) {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000341 wasDeleted = false;
342 }
343 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000344 }
345
Stuart McCulloch4482c702012-06-15 13:27:53 +0000346 boolean fDeleted = f.delete();
347 if (!fDeleted || !wasDeleted) {
348 throw new IOException("Failed to delete " + f.getAbsoluteFile());
349 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000350 }
351
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000352 /**
353 * Deletes <code>to</code> file if it exists, and renames <code>from</code>
354 * file to <code>to</code>.<br>
355 * Throws exception the rename operation fails.
356 *
357 * @param from
358 * source file
359 * @param to
360 * destination file
361 * @throws IOException
362 * if the rename operation fails
363 */
364 public static void rename(File from, File to) throws IOException {
365 IO.deleteWithException(to);
Stuart McCulloch4482c702012-06-15 13:27:53 +0000366
Stuart McCulloch2a0afd62012-09-06 18:28:06 +0000367 boolean renamed = from.renameTo(to);
368 if (!renamed)
369 throw new IOException("Could not rename " + from.getAbsoluteFile() + " to " + to.getAbsoluteFile());
370 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000371
Stuart McCullochf3173222012-06-07 21:57:32 +0000372 public static long drain(InputStream in) throws IOException {
373 long result = 0;
374 byte[] buffer = new byte[10000];
375 try {
376 int size = in.read(buffer);
377 while (size >= 0) {
378 result += size;
379 size = in.read(buffer);
380 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000381 }
382 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +0000383 in.close();
384 }
385 return result;
386 }
387
Stuart McCulloch4482c702012-06-15 13:27:53 +0000388 public void copy(Collection< ? > c, OutputStream out) throws IOException {
389 Writer w = new OutputStreamWriter(out, "UTF-8");
Stuart McCullochf3173222012-06-07 21:57:32 +0000390 PrintWriter ps = new PrintWriter(w);
391 for (Object o : c) {
392 ps.println(o);
393 }
394 ps.flush();
395 w.flush();
396 }
397
398 public static Throwable close(Closeable in) {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000399 if (in == null)
Stuart McCullochf3173222012-06-07 21:57:32 +0000400 return null;
Stuart McCulloch4482c702012-06-15 13:27:53 +0000401
Stuart McCullochf3173222012-06-07 21:57:32 +0000402 try {
403 in.close();
404 return null;
Stuart McCulloch4482c702012-06-15 13:27:53 +0000405 }
406 catch (Throwable e) {
Stuart McCullochf3173222012-06-07 21:57:32 +0000407 return e;
408 }
409 }
410
411 public static URL toURL(String s, File base) throws MalformedURLException {
412 int n = s.indexOf(':');
413 if (n > 0 && n < 10) {
414 // is url
415 return new URL(s);
416 }
417 return getFile(base, s).toURI().toURL();
418 }
419
420 public static void store(Object o, File out) throws IOException {
421 store(o, out, "UTF-8");
422 }
423
424 public static void store(Object o, File out, String encoding) throws IOException {
425 FileOutputStream fout = new FileOutputStream(out);
426 try {
427 store(o, fout, encoding);
Stuart McCulloch4482c702012-06-15 13:27:53 +0000428 }
429 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +0000430 fout.close();
431 }
432 }
433
Stuart McCulloch4482c702012-06-15 13:27:53 +0000434 public static void store(Object o, OutputStream fout) throws UnsupportedEncodingException, IOException {
Stuart McCullochf3173222012-06-07 21:57:32 +0000435 store(o, fout, "UTF-8");
436 }
437
Stuart McCulloch4482c702012-06-15 13:27:53 +0000438 public static void store(Object o, OutputStream fout, String encoding) throws UnsupportedEncodingException,
439 IOException {
Stuart McCullochf3173222012-06-07 21:57:32 +0000440 String s;
441
442 if (o == null)
443 s = "";
444 else
445 s = o.toString();
446
447 try {
448 fout.write(s.getBytes(encoding));
Stuart McCulloch4482c702012-06-15 13:27:53 +0000449 }
450 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +0000451 fout.close();
452 }
453 }
454
455 public static InputStream stream(String s) {
456 try {
457 return new ByteArrayInputStream(s.getBytes("UTF-8"));
Stuart McCulloch4482c702012-06-15 13:27:53 +0000458 }
459 catch (Exception e) {
Stuart McCullochf3173222012-06-07 21:57:32 +0000460 // Ignore
461 return null;
462 }
463 }
464
465 public static InputStream stream(String s, String encoding) throws UnsupportedEncodingException {
466 return new ByteArrayInputStream(s.getBytes(encoding));
467 }
468
469 public static InputStream stream(File s) throws FileNotFoundException {
470 return new FileInputStream(s);
471 }
472
473 public static InputStream stream(URL s) throws IOException {
474 return s.openStream();
475 }
476
477 public static Reader reader(String s) {
478 return new StringReader(s);
479 }
480
Stuart McCullochf3173222012-06-07 21:57:32 +0000481 public static BufferedReader reader(File f, String encoding) throws IOException {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000482 return reader(new FileInputStream(f), encoding);
Stuart McCullochf3173222012-06-07 21:57:32 +0000483 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000484
Stuart McCullochf3173222012-06-07 21:57:32 +0000485 public static BufferedReader reader(File f) throws IOException {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000486 return reader(f, "UTF-8");
Stuart McCullochf3173222012-06-07 21:57:32 +0000487 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000488
Stuart McCullochf3173222012-06-07 21:57:32 +0000489 public static PrintWriter writer(File f, String encoding) throws IOException {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000490 return writer(new FileOutputStream(f), encoding);
Stuart McCullochf3173222012-06-07 21:57:32 +0000491 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000492
Stuart McCullochf3173222012-06-07 21:57:32 +0000493 public static PrintWriter writer(File f) throws IOException {
494 return writer(f, "UTF-8");
495 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000496
Stuart McCullochf3173222012-06-07 21:57:32 +0000497 public static PrintWriter writer(OutputStream out, String encoding) throws IOException {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000498 return new PrintWriter(new OutputStreamWriter(out, encoding));
Stuart McCullochf3173222012-06-07 21:57:32 +0000499 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000500
Stuart McCullochf3173222012-06-07 21:57:32 +0000501 public static BufferedReader reader(InputStream in, String encoding) throws IOException {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000502 return new BufferedReader(new InputStreamReader(in, encoding));
Stuart McCullochf3173222012-06-07 21:57:32 +0000503 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000504
Stuart McCullochf3173222012-06-07 21:57:32 +0000505 public static BufferedReader reader(InputStream in) throws IOException {
506 return reader(in, "UTF-8");
507 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000508
Stuart McCullochf3173222012-06-07 21:57:32 +0000509 public static PrintWriter writer(OutputStream out) throws IOException {
510 return writer(out, "UTF-8");
511 }
512}