1 6 package fr.jayasoft.ivy.util; 7 8 import java.io.BufferedReader ; 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.FileOutputStream ; 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import java.io.OutputStream ; 15 import java.net.URL ; 16 import java.util.ArrayList ; 17 import java.util.Collection ; 18 import java.util.Collections ; 19 import java.util.List ; 20 21 import fr.jayasoft.ivy.url.URLHandlerRegistry; 22 23 27 public class FileUtil { 28 private static final int BUFFER_SIZE = 8 * 1024; 31 public static void copy(File src, File dest, CopyProgressListener l) throws IOException { 32 copy(src, dest, l, false); 33 } 34 public static void copy(File src, File dest, CopyProgressListener l, boolean overwrite) throws IOException { 35 if (dest.exists()) { 36 if (!dest.isFile()) { 37 throw new IOException ("impossible to copy: destination is not a file: "+dest); 38 } 39 if (overwrite) { 40 if (!dest.canWrite()) { 41 dest.delete(); 42 } } else { 44 Message.verbose(dest+" already exists, nothing done"); 45 } 46 } 47 copy(new FileInputStream (src), dest, l); 48 long srcLen = src.length(); 49 long destLen = dest.length(); 50 if (srcLen != destLen) { 51 dest.delete(); 52 throw new IOException ("size of source file " + src.toString() + "(" 53 + srcLen + ") differs from size of dest file " + dest.toString() 54 + "(" + destLen + ") - please retry"); 55 } 56 dest.setLastModified(src.lastModified()); 57 } 58 59 public static void copy(URL src, File dest, CopyProgressListener l) throws IOException { 60 URLHandlerRegistry.getDefault().download(src, dest, l); 61 } 62 63 public static void copy(InputStream src, File dest, CopyProgressListener l) throws IOException { 64 if (dest.getParentFile() != null) { 65 dest.getParentFile().mkdirs(); 66 } 67 copy(src, new FileOutputStream (dest), l); 68 } 69 70 public static void copy(InputStream src, OutputStream dest, CopyProgressListener l) throws IOException { 71 try { 72 CopyProgressEvent evt = null; 73 if (l != null) { 74 evt = new CopyProgressEvent(); 75 } 76 byte buffer[]=new byte[BUFFER_SIZE]; 77 int c; 78 long total = 0; 79 80 if (l != null) { 81 l.start(evt); 82 } 83 while( (c = src.read(buffer)) != -1 ) { 84 if (Thread.currentThread().isInterrupted()) { 85 throw new IOException ("transfer interrupted"); 86 } 87 dest.write(buffer, 0, c); 88 total += c; 89 if (l != null) { 90 l.progress(evt.update(buffer, c, total)); 91 } 92 } 93 if (l != null) { 94 l.end(evt.update(buffer, 0, total)); 95 } 96 } finally { 97 try { 98 src.close(); 99 } catch (IOException ex) { 100 dest.close(); 101 throw ex; 102 } 103 dest.close(); 104 } 105 } 106 107 public static String readEntirely(BufferedReader in) throws IOException { 108 StringBuffer buf = new StringBuffer (); 109 110 String line = in.readLine(); 111 while (line != null) { 112 buf.append(line + "\n"); 113 line = in.readLine(); 114 } 115 in.close(); 116 return buf.toString(); 117 } 118 119 public static String concat(String dir, String file) { 120 return dir+"/"+file; 121 } 122 123 public static void forceDelete(File f) { 124 if (f.isDirectory()) { 125 File [] sub = f.listFiles(); 126 for (int i = 0; i < sub.length; i++) { 127 forceDelete(sub[i]); 128 } 129 } 130 f.delete(); 131 } 132 143 public static List getPathFiles(File root, File file) { 144 List ret = new ArrayList (); 145 while (file != null && !file.getAbsolutePath().equals(root.getAbsolutePath())) { 146 ret.add(file); 147 file = file.getParentFile(); 148 } 149 if (root != null) { 150 ret.add(root); 151 } 152 Collections.reverse(ret); 153 return ret; 154 } 155 161 public static Collection listAll(File dir) { 162 return listAll(dir, new ArrayList ()); 163 } 164 private static Collection listAll(File file, Collection list) { 165 if (file.exists()) { 166 list.add(file); 167 } 168 if (file.isDirectory()) { 169 File [] files = file.listFiles(); 170 for (int i = 0; i < files.length; i++) { 171 listAll(files[i], list); 172 } 173 } 174 return list; 175 } 176 177 } 178 | Popular Tags |