1 25 package org.objectweb.jonas_lib.files; 26 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.nio.channels.FileChannel ; 33 import java.util.Enumeration ; 34 import java.util.jar.JarEntry ; 35 import java.util.jar.JarFile ; 36 37 import org.objectweb.jonas.common.Log; 38 39 import org.objectweb.util.monolog.api.BasicLevel; 40 import org.objectweb.util.monolog.api.Logger; 41 42 47 public class FileUtils { 48 49 50 53 private static final int BUFFER_SIZE = 2048; 54 55 56 59 private static Logger logger = Log.getLogger(FileUtils.class.getName()); 60 61 64 private FileUtils() { 65 66 } 67 68 75 public static void unpack(JarFile packedJar, File dest) throws FileUtilsException { 76 77 JarEntry entry = null; 78 79 Enumeration entries = packedJar.entries(); 81 while (entries.hasMoreElements()) { 82 entry = (JarEntry ) entries.nextElement(); 83 84 File entryFile = new File (dest, entry.getName()); 86 87 if (entry.isDirectory()) { 89 if (!entryFile.exists()) { 90 if (!entryFile.mkdirs()) { 92 String err = "Can not create directory " + entryFile + ", Check the write access."; 93 throw new FileUtilsException(err); 94 } 95 } 96 continue; 97 } 98 99 entryFile.getParentFile().mkdirs(); 102 103 InputStream is = null; 104 try { 106 is = packedJar.getInputStream(entry); 107 dump(is, entryFile); 109 } catch (IOException ioe) { 110 throw new FileUtilsException("Cannot get inputstream of entry '" + entry + "' of file '" + packedJar 111 + "'."); 112 } finally { 113 try { 114 is.close(); 115 } catch (IOException ioe) { 116 if (logger.isLoggable(BasicLevel.DEBUG)) { 117 logger.log(BasicLevel.DEBUG, "Cannot close input stream", ioe); 118 } 119 } 120 } 121 122 } 123 } 124 125 131 private static void dump(InputStream in, File entryFile) throws FileUtilsException { 132 133 try { 134 FileOutputStream out = new FileOutputStream (entryFile); 136 int n = 0; 137 try { 138 byte[] buffer = new byte[BUFFER_SIZE]; 140 n = in.read(buffer); 141 while (n > 0) { 142 out.write(buffer, 0, n); 143 n = in.read(buffer); 144 } 145 } finally { 146 out.close(); 147 } 148 } catch (IOException e) { 149 String err = "Error while unpacking entry " + entryFile + " : "; 150 throw new FileUtilsException(err, e); 151 } 152 } 153 154 160 public static void copyFile(String src, String dest) throws FileUtilsException { 161 copyFile(new File (src), new File (dest)); 162 } 163 164 170 public static void copyFile(File src, File dest) throws FileUtilsException { 171 172 if (dest.isDirectory()) { 173 if (logger.isLoggable(BasicLevel.DEBUG)) { 174 logger.log(BasicLevel.DEBUG, "Copy a file to a directory, append source filename to directory."); 175 } 176 dest = new File (dest, src.getName()); 177 } 178 179 FileInputStream fIn = null; 180 FileOutputStream fOut = null; 181 FileChannel fcIn = null; 182 FileChannel fcOut = null; 183 try { 184 185 fIn = new FileInputStream (src); 187 fOut = new FileOutputStream (dest); 188 189 FileChannel sourceFC = fIn.getChannel(); 191 FileChannel targetFC = fOut.getChannel(); 192 193 targetFC.transferFrom(sourceFC, 0, sourceFC.size()); 194 } catch (Exception e) { 195 throw new FileUtilsException("Error during copy file : " + src + " -> " + dest, e); 196 } finally { 197 try { 198 fOut.close(); 199 fIn.close(); 200 fcOut.close(); 201 fcIn.close(); 202 } catch (Exception e) { 203 if (logger.isLoggable(BasicLevel.DEBUG)) { 204 logger.log(BasicLevel.DEBUG, "Cannot close some i/o which are open.", e); 205 } 206 } 207 208 } 209 } 210 211 } 212 | Popular Tags |