KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > FileUtils


1 /*
2  * $Id: FileUtils.java 3937 2006-11-20 16:04:25Z lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.util;
12
13 import java.io.BufferedOutputStream JavaDoc;
14 import java.io.BufferedWriter JavaDoc;
15 import java.io.File JavaDoc;
16 import java.io.FileOutputStream JavaDoc;
17 import java.io.FileWriter JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.net.URLDecoder JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.zip.ZipEntry JavaDoc;
26 import java.util.zip.ZipFile JavaDoc;
27
28 import org.mule.MuleManager;
29 import org.mule.MuleRuntimeException;
30 import org.mule.config.i18n.Message;
31
32 /**
33  * <code>FileUtils</code> contains useful methods for dealing with files &
34  * directories.
35  */

36 // @ThreadSafe
37
public class FileUtils extends org.apache.commons.io.FileUtils
38 {
39     public static synchronized void copyStreamToFile(InputStream JavaDoc input, File JavaDoc destination) throws IOException JavaDoc {
40         if (destination.exists() && !destination.canWrite()) {
41             throw new IOException JavaDoc("Destination file does not exist or is not writeable");
42         }
43
44         try {
45             FileOutputStream JavaDoc output = new FileOutputStream JavaDoc(destination);
46             try {
47                 IOUtils.copy(input, output);
48             } finally {
49                 IOUtils.closeQuietly(output);
50             }
51         } finally {
52             IOUtils.closeQuietly(input);
53         }
54     }
55
56     // TODO Document me!
57
public static File JavaDoc createFile(String JavaDoc filename) throws IOException JavaDoc
58     {
59         File JavaDoc file = FileUtils.newFile(filename);
60         if (!file.canWrite())
61         {
62             String JavaDoc dirName = file.getPath();
63             int i = dirName.lastIndexOf(File.separator);
64             if (i > -1)
65             {
66                 dirName = dirName.substring(0, i);
67                 File JavaDoc dir = FileUtils.newFile(dirName);
68                 dir.mkdirs();
69             }
70             file.createNewFile();
71         }
72         return file;
73     }
74
75     // TODO Document me!
76
public static String JavaDoc prepareWinFilename(String JavaDoc filename)
77     {
78         filename = filename.replaceAll("<", "(");
79         filename = filename.replaceAll(">", ")");
80         filename = filename.replaceAll("[/\\*?|:;]", "-");
81         return filename;
82     }
83
84     // TODO Document me!
85
public static File JavaDoc openDirectory(String JavaDoc directory) throws IOException JavaDoc
86     {
87         File JavaDoc dir = FileUtils.newFile(directory);
88         if (!dir.exists())
89         {
90             dir.mkdirs();
91         }
92         if (!dir.isDirectory() || !dir.canRead())
93         {
94             throw new IOException JavaDoc("Path: " + directory + " exists but isn't a directory");
95         }
96         return dir;
97     }
98
99     /**
100      * Reads the incoming String into a file at at the given destination.
101      *
102      * @param filename name and path of the file to create
103      * @param data the contents of the file
104      * @return the new file.
105      * @throws IOException If the creating or writing to the file stream fails
106      */

107     public static File JavaDoc stringToFile(String JavaDoc filename, String JavaDoc data) throws IOException JavaDoc
108     {
109         return stringToFile(filename, data, false);
110     }
111
112     // TODO Document me!
113
public static synchronized File JavaDoc stringToFile(String JavaDoc filename, String JavaDoc data, boolean append)
114         throws IOException JavaDoc
115     {
116         return stringToFile(filename, data, append, false);
117     }
118
119     // TODO Document me!
120
public static synchronized File JavaDoc stringToFile(String JavaDoc filename, String JavaDoc data, boolean append, boolean newLine)
121         throws IOException JavaDoc
122     {
123         File JavaDoc f = createFile(filename);
124         BufferedWriter JavaDoc writer = null;
125         try
126         {
127             writer = new BufferedWriter JavaDoc(new FileWriter JavaDoc(f, append));
128             writer.write(data);
129             if (newLine)
130             {
131                 writer.newLine();
132             }
133         }
134         finally
135         {
136             if (writer != null)
137             {
138                 writer.close();
139             }
140         }
141         return f;
142     }
143
144     // TODO Document me!
145
public static String JavaDoc getResourcePath(String JavaDoc resourceName, Class JavaDoc callingClass) throws IOException JavaDoc
146     {
147         return getResourcePath(resourceName, callingClass, MuleManager.getConfiguration().getEncoding());
148     }
149
150     // TODO Document me!
151
public static String JavaDoc getResourcePath(String JavaDoc resourceName, Class JavaDoc callingClass, String JavaDoc encoding)
152         throws IOException JavaDoc
153     {
154         if (resourceName == null)
155         {
156             // no name
157
return null;
158         }
159
160         URL JavaDoc url = IOUtils.getResourceAsUrl(resourceName, callingClass);
161         if (url == null)
162         {
163             // not found
164
return null;
165         }
166
167         String JavaDoc resource = URLDecoder.decode(url.toExternalForm(), encoding);
168         if (resource != null)
169         {
170             if (resource.startsWith("file:/"))
171             {
172                 resource = resource.substring(6);
173             }
174             if (!resource.startsWith(File.separator))
175             {
176                 resource = File.separator + resource;
177             }
178         }
179
180         return resource;
181     }
182
183     // TODO Document me!
184
public static boolean deleteTree(File JavaDoc dir)
185     {
186         if (dir == null || !dir.exists())
187         {
188             return true;
189         }
190         File JavaDoc[] files = dir.listFiles();
191         if (files != null)
192         {
193             for (int i = 0; i < files.length; i++)
194             {
195                 if (files[i].isDirectory())
196                 {
197                     if (!deleteTree(files[i]))
198                     {
199                         return false;
200                     }
201                 }
202                 else
203                 {
204                     if (!files[i].delete())
205                     {
206                         return false;
207                     }
208                 }
209             }
210         }
211         return dir.delete();
212     }
213
214     /**
215      * Unzip the specified archive to the given directory
216      */

217     public static void unzip(File JavaDoc archive, File JavaDoc directory) throws IOException JavaDoc
218     {
219         ZipFile JavaDoc zip = null;
220
221         if (directory.exists())
222         {
223             if (!directory.isDirectory())
224             {
225                 throw new IOException JavaDoc("Directory is not a directory: " + directory);
226             }
227         }
228         else
229         {
230             if (!directory.mkdirs())
231             {
232                 throw new IOException JavaDoc("Could not create directory: " + directory);
233             }
234         }
235         try
236         {
237             zip = new ZipFile JavaDoc(archive);
238             for (Enumeration JavaDoc entries = zip.entries(); entries.hasMoreElements();)
239             {
240                 ZipEntry JavaDoc entry = (ZipEntry JavaDoc)entries.nextElement();
241                 File JavaDoc f = new File JavaDoc(directory, entry.getName());
242                 if (entry.isDirectory())
243                 {
244                     if (!f.mkdirs())
245                     {
246                         throw new IOException JavaDoc("Could not create directory: " + f);
247                     }
248                 }
249                 else
250                 {
251                     InputStream JavaDoc is = zip.getInputStream(entry);
252                     OutputStream JavaDoc os = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(f));
253                     IOUtils.copy(is, os);
254                     IOUtils.closeQuietly(is);
255                     IOUtils.closeQuietly(os);
256                 }
257             }
258         }
259         finally
260         {
261             if (zip != null)
262             {
263                 zip.close();
264             }
265         }
266     }
267
268     /**
269      * Workaround for JDK bug <a HREF="http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4117557">
270      * 4117557</a>. More in-context information at
271      * <a HREF="http://mule.mulesource.org/jira/browse/MULE-1112">MULE-1112</a>
272      * <p/>
273      * Factory methods correspond to constructors of the <code>java.io.File class</code>.
274      * No physical file created in this method.
275      *
276      * @see File
277      *
278      */

