| 1 19 package org.java.plugin.util; 20 21 import java.io.BufferedInputStream ; 22 import java.io.BufferedOutputStream ; 23 import java.io.ByteArrayInputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.File ; 26 import java.io.FileFilter ; 27 import java.io.FileInputStream ; 28 import java.io.FileNotFoundException ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.io.OutputStream ; 33 import java.net.MalformedURLException ; 34 import java.net.URL ; 35 import java.util.Calendar ; 36 import java.util.Date ; 37 import java.util.Locale ; 38 import java.util.jar.JarFile ; 39 import java.util.zip.ZipEntry ; 40 41 46 public final class IoUtil { 47 private static final String PACKAGE_NAME = "org.java.plugin.util"; 49 55 public static void copyFile(final File src, final File dest) 56 throws IOException { 57 if (!src.isFile()) { 58 throw new IOException ( 59 ResourceManager.getMessage(PACKAGE_NAME, "notAFile", src)); } 61 if (dest.isDirectory()) { 62 throw new IOException ( 63 ResourceManager.getMessage(PACKAGE_NAME, "isFolder", dest)); } 65 BufferedInputStream in = new BufferedInputStream ( 66 new FileInputStream (src)); 67 try { 68 BufferedOutputStream out = new BufferedOutputStream ( 69 new FileOutputStream (dest, false)); 70 try { 71 copyStream(in, out, 1024); 72 } finally { 73 out.close(); 74 } 75 } finally { 76 in.close(); 77 } 78 dest.setLastModified(src.lastModified()); 79 } 80 81 87 public static void copyFolder(final File src, final File dest) 88 throws IOException { 89 copyFolder(src, dest, true, false, null); 90 } 91 92 99 public static void copyFolder(final File src, final File dest, 100 final boolean reqursive) throws IOException { 101 copyFolder(src, dest, reqursive, false, null); 102 } 103 104 105 114 public static void copyFolder(final File src, final File dest, 115 final boolean reqursive, final boolean onlyNew) throws IOException { 116 copyFolder(src, dest, reqursive, onlyNew, null); 117 } 118 119 130 public static void copyFolder(final File src, final File dest, 131 final boolean reqursive, final boolean onlyNew, 132 final FileFilter filter) throws IOException { 133 if (!src.isDirectory()) { 134 throw new IOException ( 135 ResourceManager.getMessage(PACKAGE_NAME, 136 "notAFolder", src)); } 138 if (dest.isFile()) { 139 throw new IOException ( 140 ResourceManager.getMessage(PACKAGE_NAME, "isFile", dest)); } 142 if (!dest.exists() && !dest.mkdirs()) { 143 throw new IOException ( 144 ResourceManager.getMessage(PACKAGE_NAME, 145 "cantMakeFolder", dest)); } 147 File [] srcFiles = src.listFiles(); 148 for (int i = 0; i < srcFiles.length; i++) { 149 File file = srcFiles[i]; 150 if ((filter != null) && !filter.accept(file)) { 151 continue; 152 } 153 if (file.isDirectory()) { 154 if (reqursive) { 155 copyFolder(file, new File (dest, file.getName()), reqursive, 156 onlyNew, filter); 157 } 158 continue; 159 } 160 File destFile = new File (dest, file.getName()); 161 if (onlyNew && destFile.isFile() 162 && (destFile.lastModified() > file.lastModified())) { 163 continue; 164 } 165 copyFile(file, destFile); 166 } 167 dest.setLastModified(src.lastModified()); 168 } 169 170 177 public static void copyStream(final InputStream in, final OutputStream out, 178 final int bufferSize) throws IOException { 179 byte[] buf = new byte[bufferSize]; 180 int len; 181 while ((len = in.read(buf)) != -1) { 182 out.write(buf, 0, len); 183 } 184 } 185 186 191 public static boolean emptyFolder(final File folder) { 192 if (!folder.isDirectory()) { 193 return true; 194 } 195 File [] files = folder.listFiles(); 196 boolean result = true; 197 for (int i = 0; i < files.length; i++) { 198 File file = files[i]; 199 if (file.isDirectory()) { 200 if (emptyFolder(file)) { 201 result &= file.delete(); 202 } else { 203 result = false; 204 } 205 } else { 206 result &= file.delete(); 207 } 208 } 209 return result; 210 } 211 212 222 public static boolean compareFiles(final File file1, final File file2) { 223 if (!file1.isFile() || !file2.isFile()) { 224 return false; 225 } 226 if (!file1.getName().equals(file2.getName())) { 227 return false; 228 } 229 if (file1.length() != file2.length()) { 230 return false; 231 } 232 return compareFileDates(new Date (file1.lastModified()), 233 new Date (file2.lastModified())); 234 } 235 236 244 public static boolean compareFileDates(final Date date1, final Date date2) { 245 if ((date1 == null) || (date2 == null)) { 246 return false; 247 } 248 Calendar cldr = Calendar.getInstance(Locale.ENGLISH); 249 cldr.setTime(date1); 250 cldr.set(Calendar.MILLISECOND, 0); 251 long dt1 = cldr.getTimeInMillis(); 252 cldr.setTime(date2); 253 cldr.set(Calendar.MILLISECOND, 0); 254 long dt2 = cldr.getTimeInMillis(); 255 return dt1 == dt2; 256 } 257 258 268 public static void synchronizeFolders(final File src, final File dest) 269 throws IOException { 270 synchronizeFolders(src, dest, null); 271 } 272 273 284 public static void synchronizeFolders(final File src, final File dest, 285 final FileFilter filter) throws IOException { 286 if (!src.isDirectory()) { 287 throw new IOException ( 288 ResourceManager.getMessage(PACKAGE_NAME, 289 "notAFolder", src)); } 291 if (dest.isFile()) { 292 throw new IOException ( 293 ResourceManager.getMessage(PACKAGE_NAME, "isFile", dest)); } 295 if (!dest.exists() && !dest.mkdirs()) { 296 throw new IOException ( 297 ResourceManager.getMessage(PACKAGE_NAME, 298 "cantMakeFolder", dest)); } 300 File [] srcFiles = src.listFiles(); 301 for (int i = 0; i < srcFiles.length; i++) { 302 File srcFile = srcFiles[i]; 303 if ((filter != null) && !filter.accept(srcFile)) { 304 continue; 305 } 306 File destFile = new File (dest, srcFile.getName()); 307 if (srcFile.isDirectory()) { 308 if (destFile.isFile() && !destFile.delete()) { 309 throw new IOException ( 310 ResourceManager.getMessage(PACKAGE_NAME, 311 "cantDeleteFile", destFile)); } 313 synchronizeFolders(srcFile, destFile, filter); 314 continue; 315 } 316 if (compareFiles(srcFile, destFile)) { 317 continue; 318 } 319 copyFile(srcFile, destFile); 320 } 321 File [] destFiles = dest.listFiles(); 322 for (int i = 0; i < destFiles.length; i++) { 323 File destFile = destFiles[i]; 324 File srcFile = new File (src, destFile.getName()); 325 if (((filter != null) && filter.accept(destFile) && srcFile.exists()) 326 || ((filter == null) && srcFile.exists())) { 327 continue; 328 } 329 if (destFile.isDirectory() && !emptyFolder(destFile)) { 330 throw new IOException ( 331 ResourceManager.getMessage(PACKAGE_NAME, 332 "cantEmptyFolder", destFile)); } 334 if (!destFile.delete()) { 335 throw new IOException ( 336 ResourceManager.getMessage(PACKAGE_NAME, 337 "cantDeleteFile", destFile)); } 339 } 340 dest.setLastModified(src.lastModified()); 341 } 342 343 348 public static boolean isResourceExists(final URL url) { 349 File file = url2file(url); 350 if (file != null) { 351 return file.canRead(); 352 } 353 if ("jar".equalsIgnoreCase(url.getProtocol())) { return isJarResourceExists(url); 355 } 356 return isUrlResourceExists(url); 357 } 358 359 private static boolean isUrlResourceExists(final URL url) { 360 try { 361 InputStream is = url.openStream(); 364 try { 365 is.close(); 366 } catch (IOException ioe) { 367 } 369 return true; 370 } catch (IOException ioe) { 371 return false; 372 } 373 } 374 375 private static boolean isJarResourceExists(final URL url) { 376 try { 377 String urlStr = url.toExternalForm(); 378 int p = urlStr.indexOf("!/"); if (p == -1) { return false; 381 } 382 URL fileUrl = new URL (urlStr.substring(4, p)); 383 File file = url2file(fileUrl); 384 if (file == null) { return isUrlResourceExists(url); 386 } 387 if (!file.canRead()) { 388 return false; 389 } 390 if (p == urlStr.length() - 2) { return true; 392 } 393 JarFile jarFile = new JarFile (file); 394 try { 395 return jarFile.getEntry(urlStr.substring(p + 2)) != null; 396 } finally { 397 jarFile.close(); 398 } 399 } catch (IOException ioe) { 400 return false; 401 } 402 } 403 404 421 public static InputStream getResourceInputStream(final URL url) 422 throws IOException { 423 File file = url2file(url); 424 if (file != null) { 425 return new BufferedInputStream (new FileInputStream (file)); 426 } 427 if (!"jar".equalsIgnoreCase(url.getProtocol())) { return url.openStream(); 429 } 430 String urlStr = url.toExternalForm(); 431 if (urlStr.endsWith("!/")) { throw new FileNotFoundException (url.toExternalForm()); 434 } 435 int p = urlStr.indexOf("!/"); if (p == -1) { 437 throw new MalformedURLException (url.toExternalForm()); 438 } 439 String path = urlStr.substring(p + 2); 440 file = url2file(new URL (urlStr.substring(4, p))); 441 if (file == null) { return url.openStream(); 443 } 444 JarFile jarFile = new JarFile (file); 445 try { 446 ZipEntry entry = jarFile.getEntry(path); 447 if (entry == null) { 448 throw new FileNotFoundException (url.toExternalForm()); 449 } 450 InputStream in = jarFile.getInputStream(entry); 451 try { 452 ByteArrayOutputStream out = new ByteArrayOutputStream (); 453 copyStream(in, out, 1024); 454 return new ByteArrayInputStream (out.toByteArray()); 455 } finally { 456 in.close(); 457 } 458 } finally { 459 jarFile.close(); 460 } 461 } 462 463 469 public static File url2file(final URL url) { 470 if (!"file".equalsIgnoreCase(url.getProtocol())) { return null; 472 } 473 return new File (url.getFile().replaceAll("%20", " ")); } 480 481 488 public static URL file2url(final File file) throws MalformedURLException { 489 try { 490 return file.getCanonicalFile().toURI().toURL(); 491 } catch (MalformedURLException mue) { 492 throw mue; 493 } catch (IOException ioe) { 494 throw new MalformedURLException ( 495 ResourceManager.getMessage(PACKAGE_NAME, "file2urlFailed", new Object [] {file, ioe})); 497 } catch (NoSuchMethodError nsme) { 498 } 501 try { 502 return new URL ("file://" + file.getCanonicalPath().replace('\\', '/') 504 .replaceAll(" ", "%20")); } catch (MalformedURLException mue) { 506 throw mue; 507 } catch (IOException ioe) { 508 throw new MalformedURLException ( 509 ResourceManager.getMessage(PACKAGE_NAME, "file2urlFailed", new Object [] {file, ioe})); 511 } 512 } 513 514 private IoUtil() { 515 } 517 } 518 | Popular Tags |