1 22 package org.jboss.util.file; 23 24 import java.io.BufferedInputStream ; 25 import java.io.BufferedOutputStream ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.OutputStream ; 32 import java.io.UnsupportedEncodingException ; 33 import java.net.URL ; 34 35 import org.jboss.logging.Logger; 36 import org.jboss.util.stream.Streams; 37 38 46 public final class Files 47 { 48 49 private static final Logger log = Logger.getLogger(Files.class); 50 51 52 private static final char[] hexDigits = new char[] 53 { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 54 55 56 public static final int DEFAULT_BUFFER_SIZE = 8192; 57 58 63 public static boolean delete(final File dir) 64 { 65 boolean success = true; 66 67 File files[] = dir.listFiles(); 68 if (files != null) 69 { 70 for (int i = 0; i < files.length; i++) 71 { 72 File f = files[i]; 73 if( f.isDirectory() == true ) 74 { 75 if( delete(f) == false ) 77 { 78 success = false; 79 log.debug("Failed to delete dir: "+f.getAbsolutePath()); 80 } 81 } 82 else if( f.delete() == false ) 84 { 85 success = false; 86 log.debug("Failed to delete file: "+f.getAbsolutePath()); 87 } 88 } 89 } 90 91 if( dir.delete() == false ) 93 { 94 success = false; 95 log.debug("Failed to delete dir: "+dir.getAbsolutePath()); 96 } 97 98 return success; 99 } 100 101 107 public static boolean delete(final String dirname) 108 { 109 return delete(new File (dirname)); 110 } 111 112 119 public static boolean deleteContaining(final String filename) 120 { 121 File file = new File (filename); 122 File containingDir = file.getParentFile(); 123 return delete(containingDir); 124 } 125 126 135 public static void copy(final File source, 136 final File target, 137 final byte buff[]) 138 throws IOException 139 { 140 BufferedInputStream in = new BufferedInputStream (new FileInputStream (source)); 141 BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (target)); 142 143 int read; 144 145 try 146 { 147 while ((read = in.read(buff)) != -1) 148 { 149 out.write(buff, 0, read); 150 } 151 } 152 finally 153 { 154 Streams.flush(out); 155 Streams.close(in); 156 Streams.close(out); 157 } 158 } 159 160 169 public static void copy(final File source, 170 final File target, 171 final int size) 172 throws IOException 173 { 174 copy(source, target, new byte[size]); 175 } 176 177 185 public static void copy(final File source, final File target) 186 throws IOException 187 { 188 copy(source, target, DEFAULT_BUFFER_SIZE); 189 } 190 191 198 public static void copy(URL src, File dest) throws IOException 199 { 200 log.debug("Copying " + src + " -> " + dest); 201 202 File dir = dest.getParentFile(); 204 if (!dir.exists()) 205 { 206 if (!dir.mkdirs()) 207 { 208 throw new IOException ("mkdirs failed for: " + dir.getAbsolutePath()); 209 } 210 } 211 if (dest.exists()) 213 { 214 if (!Files.delete(dest)) 215 { 216 throw new IOException ("delete of previous content failed for: " + dest.getAbsolutePath()); 217 } 218 } 219 InputStream in = new BufferedInputStream (src.openStream()); 222 OutputStream out = new BufferedOutputStream (new FileOutputStream (dest)); 223 Streams.copy(in, out); 224 out.flush(); 225 out.close(); 226 in.close(); 227 } 228 229 241 public static String encodeFileName(String name) 242 { 243 return encodeFileName(name, '@'); 244 } 245 246 261 public static String decodeFileName(String name) 262 { 263 return decodeFileName(name, '@'); 264 } 265 266 273 public static String encodeFileName(String name, char escape) 274 { 275 StringBuffer rc = new StringBuffer (); 276 for (int i = 0; i < name.length(); i++ ) 277 { 278 switch (name.charAt(i)) 279 { 280 case 'a': case 'A': case 'b': case 'B': case 'c': case 'C': 282 case 'd': case 'D': case 'e': case 'E': case 'f': case 'F': 283 case 'g': case 'G': case 'h': case 'H': case 'i': case 'I': 284 case 'j': case 'J': case 'k': case 'K': case 'l': case 'L': 285 case 'm': case 'M': case 'n': case 'N': case 'o': case 'O': 286 case 'p': case 'P': case 'q': case 'Q': case 'r': case 'R': 287 case 's': case 'S': case 't': case 'T': case 'u': case 'U': 288 case 'v': case 'V': case 'w': case 'W': case 'x': case 'X': 289 case 'y': case 'Y': case 'z': case 'Z': 290 case '1': case '2': case '3': case '4': case '5': 291 case '6': case '7': case '8': case '9': case '0': 292 case '-': case '_': case '.': 293 rc.append(name.charAt(i)); 294 break; 295 296 default: 298 299 try 305 { 306 byte data[] = ("" + name.charAt(i)).getBytes("UTF8"); 307 for (int j = 0; j < data.length; j++) 308 { 309 rc.append(escape); 310 rc.append(hexDigits[ (data[j] >> 4) & 0xF ]); rc.append(hexDigits[ (data[j] ) & 0xF ]); } 313 } 314 catch (UnsupportedEncodingException wonthappen) 315 { 316 } 318 } 319 } 320 return rc.toString(); 321 } 322 323 330 public static String decodeFileName(String name, char escape) 331 { 332 if (name == null) 333 { 334 return null; 335 } 336 StringBuffer sbuf = new StringBuffer (name.length()); 337 338 for (int i = 0; i < name.length(); i++) 339 { 340 char c = name.charAt(i); 341 if (c == escape) 342 { 343 char h1 = name.charAt(++i); 344 char h2 = name.charAt(++i); 345 346 int d1 = (h1 >= 'a') ? (10 + h1 - 'a') 348 : ((h1 >= 'A') ? (10 + h1 - 'A') 349 : (h1 - '0')); 350 351 int d2 = (h2 >= 'a') ? (10 + h2 - 'a') 352 : ((h2 >= 'A') ? (10 + h2 - 'A') 353 : (h2 - '0')); 354 355 byte[] bytes = new byte[] { (byte)(d1 * 16 + d2) }; 359 360 try 361 { 362 String s = new String (bytes, "UTF8"); 363 sbuf.append(s); 364 } 365 catch (UnsupportedEncodingException wonthappen) 366 { 367 } 369 } 370 else 371 { 372 sbuf.append(c); 373 } 374 } 375 return sbuf.toString(); 376 } 377 378 385 public static String findRelativePath(String base, String path) 386 throws IOException 387 { 388 String a = new File (base).getCanonicalFile().toURI().getPath(); 389 String b = new File (path).getCanonicalFile().toURI().getPath(); 390 String [] basePaths = a.split("/"); 391 String [] otherPaths = b.split("/"); 392 int n = 0; 393 for(; n < basePaths.length && n < otherPaths.length; n ++) 394 { 395 if( basePaths[n].equals(otherPaths[n]) == false ) 396 break; 397 } 398 System.out.println("Common length: "+n); 399 StringBuffer tmp = new StringBuffer ("../"); 400 for(int m = n; m < basePaths.length - 1; m ++) 401 tmp.append("../"); 402 for(int m = n; m < otherPaths.length; m ++) 403 { 404 tmp.append(otherPaths[m]); 405 tmp.append("/"); 406 } 407 408 return tmp.toString(); 409 } 410 } 411 | Popular Tags |