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 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 19 | } |
| 20 | finally { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 21 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 30 | public static void copy(byte[] r, Writer w) throws IOException { |
| 31 | copy(new ByteArrayInputStream(r), w, "UTF-8"); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 34 | public static void copy(byte[] r, OutputStream w) throws IOException { |
| 35 | copy(new ByteArrayInputStream(r), w); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 36 | } |
| 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 42 | } |
| 43 | finally { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 44 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 56 | } |
| 57 | finally { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 58 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 76 | } |
| 77 | finally { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 78 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 90 | } |
| 91 | finally { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 92 | 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 McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 108 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 116 | } |
| 117 | finally { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 118 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 150 | } |
| 151 | finally { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 152 | out.close(); |
| 153 | } |
| 154 | } else if (a.isDirectory()) { |
| 155 | b.mkdirs(); |
| 156 | if (!b.isDirectory()) |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 157 | throw new IllegalArgumentException("target directory for a directory must be a directory: " + b); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 158 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 170 | } |
| 171 | finally { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 172 | 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 McCulloch | a21b9e8 | 2012-08-02 13:26:25 +0000 | [diff] [blame^] | 225 | /** |
| 226 | * Create a temporary file. |
| 227 | * |
| 228 | * @param directory |
| 229 | * the directory in which to create the file. Can be null, in |
| 230 | * which case the system TMP directory is used |
| 231 | * @param pattern |
| 232 | * the filename prefix pattern. Must be at least 3 characters |
| 233 | * long |
| 234 | * @param suffix |
| 235 | * the filename suffix. Can be null, in which case (system) |
| 236 | * default suffix is used |
| 237 | * @return |
| 238 | * @throws IllegalArgumentException |
| 239 | * when pattern is null or too short |
| 240 | * @throws IOException |
| 241 | * when the specified (non-null) directory is not a directory |
| 242 | */ |
| 243 | public static File createTempFile(File directory, String pattern, String suffix) throws IllegalArgumentException, |
| 244 | IOException { |
| 245 | if ((pattern == null) || (pattern.length() < 3)) { |
| 246 | throw new IllegalArgumentException("Pattern must be at least 3 characters long, got " |
| 247 | + ((pattern == null) ? "null" : pattern.length())); |
| 248 | } |
| 249 | |
| 250 | if ((directory != null) && !directory.isDirectory()) { |
| 251 | throw new FileNotFoundException("Directory " + directory + " is not a directory"); |
| 252 | } |
| 253 | |
| 254 | return File.createTempFile(pattern, suffix, directory); |
| 255 | } |
| 256 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 257 | public static File getFile(String filename) { |
| 258 | return new File(filename.replace("/", File.separator)); |
| 259 | } |
| 260 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 261 | public static File getFile(File base, String file) { |
| 262 | File f = new File(file); |
| 263 | if (f.isAbsolute()) |
| 264 | return f; |
| 265 | int n; |
| 266 | |
| 267 | f = base.getAbsoluteFile(); |
| 268 | while ((n = file.indexOf('/')) > 0) { |
| 269 | String first = file.substring(0, n); |
| 270 | file = file.substring(n + 1); |
| 271 | if (first.equals("..")) |
| 272 | f = f.getParentFile(); |
| 273 | else |
| 274 | f = new File(f, first); |
| 275 | } |
| 276 | if (file.equals("..")) |
| 277 | return f.getParentFile(); |
| 278 | return new File(f, file).getAbsoluteFile(); |
| 279 | } |
| 280 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 281 | /** Deletes the specified file. |
| 282 | * Folders are recursively deleted.<br> |
| 283 | * If file(s) cannot be deleted, no feedback is provided (fail silently). |
| 284 | * @param f file to be deleted |
| 285 | */ |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 286 | public static void delete(File f) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 287 | try { |
| 288 | deleteWithException(f); |
| 289 | } catch (IOException e) { |
| 290 | // Ignore a failed delete |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /** Deletes the specified file. |
| 295 | * Folders are recursively deleted.<br> |
| 296 | * Throws exception if any of the files could not be deleted. |
| 297 | * @param f file to be deleted |
| 298 | * @throws IOException if the file (or contents of a folder) could not be deleted |
| 299 | */ |
| 300 | public static void deleteWithException(File f) throws IOException { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 301 | f = f.getAbsoluteFile(); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 302 | if (!f.exists()) return; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 303 | if (f.getParentFile() == null) |
| 304 | throw new IllegalArgumentException("Cannot recursively delete root for safety reasons"); |
| 305 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 306 | boolean wasDeleted = true; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 307 | if (f.isDirectory()) { |
| 308 | File[] subs = f.listFiles(); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 309 | for (File sub : subs) { |
| 310 | try { |
| 311 | deleteWithException(sub); |
| 312 | } catch (IOException e) { |
| 313 | wasDeleted = false; |
| 314 | } |
| 315 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 318 | boolean fDeleted = f.delete(); |
| 319 | if (!fDeleted || !wasDeleted) { |
| 320 | throw new IOException("Failed to delete " + f.getAbsoluteFile()); |
| 321 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 324 | /** Deletes <code>to</code> file if it exists, and renames <code>from</code> file to <code>to</code>.<br> |
| 325 | * Throws exception the rename operation fails. |
| 326 | * @param from source file |
| 327 | * @param to destination file |
| 328 | * @throws IOException if the rename operation fails |
| 329 | */ |
| 330 | public static void rename(File from, File to) throws IOException { |
| 331 | IO.deleteWithException(to); |
| 332 | |
| 333 | boolean renamed = from.renameTo(to); |
| 334 | if (!renamed) throw new IOException("Could not rename " + from.getAbsoluteFile() + " to " + to.getAbsoluteFile()); |
| 335 | } |
| 336 | |
| 337 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 338 | public static long drain(InputStream in) throws IOException { |
| 339 | long result = 0; |
| 340 | byte[] buffer = new byte[10000]; |
| 341 | try { |
| 342 | int size = in.read(buffer); |
| 343 | while (size >= 0) { |
| 344 | result += size; |
| 345 | size = in.read(buffer); |
| 346 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 347 | } |
| 348 | finally { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 349 | in.close(); |
| 350 | } |
| 351 | return result; |
| 352 | } |
| 353 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 354 | public void copy(Collection< ? > c, OutputStream out) throws IOException { |
| 355 | Writer w = new OutputStreamWriter(out, "UTF-8"); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 356 | PrintWriter ps = new PrintWriter(w); |
| 357 | for (Object o : c) { |
| 358 | ps.println(o); |
| 359 | } |
| 360 | ps.flush(); |
| 361 | w.flush(); |
| 362 | } |
| 363 | |
| 364 | public static Throwable close(Closeable in) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 365 | if (in == null) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 366 | return null; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 367 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 368 | try { |
| 369 | in.close(); |
| 370 | return null; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 371 | } |
| 372 | catch (Throwable e) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 373 | return e; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | public static URL toURL(String s, File base) throws MalformedURLException { |
| 378 | int n = s.indexOf(':'); |
| 379 | if (n > 0 && n < 10) { |
| 380 | // is url |
| 381 | return new URL(s); |
| 382 | } |
| 383 | return getFile(base, s).toURI().toURL(); |
| 384 | } |
| 385 | |
| 386 | public static void store(Object o, File out) throws IOException { |
| 387 | store(o, out, "UTF-8"); |
| 388 | } |
| 389 | |
| 390 | public static void store(Object o, File out, String encoding) throws IOException { |
| 391 | FileOutputStream fout = new FileOutputStream(out); |
| 392 | try { |
| 393 | store(o, fout, encoding); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 394 | } |
| 395 | finally { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 396 | fout.close(); |
| 397 | } |
| 398 | } |
| 399 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 400 | public static void store(Object o, OutputStream fout) throws UnsupportedEncodingException, IOException { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 401 | store(o, fout, "UTF-8"); |
| 402 | } |
| 403 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 404 | public static void store(Object o, OutputStream fout, String encoding) throws UnsupportedEncodingException, |
| 405 | IOException { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 406 | String s; |
| 407 | |
| 408 | if (o == null) |
| 409 | s = ""; |
| 410 | else |
| 411 | s = o.toString(); |
| 412 | |
| 413 | try { |
| 414 | fout.write(s.getBytes(encoding)); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 415 | } |
| 416 | finally { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 417 | fout.close(); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | public static InputStream stream(String s) { |
| 422 | try { |
| 423 | return new ByteArrayInputStream(s.getBytes("UTF-8")); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 424 | } |
| 425 | catch (Exception e) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 426 | // Ignore |
| 427 | return null; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | public static InputStream stream(String s, String encoding) throws UnsupportedEncodingException { |
| 432 | return new ByteArrayInputStream(s.getBytes(encoding)); |
| 433 | } |
| 434 | |
| 435 | public static InputStream stream(File s) throws FileNotFoundException { |
| 436 | return new FileInputStream(s); |
| 437 | } |
| 438 | |
| 439 | public static InputStream stream(URL s) throws IOException { |
| 440 | return s.openStream(); |
| 441 | } |
| 442 | |
| 443 | public static Reader reader(String s) { |
| 444 | return new StringReader(s); |
| 445 | } |
| 446 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 447 | public static BufferedReader reader(File f, String encoding) throws IOException { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 448 | return reader(new FileInputStream(f), encoding); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 449 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 450 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 451 | public static BufferedReader reader(File f) throws IOException { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 452 | return reader(f, "UTF-8"); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 453 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 454 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 455 | public static PrintWriter writer(File f, String encoding) throws IOException { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 456 | return writer(new FileOutputStream(f), encoding); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 457 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 458 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 459 | public static PrintWriter writer(File f) throws IOException { |
| 460 | return writer(f, "UTF-8"); |
| 461 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 462 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 463 | public static PrintWriter writer(OutputStream out, String encoding) throws IOException { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 464 | return new PrintWriter(new OutputStreamWriter(out, encoding)); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 465 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 466 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 467 | public static BufferedReader reader(InputStream in, String encoding) throws IOException { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 468 | return new BufferedReader(new InputStreamReader(in, encoding)); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 469 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 470 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 471 | public static BufferedReader reader(InputStream in) throws IOException { |
| 472 | return reader(in, "UTF-8"); |
| 473 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 474 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 475 | public static PrintWriter writer(OutputStream out) throws IOException { |
| 476 | return writer(out, "UTF-8"); |
| 477 | } |
| 478 | } |