KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > files > FileUtils


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: FileUtils.java,v 1.2 2005/04/28 16:53:00 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas_lib.files;
26
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.nio.channels.FileChannel JavaDoc;
33 import java.util.Enumeration JavaDoc;
34 import java.util.jar.JarEntry JavaDoc;
35 import java.util.jar.JarFile JavaDoc;
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 /**
43  * This class manages operation done many times by JOnAS on files, like copying
44  * them.
45  * @author Florent Benoit
46  */

47 public class FileUtils {
48
49
50     /**
51      * Size of the buffer.
52      */

53     private static final int BUFFER_SIZE = 2048;
54
55
56     /**
57      * Logger
58      */

59     private static Logger logger = Log.getLogger(FileUtils.class.getName());
60
61     /**
62      * Utility class, no public constructor
63      */

64     private FileUtils() {
65
66     }
67
68     /**
69      * Unpack the source archive in a given directory and returns directory
70      * directory created.
71      * @param packedJar source JarFile to be unpacked
72      * @param dest the destination folder
73      * @throws FileUtilsException When unpack fails
74      */

75     public static void unpack(JarFile JavaDoc packedJar, File JavaDoc dest) throws FileUtilsException {
76
77         JarEntry JavaDoc entry = null;
78
79         // get entries of the jar file
80
Enumeration JavaDoc entries = packedJar.entries();
81         while (entries.hasMoreElements()) {
82             entry = (JarEntry JavaDoc) entries.nextElement();
83
84             // File entry
85
File JavaDoc entryFile = new File JavaDoc(dest, entry.getName());
86
87             // Create directory
88
if (entry.isDirectory()) {
89                 if (!entryFile.exists()) {
90                     // create parent directories (with mkdirs)
91
if (!entryFile.mkdirs()) {
92                         String JavaDoc err = "Can not create directory " + entryFile + ", Check the write access.";
93                         throw new FileUtilsException(err);
94                     }
95                 }
96                 continue;
97             }
98
99             // If it's a file, we must extract the file
100
// Ensure that the directory exists.
101
entryFile.getParentFile().mkdirs();
102
103             InputStream JavaDoc is = null;
104             // get the input stream
105
try {
106                 is = packedJar.getInputStream(entry);
107                 // Dump to the file
108
dump(is, entryFile);
109             } catch (IOException JavaDoc 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 JavaDoc 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     /**
126      * Write the given input stream in the given file.
127      * @param in the inputStream to copy.
128      * @param entryFile the file where the inputStream must be dumped.
129      * @throws FileUtilsException if the dump failed.
130      */

131     private static void dump(InputStream JavaDoc in, File JavaDoc entryFile) throws FileUtilsException {
132
133         try {
134             //File output
135
FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(entryFile);
136             int n = 0;
137             try {
138                 //buffer
139
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 JavaDoc e) {
149             String JavaDoc err = "Error while unpacking entry " + entryFile + " : ";
150             throw new FileUtilsException(err, e);
151         }
152     }
153
154     /**
155      * Copy a file
156      * @param src source file
157      * @param dest dest file
158      * @throws FileUtilsException if the copy of the file failed
159      */

160     public static void copyFile(String JavaDoc src, String JavaDoc dest) throws FileUtilsException {
161         copyFile(new File JavaDoc(src), new File JavaDoc(dest));
162     }
163
164     /**
165      * Copy a file
166      * @param src source file
167      * @param dest dest file
168      * @throws FileUtilsException if the copy of the file failed
169      */

170     public static void copyFile(File JavaDoc src, File JavaDoc 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 JavaDoc(dest, src.getName());
177         }
178
179         FileInputStream JavaDoc fIn = null;
180         FileOutputStream JavaDoc fOut = null;
181         FileChannel JavaDoc fcIn = null;
182         FileChannel JavaDoc fcOut = null;
183         try {
184
185             // InputStream
186
fIn = new FileInputStream JavaDoc(src);
187             fOut = new FileOutputStream JavaDoc(dest);
188
189             // nio channel
190
FileChannel JavaDoc sourceFC = fIn.getChannel();
191             FileChannel JavaDoc targetFC = fOut.getChannel();
192
193             targetFC.transferFrom(sourceFC, 0, sourceFC.size());
194         } catch (Exception JavaDoc 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 JavaDoc 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