KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > tools > jbicommon > util > FileUtil


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005-2006 Fossil E-Commerce, http://www.fossilec.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: FileUtil.java 508 2006-06-02 08:05:25Z ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22
23 package org.objectweb.petals.tools.jbicommon.util;
24
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31
32 /**
33  * This util class is used to manage file
34  *
35  * @version $Rev: 508 $ $Date: 2006-06-02 08:05:25Z $
36  * @since Petals 1.0
37  * @author <a HREF="mailto:rmarins@fossilec.com">Rafael Marins</a>
38  * @author ddesjardins - eBMWebsourcing
39  */

40 public final class FileUtil {
41
42     private static final Logger JavaDoc LOGGER = Logger.getLogger(XMLUtil.class
43             .getName());
44
45     /**
46      * Buffer size used when copying the content of an input stream to an output
47      * stream.
48      */

49     private static final int DEFAULT_BUFFER_SIZE = 4096; // NOPMD by gblondelle
50

51     /**
52      * Do not construct.
53      */

54     private FileUtil() {
55         super();
56     }
57
58     /**
59      * Copy an in stream to an out stream
60      *
61      * @param in
62      * @param out
63      * @throws IOException
64      */

65     public static void copyInputStream(final InputStream JavaDoc in,
66             final OutputStream JavaDoc out) throws IOException JavaDoc {
67         byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
68         int len;
69         // while ((len = in.read(buffer)) >= 0) {
70
// out.write(buffer, 0, len);
71
// }
72
try {
73             while (true) {
74                 len = in.read(buffer);
75                 if (len == -1) {
76                     break;
77                 }
78                 out.write(buffer);
79             }
80         } finally {
81             if (in != null) {
82                 in.close();
83             }
84             if (out != null) {
85                 out.close();
86             }
87             in.close();
88             out.close();
89         }
90     }
91
92     /**
93      * @param dir
94      * the directory to delete
95      * @return true if the directory is completely removed
96      */

97     public static boolean removeDirectory(final File JavaDoc dir) {
98
99         boolean completeRemoval = true;
100
101         if (!dir.isDirectory() && !dir.exists()) {
102             return false; // NOPMD by gblondelle
103
}
104
105         // first remove directory contents
106
for (File JavaDoc aFile : dir.listFiles()) {
107
108             if (aFile.isDirectory()) {
109                 completeRemoval = removeDirectory(aFile);
110             } else {
111                 completeRemoval = aFile.delete();
112             }
113         }
114
115         if (completeRemoval) {
116             // delete main directory (now empty)
117
completeRemoval = dir.delete();
118         } else {
119             // do nothing simply log
120
LOGGER.log(Level.WARNING,
121                     "Directory not deleted because not empty :"
122                             + dir.getAbsolutePath());
123         }
124
125         return completeRemoval;
126     }
127
128 }
129
Popular Tags