279     public static File JavaDoc newFile(String JavaDoc pathName)
280     {
281         try
282         {
283             return new File JavaDoc(pathName).getCanonicalFile();
284         }
285         catch (IOException JavaDoc e)
286         {
287             throw new MuleRuntimeException(
288                     Message.createStaticMessage("Unable to create a canonical file for " + pathName), e);
289         }
290     }
291
292     /**
293      * Workaround for JDK bug <a HREF="http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4117557">
294      * 4117557</a>. More in-context information at
295      * <a HREF="http://mule.mulesource.org/jira/browse/MULE-1112">MULE-1112</a>
296      * <p/>
297      * Factory methods correspond to constructors of the <code>java.io.File class</code>.
298      * No physical file created in this method.
299      *
300      * @see File
301      *
302      */

303     public static File JavaDoc newFile(URI JavaDoc uri)
304     {
305         try
306         {
307             return new File JavaDoc(uri).getCanonicalFile();
308         }
309         catch (IOException JavaDoc e)
310         {
311             throw new MuleRuntimeException(
312                     Message.createStaticMessage("Unable to create a canonical file for " + uri), e);
313         }
314     }
315
316     /**
317      * Workaround for JDK bug <a HREF="http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4117557">
318      * 4117557</a>. More in-context information at
319      * <a HREF="http://mule.mulesource.org/jira/browse/MULE-1112">MULE-1112</a>
320      * <p/>
321      * Factory methods correspond to constructors of the <code>java.io.File class</code>.
322      * No physical file created in this method.
323      *
324      * @see File
325      *
326      */

327     public static File JavaDoc newFile(File JavaDoc parent, String JavaDoc child)
328     {
329         try
330         {
331             return new File JavaDoc(parent, child).getCanonicalFile();
332         }
333         catch (IOException JavaDoc e)
334         {
335             throw new MuleRuntimeException(
336                     Message.createStaticMessage("Unable to create a canonical file for parent: " +
337                             parent + " and child: " + child), e);
338         }
339     }
340
341     /**
342      * Workaround for JDK bug <a HREF="http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4117557">
343      * 4117557</a>. More in-context information at
344      * <a HREF="http://mule.mulesource.org/jira/browse/MULE-1112">MULE-1112</a>
345      * <p/>
346      * Factory methods correspond to constructors of the <code>java.io.File class</code>.
347      * No physical file created in this method.
348      *
349      * @see File
350      *
351      */

352     public static File JavaDoc newFile(String JavaDoc parent, String JavaDoc child)
353     {
354         try
355         {
356             return new File JavaDoc(parent, child).getCanonicalFile();
357         }
358         catch (IOException JavaDoc e)
359         {
360             throw new MuleRuntimeException(
361                     Message.createStaticMessage("Unable to create a canonical file for parent: " +
362                             parent + " and child: " + child), e);
363         }
364     }
365 }
366
Popular Tags