Stuart McCulloch | f317322 | 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 { |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 10 | static public File work = new File(System.getProperty("user.dir")); |
| 11 | static public File home = new File(System.getProperty("user.home")); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 12 | |
| 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 21 | } |
| 22 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 23 | 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 32 | public static void copy(byte[] r, Writer w) throws IOException { |
| 33 | copy(new ByteArrayInputStream(r), w, "UTF-8"); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 36 | public static void copy(byte[] r, OutputStream w) throws IOException { |
| 37 | copy(new ByteArrayInputStream(r), w); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 38 | } |
| 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 44 | } |
| 45 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 46 | 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 58 | } |
| 59 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 60 | 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 78 | } |
| 79 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 80 | 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 92 | } |
| 93 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 94 | 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 McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 110 | 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 118 | } |
| 119 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 120 | 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 152 | } |
| 153 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 154 | out.close(); |
| 155 | } |
| 156 | } else if (a.isDirectory()) { |
Stuart McCulloch | 2929e2d | 2012-08-07 10:57:21 +0000 | [diff] [blame] | 157 | if (!b.exists() && !b.mkdirs()) { |
| 158 | throw new IOException("Could not create directory " + b); |
| 159 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 160 | if (!b.isDirectory()) |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 161 | throw new IllegalArgumentException("target directory for a directory must be a directory: " + b); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 162 | 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 174 | } |
| 175 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 176 | 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 McCulloch | 55fbda5 | 2012-08-02 13:26:25 +0000 | [diff] [blame] | 229 | /** |
| 230 | * Create a temporary file. |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 231 | * |
Stuart McCulloch | 55fbda5 | 2012-08-02 13:26:25 +0000 | [diff] [blame] | 232 | * @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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 261 | public static File getFile(String filename) { |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 262 | return getFile(work, filename); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 263 | } |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 264 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 265 | public static File getFile(File base, String file) { |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 266 | |
| 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 McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 278 | File f = new File(file); |
| 279 | if (f.isAbsolute()) |
| 280 | return f; |
| 281 | int n; |
| 282 | |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 283 | if (base == null) |
| 284 | base = work; |
| 285 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 286 | f = base.getAbsoluteFile(); |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 287 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 288 | 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 McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 301 | /** |
| 302 | * Deletes the specified file. Folders are recursively deleted.<br> |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 303 | * If file(s) cannot be deleted, no feedback is provided (fail silently). |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 304 | * |
| 305 | * @param f |
| 306 | * file to be deleted |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 307 | */ |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 308 | public static void delete(File f) { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 309 | try { |
| 310 | deleteWithException(f); |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 311 | } |
| 312 | catch (IOException e) { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 313 | // Ignore a failed delete |
| 314 | } |
| 315 | } |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 316 | |
| 317 | /** |
| 318 | * Deletes the specified file. Folders are recursively deleted.<br> |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 319 | * Throws exception if any of the files could not be deleted. |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 320 | * |
| 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 325 | */ |
| 326 | public static void deleteWithException(File f) throws IOException { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 327 | f = f.getAbsoluteFile(); |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 328 | if (!f.exists()) |
| 329 | return; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 330 | if (f.getParentFile() == null) |
| 331 | throw new IllegalArgumentException("Cannot recursively delete root for safety reasons"); |
| 332 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 333 | boolean wasDeleted = true; |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 334 | if (f.isDirectory()) { |
| 335 | File[] subs = f.listFiles(); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 336 | for (File sub : subs) { |
| 337 | try { |
| 338 | deleteWithException(sub); |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 339 | } |
| 340 | catch (IOException e) { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 341 | wasDeleted = false; |
| 342 | } |
| 343 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 346 | boolean fDeleted = f.delete(); |
| 347 | if (!fDeleted || !wasDeleted) { |
| 348 | throw new IOException("Failed to delete " + f.getAbsoluteFile()); |
| 349 | } |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 352 | /** |
| 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 366 | |
Stuart McCulloch | 2a0afd6 | 2012-09-06 18:28:06 +0000 | [diff] [blame^] | 367 | boolean renamed = from.renameTo(to); |
| 368 | if (!renamed) |
| 369 | throw new IOException("Could not rename " + from.getAbsoluteFile() + " to " + to.getAbsoluteFile()); |
| 370 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 371 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 372 | 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 381 | } |
| 382 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 383 | in.close(); |
| 384 | } |
| 385 | return result; |
| 386 | } |
| 387 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 388 | public void copy(Collection< ? > c, OutputStream out) throws IOException { |
| 389 | Writer w = new OutputStreamWriter(out, "UTF-8"); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 390 | 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 399 | if (in == null) |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 400 | return null; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 401 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 402 | try { |
| 403 | in.close(); |
| 404 | return null; |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 405 | } |
| 406 | catch (Throwable e) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 407 | 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 428 | } |
| 429 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 430 | fout.close(); |
| 431 | } |
| 432 | } |
| 433 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 434 | public static void store(Object o, OutputStream fout) throws UnsupportedEncodingException, IOException { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 435 | store(o, fout, "UTF-8"); |
| 436 | } |
| 437 | |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 438 | public static void store(Object o, OutputStream fout, String encoding) throws UnsupportedEncodingException, |
| 439 | IOException { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 440 | 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 McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 449 | } |
| 450 | finally { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 451 | fout.close(); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | public static InputStream stream(String s) { |
| 456 | try { |
| 457 | return new ByteArrayInputStream(s.getBytes("UTF-8")); |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 458 | } |
| 459 | catch (Exception e) { |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 460 | // 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 McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 481 | public static BufferedReader reader(File f, String encoding) throws IOException { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 482 | return reader(new FileInputStream(f), encoding); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 483 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 484 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 485 | public static BufferedReader reader(File f) throws IOException { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 486 | return reader(f, "UTF-8"); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 487 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 488 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 489 | public static PrintWriter writer(File f, String encoding) throws IOException { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 490 | return writer(new FileOutputStream(f), encoding); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 491 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 492 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 493 | public static PrintWriter writer(File f) throws IOException { |
| 494 | return writer(f, "UTF-8"); |
| 495 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 496 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 497 | public static PrintWriter writer(OutputStream out, String encoding) throws IOException { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 498 | return new PrintWriter(new OutputStreamWriter(out, encoding)); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 499 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 500 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 501 | public static BufferedReader reader(InputStream in, String encoding) throws IOException { |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 502 | return new BufferedReader(new InputStreamReader(in, encoding)); |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 503 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 504 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 505 | public static BufferedReader reader(InputStream in) throws IOException { |
| 506 | return reader(in, "UTF-8"); |
| 507 | } |
Stuart McCulloch | 4482c70 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 508 | |
Stuart McCulloch | f317322 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 509 | public static PrintWriter writer(OutputStream out) throws IOException { |
| 510 | return writer(out, "UTF-8"); |
| 511 | } |
| 512 | } |