blob: 183d92a5c62edd8301efd1cde34160a85db97cad [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 {
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 McCulloch4482c702012-06-15 13:27:53 +000019 }
20 finally {
Stuart McCullochf3173222012-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 McCulloch4482c702012-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 McCullochf3173222012-06-07 21:57:32 +000032 }
33
Stuart McCulloch4482c702012-06-15 13:27:53 +000034 public static void copy(byte[] r, OutputStream w) throws IOException {
35 copy(new ByteArrayInputStream(r), w);
Stuart McCullochf3173222012-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 McCulloch4482c702012-06-15 13:27:53 +000042 }
43 finally {
Stuart McCullochf3173222012-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 McCulloch4482c702012-06-15 13:27:53 +000056 }
57 finally {
Stuart McCullochf3173222012-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 McCulloch4482c702012-06-15 13:27:53 +000076 }
77 finally {
Stuart McCullochf3173222012-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 McCulloch4482c702012-06-15 13:27:53 +000090 }
91 finally {
Stuart McCullochf3173222012-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 McCullochf3173222012-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 McCulloch4482c702012-06-15 13:27:53 +0000116 }
117 finally {
Stuart McCullochf3173222012-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 McCulloch4482c702012-06-15 13:27:53 +0000150 }
151 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +0000152 out.close();
153 }
154 } else if (a.isDirectory()) {
155 b.mkdirs();
156 if (!b.isDirectory())
Stuart McCulloch4482c702012-06-15 13:27:53 +0000157 throw new IllegalArgumentException("target directory for a directory must be a directory: " + b);
Stuart McCullochf3173222012-06-07 21:57:32 +0000158 File subs[] = a.listFiles();
159 for (File sub : subs) {
160 copy(sub, new File(b, sub.getName()));
161 }
162 } else
163 throw new FileNotFoundException("During copy: " + a.toString());
164 }
165
166 public static void copy(InputStream a, File b) throws IOException {
167 FileOutputStream out = new FileOutputStream(b);
168 try {
169 copy(a, out);
Stuart McCulloch4482c702012-06-15 13:27:53 +0000170 }
171 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +0000172 out.close();
173 }
174 }
175
176 public static void copy(File a, OutputStream b) throws IOException {
177 copy(new FileInputStream(a), b);
178 }
179
180 public static String collect(File a, String encoding) throws IOException {
181 ByteArrayOutputStream out = new ByteArrayOutputStream();
182 copy(a, out);
183 return new String(out.toByteArray(), encoding);
184 }
185
186 public static String collect(URL a, String encoding) throws IOException {
187 ByteArrayOutputStream out = new ByteArrayOutputStream();
188 copy(a.openStream(), out);
189 return new String(out.toByteArray(), encoding);
190 }
191
192 public static String collect(URL a) throws IOException {
193 return collect(a, "UTF-8");
194 }
195
196 public static String collect(File a) throws IOException {
197 return collect(a, "UTF-8");
198 }
199
200 public static String collect(String a) throws IOException {
201 return collect(new File(a), "UTF-8");
202 }
203
204 public static String collect(InputStream a, String encoding) throws IOException {
205 ByteArrayOutputStream out = new ByteArrayOutputStream();
206 copy(a, out);
207 return new String(out.toByteArray(), encoding);
208 }
209
210 public static String collect(InputStream a) throws IOException {
211 return collect(a, "UTF-8");
212 }
213
214 public static String collect(Reader a) throws IOException {
215 StringWriter sw = new StringWriter();
216 char[] buffer = new char[10000];
217 int size = a.read(buffer);
218 while (size > 0) {
219 sw.write(buffer, 0, size);
220 size = a.read(buffer);
221 }
222 return sw.toString();
223 }
224
Stuart McCulloch4482c702012-06-15 13:27:53 +0000225 public static File getFile(String filename) {
226 return new File(filename.replace("/", File.separator));
227 }
228
Stuart McCullochf3173222012-06-07 21:57:32 +0000229 public static File getFile(File base, String file) {
230 File f = new File(file);
231 if (f.isAbsolute())
232 return f;
233 int n;
234
235 f = base.getAbsoluteFile();
236 while ((n = file.indexOf('/')) > 0) {
237 String first = file.substring(0, n);
238 file = file.substring(n + 1);
239 if (first.equals(".."))
240 f = f.getParentFile();
241 else
242 f = new File(f, first);
243 }
244 if (file.equals(".."))
245 return f.getParentFile();
246 return new File(f, file).getAbsoluteFile();
247 }
248
Stuart McCulloch4482c702012-06-15 13:27:53 +0000249 /** Deletes the specified file.
250 * Folders are recursively deleted.<br>
251 * If file(s) cannot be deleted, no feedback is provided (fail silently).
252 * @param f file to be deleted
253 */
Stuart McCullochf3173222012-06-07 21:57:32 +0000254 public static void delete(File f) {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000255 try {
256 deleteWithException(f);
257 } catch (IOException e) {
258 // Ignore a failed delete
259 }
260 }
261
262 /** Deletes the specified file.
263 * Folders are recursively deleted.<br>
264 * Throws exception if any of the files could not be deleted.
265 * @param f file to be deleted
266 * @throws IOException if the file (or contents of a folder) could not be deleted
267 */
268 public static void deleteWithException(File f) throws IOException {
Stuart McCullochf3173222012-06-07 21:57:32 +0000269 f = f.getAbsoluteFile();
Stuart McCulloch4482c702012-06-15 13:27:53 +0000270 if (!f.exists()) return;
Stuart McCullochf3173222012-06-07 21:57:32 +0000271 if (f.getParentFile() == null)
272 throw new IllegalArgumentException("Cannot recursively delete root for safety reasons");
273
Stuart McCulloch4482c702012-06-15 13:27:53 +0000274 boolean wasDeleted = true;
Stuart McCullochf3173222012-06-07 21:57:32 +0000275 if (f.isDirectory()) {
276 File[] subs = f.listFiles();
Stuart McCulloch4482c702012-06-15 13:27:53 +0000277 for (File sub : subs) {
278 try {
279 deleteWithException(sub);
280 } catch (IOException e) {
281 wasDeleted = false;
282 }
283 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000284 }
285
Stuart McCulloch4482c702012-06-15 13:27:53 +0000286 boolean fDeleted = f.delete();
287 if (!fDeleted || !wasDeleted) {
288 throw new IOException("Failed to delete " + f.getAbsoluteFile());
289 }
Stuart McCullochf3173222012-06-07 21:57:32 +0000290 }
291
Stuart McCulloch4482c702012-06-15 13:27:53 +0000292 /** Deletes <code>to</code> file if it exists, and renames <code>from</code> file to <code>to</code>.<br>
293 * Throws exception the rename operation fails.
294 * @param from source file
295 * @param to destination file
296 * @throws IOException if the rename operation fails
297 */
298 public static void rename(File from, File to) throws IOException {
299 IO.deleteWithException(to);
300
301 boolean renamed = from.renameTo(to);
302 if (!renamed) throw new IOException("Could not rename " + from.getAbsoluteFile() + " to " + to.getAbsoluteFile());
303 }
304
305
Stuart McCullochf3173222012-06-07 21:57:32 +0000306 public static long drain(InputStream in) throws IOException {
307 long result = 0;
308 byte[] buffer = new byte[10000];
309 try {
310 int size = in.read(buffer);
311 while (size >= 0) {
312 result += size;
313 size = in.read(buffer);
314 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000315 }
316 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +0000317 in.close();
318 }
319 return result;
320 }
321
Stuart McCulloch4482c702012-06-15 13:27:53 +0000322 public void copy(Collection< ? > c, OutputStream out) throws IOException {
323 Writer w = new OutputStreamWriter(out, "UTF-8");
Stuart McCullochf3173222012-06-07 21:57:32 +0000324 PrintWriter ps = new PrintWriter(w);
325 for (Object o : c) {
326 ps.println(o);
327 }
328 ps.flush();
329 w.flush();
330 }
331
332 public static Throwable close(Closeable in) {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000333 if (in == null)
Stuart McCullochf3173222012-06-07 21:57:32 +0000334 return null;
Stuart McCulloch4482c702012-06-15 13:27:53 +0000335
Stuart McCullochf3173222012-06-07 21:57:32 +0000336 try {
337 in.close();
338 return null;
Stuart McCulloch4482c702012-06-15 13:27:53 +0000339 }
340 catch (Throwable e) {
Stuart McCullochf3173222012-06-07 21:57:32 +0000341 return e;
342 }
343 }
344
345 public static URL toURL(String s, File base) throws MalformedURLException {
346 int n = s.indexOf(':');
347 if (n > 0 && n < 10) {
348 // is url
349 return new URL(s);
350 }
351 return getFile(base, s).toURI().toURL();
352 }
353
354 public static void store(Object o, File out) throws IOException {
355 store(o, out, "UTF-8");
356 }
357
358 public static void store(Object o, File out, String encoding) throws IOException {
359 FileOutputStream fout = new FileOutputStream(out);
360 try {
361 store(o, fout, encoding);
Stuart McCulloch4482c702012-06-15 13:27:53 +0000362 }
363 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +0000364 fout.close();
365 }
366 }
367
Stuart McCulloch4482c702012-06-15 13:27:53 +0000368 public static void store(Object o, OutputStream fout) throws UnsupportedEncodingException, IOException {
Stuart McCullochf3173222012-06-07 21:57:32 +0000369 store(o, fout, "UTF-8");
370 }
371
Stuart McCulloch4482c702012-06-15 13:27:53 +0000372 public static void store(Object o, OutputStream fout, String encoding) throws UnsupportedEncodingException,
373 IOException {
Stuart McCullochf3173222012-06-07 21:57:32 +0000374 String s;
375
376 if (o == null)
377 s = "";
378 else
379 s = o.toString();
380
381 try {
382 fout.write(s.getBytes(encoding));
Stuart McCulloch4482c702012-06-15 13:27:53 +0000383 }
384 finally {
Stuart McCullochf3173222012-06-07 21:57:32 +0000385 fout.close();
386 }
387 }
388
389 public static InputStream stream(String s) {
390 try {
391 return new ByteArrayInputStream(s.getBytes("UTF-8"));
Stuart McCulloch4482c702012-06-15 13:27:53 +0000392 }
393 catch (Exception e) {
Stuart McCullochf3173222012-06-07 21:57:32 +0000394 // Ignore
395 return null;
396 }
397 }
398
399 public static InputStream stream(String s, String encoding) throws UnsupportedEncodingException {
400 return new ByteArrayInputStream(s.getBytes(encoding));
401 }
402
403 public static InputStream stream(File s) throws FileNotFoundException {
404 return new FileInputStream(s);
405 }
406
407 public static InputStream stream(URL s) throws IOException {
408 return s.openStream();
409 }
410
411 public static Reader reader(String s) {
412 return new StringReader(s);
413 }
414
Stuart McCullochf3173222012-06-07 21:57:32 +0000415 public static BufferedReader reader(File f, String encoding) throws IOException {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000416 return reader(new FileInputStream(f), encoding);
Stuart McCullochf3173222012-06-07 21:57:32 +0000417 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000418
Stuart McCullochf3173222012-06-07 21:57:32 +0000419 public static BufferedReader reader(File f) throws IOException {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000420 return reader(f, "UTF-8");
Stuart McCullochf3173222012-06-07 21:57:32 +0000421 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000422
Stuart McCullochf3173222012-06-07 21:57:32 +0000423 public static PrintWriter writer(File f, String encoding) throws IOException {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000424 return writer(new FileOutputStream(f), encoding);
Stuart McCullochf3173222012-06-07 21:57:32 +0000425 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000426
Stuart McCullochf3173222012-06-07 21:57:32 +0000427 public static PrintWriter writer(File f) throws IOException {
428 return writer(f, "UTF-8");
429 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000430
Stuart McCullochf3173222012-06-07 21:57:32 +0000431 public static PrintWriter writer(OutputStream out, String encoding) throws IOException {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000432 return new PrintWriter(new OutputStreamWriter(out, encoding));
Stuart McCullochf3173222012-06-07 21:57:32 +0000433 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000434
Stuart McCullochf3173222012-06-07 21:57:32 +0000435 public static BufferedReader reader(InputStream in, String encoding) throws IOException {
Stuart McCulloch4482c702012-06-15 13:27:53 +0000436 return new BufferedReader(new InputStreamReader(in, encoding));
Stuart McCullochf3173222012-06-07 21:57:32 +0000437 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000438
Stuart McCullochf3173222012-06-07 21:57:32 +0000439 public static BufferedReader reader(InputStream in) throws IOException {
440 return reader(in, "UTF-8");
441 }
Stuart McCulloch4482c702012-06-15 13:27:53 +0000442
Stuart McCullochf3173222012-06-07 21:57:32 +0000443 public static PrintWriter writer(OutputStream out) throws IOException {
444 return writer(out, "UTF-8");
445 }
446}