Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.io; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.net.*; |
| 5 | import java.nio.*; |
| 6 | import java.security.*; |
| 7 | import java.util.*; |
| 8 | |
| 9 | public 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 | } |
| 19 | } finally { |
| 20 | r.close(); |
| 21 | w.flush(); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | public static void copy(InputStream r, Writer w) throws IOException { |
| 26 | copy(r, w, "UTF-8"); |
| 27 | } |
| 28 | |
| 29 | public static void copy(byte []r, Writer w) throws IOException { |
| 30 | copy( new ByteArrayInputStream(r), w, "UTF-8"); |
| 31 | } |
| 32 | |
| 33 | public static void copy(byte []r, OutputStream w) throws IOException { |
| 34 | copy( new ByteArrayInputStream(r), w); |
| 35 | } |
| 36 | |
| 37 | public static void copy(InputStream r, Writer w, String charset) throws IOException { |
| 38 | try { |
| 39 | InputStreamReader isr = new InputStreamReader(r, charset); |
| 40 | copy(isr, w); |
| 41 | } finally { |
| 42 | r.close(); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | public static void copy(Reader r, OutputStream o) throws IOException { |
| 47 | copy(r, o, "UTF-8"); |
| 48 | } |
| 49 | |
| 50 | public static void copy(Reader r, OutputStream o, String charset) throws IOException { |
| 51 | try { |
| 52 | OutputStreamWriter osw = new OutputStreamWriter(o, charset); |
| 53 | copy(r, osw); |
| 54 | } finally { |
| 55 | r.close(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | public static void copy(InputStream in, OutputStream out) throws IOException { |
| 60 | DataOutputStream dos = new DataOutputStream(out); |
| 61 | copy(in, (DataOutput) dos); |
| 62 | out.flush(); |
| 63 | } |
| 64 | |
| 65 | public static void copy(InputStream in, DataOutput out) throws IOException { |
| 66 | byte[] buffer = new byte[10000]; |
| 67 | try { |
| 68 | int size = in.read(buffer); |
| 69 | while (size > 0) { |
| 70 | out.write(buffer, 0, size); |
| 71 | size = in.read(buffer); |
| 72 | } |
| 73 | } finally { |
| 74 | in.close(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public static void copy(InputStream in, ByteBuffer bb) throws IOException { |
| 79 | byte[] buffer = new byte[10000]; |
| 80 | try { |
| 81 | int size = in.read(buffer); |
| 82 | while (size > 0) { |
| 83 | bb.put(buffer, 0, size); |
| 84 | size = in.read(buffer); |
| 85 | } |
| 86 | } finally { |
| 87 | in.close(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public static void copy(URL in, MessageDigest md) throws IOException { |
| 92 | copy(in.openStream(), md); |
| 93 | } |
| 94 | |
| 95 | public static void copy(File in, MessageDigest md) throws IOException { |
| 96 | copy(new FileInputStream(in), md); |
| 97 | } |
| 98 | |
| 99 | public static void copy(URLConnection in, MessageDigest md) throws IOException { |
| 100 | copy(in.getInputStream(), md); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | public static void copy(InputStream in, MessageDigest md) throws IOException { |
| 105 | byte[] buffer = new byte[10000]; |
| 106 | try { |
| 107 | int size = in.read(buffer); |
| 108 | while (size > 0) { |
| 109 | md.update(buffer, 0, size); |
| 110 | size = in.read(buffer); |
| 111 | } |
| 112 | } finally { |
| 113 | in.close(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | public static void copy(URL url, File file) throws IOException { |
| 118 | URLConnection c = url.openConnection(); |
| 119 | copy(c, file); |
| 120 | } |
| 121 | |
| 122 | public static void copy(URLConnection c, File file) throws IOException { |
| 123 | copy(c.getInputStream(), file); |
| 124 | } |
| 125 | |
| 126 | public static void copy(InputStream in, URL out) throws IOException { |
| 127 | copy(in, out, null); |
| 128 | } |
| 129 | |
| 130 | public static void copy(InputStream in, URL out, String method) throws IOException { |
| 131 | URLConnection c = out.openConnection(); |
| 132 | if (c instanceof HttpURLConnection && method != null) { |
| 133 | HttpURLConnection http = (HttpURLConnection) c; |
| 134 | http.setRequestMethod(method); |
| 135 | } |
| 136 | c.setDoOutput(true); |
| 137 | copy(in, c.getOutputStream()); |
| 138 | } |
| 139 | |
| 140 | public static void copy(File a, File b) throws IOException { |
| 141 | if (a.isFile()) { |
| 142 | FileOutputStream out = new FileOutputStream(b); |
| 143 | try { |
| 144 | copy(new FileInputStream(a), out); |
| 145 | } finally { |
| 146 | out.close(); |
| 147 | } |
| 148 | } else if (a.isDirectory()) { |
| 149 | b.mkdirs(); |
| 150 | if (!b.isDirectory()) |
| 151 | throw new IllegalArgumentException( |
| 152 | "target directory for a directory must be a directory: " + b); |
| 153 | File subs[] = a.listFiles(); |
| 154 | for (File sub : subs) { |
| 155 | copy(sub, new File(b, sub.getName())); |
| 156 | } |
| 157 | } else |
| 158 | throw new FileNotFoundException("During copy: " + a.toString()); |
| 159 | } |
| 160 | |
| 161 | public static void copy(InputStream a, File b) throws IOException { |
| 162 | FileOutputStream out = new FileOutputStream(b); |
| 163 | try { |
| 164 | copy(a, out); |
| 165 | } finally { |
| 166 | out.close(); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | public static void copy(File a, OutputStream b) throws IOException { |
| 171 | copy(new FileInputStream(a), b); |
| 172 | } |
| 173 | |
| 174 | public static String collect(File a, String encoding) throws IOException { |
| 175 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 176 | copy(a, out); |
| 177 | return new String(out.toByteArray(), encoding); |
| 178 | } |
| 179 | |
| 180 | public static String collect(URL a, String encoding) throws IOException { |
| 181 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 182 | copy(a.openStream(), out); |
| 183 | return new String(out.toByteArray(), encoding); |
| 184 | } |
| 185 | |
| 186 | public static String collect(URL a) throws IOException { |
| 187 | return collect(a, "UTF-8"); |
| 188 | } |
| 189 | |
| 190 | public static String collect(File a) throws IOException { |
| 191 | return collect(a, "UTF-8"); |
| 192 | } |
| 193 | |
| 194 | public static String collect(String a) throws IOException { |
| 195 | return collect(new File(a), "UTF-8"); |
| 196 | } |
| 197 | |
| 198 | public static String collect(InputStream a, String encoding) throws IOException { |
| 199 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 200 | copy(a, out); |
| 201 | return new String(out.toByteArray(), encoding); |
| 202 | } |
| 203 | |
| 204 | public static String collect(InputStream a) throws IOException { |
| 205 | return collect(a, "UTF-8"); |
| 206 | } |
| 207 | |
| 208 | public static String collect(Reader a) throws IOException { |
| 209 | StringWriter sw = new StringWriter(); |
| 210 | char[] buffer = new char[10000]; |
| 211 | int size = a.read(buffer); |
| 212 | while (size > 0) { |
| 213 | sw.write(buffer, 0, size); |
| 214 | size = a.read(buffer); |
| 215 | } |
| 216 | return sw.toString(); |
| 217 | } |
| 218 | |
| 219 | public static File getFile(File base, String file) { |
| 220 | File f = new File(file); |
| 221 | if (f.isAbsolute()) |
| 222 | return f; |
| 223 | int n; |
| 224 | |
| 225 | f = base.getAbsoluteFile(); |
| 226 | while ((n = file.indexOf('/')) > 0) { |
| 227 | String first = file.substring(0, n); |
| 228 | file = file.substring(n + 1); |
| 229 | if (first.equals("..")) |
| 230 | f = f.getParentFile(); |
| 231 | else |
| 232 | f = new File(f, first); |
| 233 | } |
| 234 | if (file.equals("..")) |
| 235 | return f.getParentFile(); |
| 236 | return new File(f, file).getAbsoluteFile(); |
| 237 | } |
| 238 | |
| 239 | public static void delete(File f) { |
| 240 | f = f.getAbsoluteFile(); |
| 241 | if (f.getParentFile() == null) |
| 242 | throw new IllegalArgumentException("Cannot recursively delete root for safety reasons"); |
| 243 | |
| 244 | if (f.isDirectory()) { |
| 245 | File[] subs = f.listFiles(); |
| 246 | for (File sub : subs) |
| 247 | delete(sub); |
| 248 | } |
| 249 | |
| 250 | f.delete(); |
| 251 | } |
| 252 | |
| 253 | public static long drain(InputStream in) throws IOException { |
| 254 | long result = 0; |
| 255 | byte[] buffer = new byte[10000]; |
| 256 | try { |
| 257 | int size = in.read(buffer); |
| 258 | while (size >= 0) { |
| 259 | result += size; |
| 260 | size = in.read(buffer); |
| 261 | } |
| 262 | } finally { |
| 263 | in.close(); |
| 264 | } |
| 265 | return result; |
| 266 | } |
| 267 | |
| 268 | public void copy(Collection<?> c, OutputStream out) throws IOException { |
| 269 | Writer w = new OutputStreamWriter(out,"UTF-8"); |
| 270 | PrintWriter ps = new PrintWriter(w); |
| 271 | for (Object o : c) { |
| 272 | ps.println(o); |
| 273 | } |
| 274 | ps.flush(); |
| 275 | w.flush(); |
| 276 | } |
| 277 | |
| 278 | public static Throwable close(Closeable in) { |
| 279 | if ( in == null) |
| 280 | return null; |
| 281 | |
| 282 | try { |
| 283 | in.close(); |
| 284 | return null; |
| 285 | } catch (Throwable e) { |
| 286 | return e; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | public static URL toURL(String s, File base) throws MalformedURLException { |
| 291 | int n = s.indexOf(':'); |
| 292 | if (n > 0 && n < 10) { |
| 293 | // is url |
| 294 | return new URL(s); |
| 295 | } |
| 296 | return getFile(base, s).toURI().toURL(); |
| 297 | } |
| 298 | |
| 299 | public static void store(Object o, File out) throws IOException { |
| 300 | store(o, out, "UTF-8"); |
| 301 | } |
| 302 | |
| 303 | public static void store(Object o, File out, String encoding) throws IOException { |
| 304 | FileOutputStream fout = new FileOutputStream(out); |
| 305 | try { |
| 306 | store(o, fout, encoding); |
| 307 | } finally { |
| 308 | fout.close(); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | public static void store(Object o, OutputStream fout) throws UnsupportedEncodingException, |
| 313 | IOException { |
| 314 | store(o, fout, "UTF-8"); |
| 315 | } |
| 316 | |
| 317 | public static void store(Object o, OutputStream fout, String encoding) |
| 318 | throws UnsupportedEncodingException, IOException { |
| 319 | String s; |
| 320 | |
| 321 | if (o == null) |
| 322 | s = ""; |
| 323 | else |
| 324 | s = o.toString(); |
| 325 | |
| 326 | try { |
| 327 | fout.write(s.getBytes(encoding)); |
| 328 | } finally { |
| 329 | fout.close(); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | public static InputStream stream(String s) { |
| 334 | try { |
| 335 | return new ByteArrayInputStream(s.getBytes("UTF-8")); |
| 336 | } catch (Exception e) { |
| 337 | // Ignore |
| 338 | return null; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | public static InputStream stream(String s, String encoding) throws UnsupportedEncodingException { |
| 343 | return new ByteArrayInputStream(s.getBytes(encoding)); |
| 344 | } |
| 345 | |
| 346 | public static InputStream stream(File s) throws FileNotFoundException { |
| 347 | return new FileInputStream(s); |
| 348 | } |
| 349 | |
| 350 | public static InputStream stream(URL s) throws IOException { |
| 351 | return s.openStream(); |
| 352 | } |
| 353 | |
| 354 | public static Reader reader(String s) { |
| 355 | return new StringReader(s); |
| 356 | } |
| 357 | |
| 358 | |
| 359 | public static BufferedReader reader(File f, String encoding) throws IOException { |
| 360 | return reader( new FileInputStream(f), encoding); |
| 361 | } |
| 362 | |
| 363 | public static BufferedReader reader(File f) throws IOException { |
| 364 | return reader(f,"UTF-8"); |
| 365 | } |
| 366 | |
| 367 | public static PrintWriter writer(File f, String encoding) throws IOException { |
| 368 | return writer( new FileOutputStream(f),encoding); |
| 369 | } |
| 370 | |
| 371 | public static PrintWriter writer(File f) throws IOException { |
| 372 | return writer(f, "UTF-8"); |
| 373 | } |
| 374 | |
| 375 | public static PrintWriter writer(OutputStream out, String encoding) throws IOException { |
| 376 | return new PrintWriter( new OutputStreamWriter( out,encoding)); |
| 377 | } |
| 378 | public static BufferedReader reader(InputStream in, String encoding) throws IOException { |
| 379 | return new BufferedReader( new InputStreamReader(in, encoding)); |
| 380 | } |
| 381 | |
| 382 | public static BufferedReader reader(InputStream in) throws IOException { |
| 383 | return reader(in, "UTF-8"); |
| 384 | } |
| 385 | public static PrintWriter writer(OutputStream out) throws IOException { |
| 386 | return writer(out, "UTF-8"); |
| 387 | } |
| 388 | } |