1 17 package org.columba.core.io; 18 19 import java.io.BufferedOutputStream ; 20 import java.io.BufferedReader ; 21 import java.io.BufferedWriter ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.InputStreamReader ; 28 import java.io.OutputStreamWriter ; 29 import java.net.URL ; 30 import java.util.logging.Logger ; 31 32 35 public final class DiskIO { 36 37 private static final Logger LOG = Logger.getLogger("org.columba.core.io"); 38 39 private static String resourceFolder = ""; 40 41 44 private DiskIO() { 45 } 47 48 63 public static boolean ensureDirectory(File dir) { 64 if (dir == null) { 65 throw new IllegalArgumentException ("dir = null"); 66 } 67 boolean success = true; 68 if (!dir.isDirectory()) { 69 success = !dir.isFile() && dir.mkdirs(); 70 71 if (success) { 72 LOG.info("Created directory: " + dir.toString()); 73 } else { 74 LOG.severe("failed while trying to create directory: " 75 + dir.toString()); 76 } 77 } 78 79 return success; 80 } 81 82 public static boolean ensureDirectory(String path) { 84 return ensureDirectory(new File (path)); 85 } 86 87 public static void saveStringInFile(File toFile, String insertString) 88 throws IOException { 89 BufferedWriter out; 90 91 out = new BufferedWriter (new OutputStreamWriter (new FileOutputStream ( 92 toFile), "ISO-8859-1")); 93 out.write(insertString); 94 out.flush(); 95 out.close(); 96 } 97 98 public static void saveStreamInFile(File toFile, InputStream is) 99 throws IOException { 100 101 BufferedOutputStream out = new BufferedOutputStream ( 102 new FileOutputStream (toFile)); 103 104 StreamUtils.streamCopy(is, out); 105 106 out.flush(); 107 out.close(); 108 109 is.close(); 110 } 111 112 public static String readFileInString(File fromFile) throws IOException { 114 StringBuffer strbuf = new StringBuffer ((int) fromFile.length()); 115 116 BufferedReader in = new BufferedReader (new InputStreamReader ( 117 new FileInputStream (fromFile), "ISO-8859-1")); 118 String str; 119 strbuf = new StringBuffer ((int) fromFile.length()); 120 121 while ((str = in.readLine()) != null) { 122 strbuf.append(str + "\n"); 123 } 124 125 in.close(); 126 127 return strbuf.toString(); 128 129 141 } 142 143 145 158 public static boolean deleteDirectory(File dir) { 159 File [] files; 160 File f; 161 int i; 162 boolean success = true; 163 164 if (dir == null) { 165 throw new IllegalArgumentException ("dir = null"); 166 } 167 168 if (!dir.isDirectory()) { 169 return false; 170 } 171 172 files = dir.listFiles(); 173 174 for (i = 0; i < files.length; i++) { 175 if ((f = files[i]).isDirectory()) { 176 deleteDirectory(f); 177 } else if (!f.delete()) { 178 LOG.severe("*** failed to delete file " + f.getAbsolutePath()); 179 } 180 } 181 182 success = dir.delete(); 183 184 if (!success) { 185 LOG.severe("*** failed to delete directory " 186 + dir.getAbsolutePath()); 187 } 188 189 return success; 190 } 191 192 194 201 public static boolean emptyDirectory(File dir) { 202 boolean success; 203 204 if (dir == null) { 205 throw new IllegalArgumentException ("dir = null"); 206 } 207 208 if ((success = dir.exists() && deleteDirectory(dir))) { 209 dir.mkdirs(); 210 } 211 212 return success && dir.exists(); 213 } 214 215 217 229 public static InputStream getResourceStream(String path) 230 throws java.io.IOException { 231 URL url; 232 233 if ((url = getResourceURL(path)) == null) { 234 return null; 235 } 236 return url.openStream(); 237 } 238 239 241 253 public static URL getResourceURL(String path) { 256 URL url; 257 258 if (path == null) { 259 throw new IllegalArgumentException ("path = null"); 260 } 261 262 if (path.startsWith("#")) { 263 path = resourceFolder + path.substring(1); 264 } 265 266 url = DiskIO.class.getResource("/" + path); 268 269 if (url == null) { 270 LOG.info("*** failed locating resource: " + path); 271 272 return null; 273 } 274 275 return url; 276 } 277 278 public static void setResourceRoot(String path) { 280 if (path == null) { 281 resourceFolder = ""; 282 } else { 283 if (!path.endsWith("/")) { 284 path += "/"; 285 } 286 287 resourceFolder = path; 288 } 289 } 290 291 public static String getResourceRoot() { 293 return resourceFolder; 294 } 295 296 307 public static void copyFile(File inputFile, File outputFile) 308 throws java.io.IOException { 309 FileInputStream in; 310 FileOutputStream out; 311 312 byte[] buffer = new byte[512]; 313 int len; 314 315 try { 316 out = new FileOutputStream (outputFile); 317 in = new FileInputStream (inputFile); 318 319 while ((len = in.read(buffer)) != -1) { 320 out.write(buffer, 0, len); 321 } 322 323 in.close(); 324 out.close(); 325 } catch (IOException e) { 326 LOG.info("*** error during file copy " 327 + outputFile.getAbsolutePath() + ": " + e.getMessage()); 328 throw e; 329 } 330 } 331 332 334 348 public static boolean copyResource(String resource, File outputFile) 349 throws java.io.IOException { 350 InputStream in; 351 FileOutputStream out; 352 byte[] buffer = new byte[512]; 353 int len; 354 355 try { 357 if ((in = DiskIO.getResourceStream(resource)) == null) { 358 return false; 359 } 360 361 out = new FileOutputStream (outputFile); 362 363 while ((len = in.read(buffer)) != -1) { 364 out.write(buffer, 0, len); 365 } 366 367 out.close(); 368 in.close(); 369 370 LOG.fine("created : " + outputFile.getAbsolutePath()); 371 } catch (IOException e) { 372 LOG.severe("*** error during resource file copy " 373 + outputFile.getAbsolutePath() + ": " + e.getMessage()); 374 throw e; 375 } 376 377 return true; 378 } 379 380 public static String readStringFromResource(String resource) 382 throws java.io.IOException { 383 InputStream in; 384 385 StringBuffer result = new StringBuffer (); 386 387 try { 389 if ((in = DiskIO.getResourceStream(resource)) == null) { 390 return ""; 391 } 392 393 BufferedReader reader = new BufferedReader ( 394 new InputStreamReader (in)); 395 396 String nextLine = reader.readLine(); 397 398 while (nextLine != null) { 399 result.append(nextLine); 400 result.append("\n"); 401 nextLine = reader.readLine(); 402 } 403 404 in.close(); 405 } catch (IOException e) { 406 e.printStackTrace(); 407 throw e; 408 } 409 410 return result.toString(); 411 } 412 413 417 public static boolean copyResource(String resource, String outputFile) 418 throws java.io.IOException { 419 return copyResource(resource, new File (outputFile)); 420 } 421 } | Popular Tags |