KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > portlet > util > FileUtils


1 package com.opensymphony.webwork.portlet.util;
2
3 import com.opensymphony.util.TextUtils;
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import java.io.*;
9 import java.util.ArrayList JavaDoc;
10 import java.util.List JavaDoc;
11
12 public class FileUtils {
13     private static Log log;
14
15     private static final int BUFFER_SIZE = 1024;
16
17     static {
18         log = LogFactory.getLog(com.opensymphony.webwork.portlet.util.FileUtils.class);
19     }
20
21     public FileUtils() {
22     }
23
24     public static void copyDirectory(File srcDir, File destDir) throws IOException {
25         copyDirectory(srcDir, destDir, false);
26     }
27
28     public static void copyDirectory(File srcDir, File destDir, boolean overwrite) throws IOException {
29         File files[] = srcDir.listFiles();
30         if (!destDir.exists())
31             destDir.mkdirs();
32         else
33             log.debug(destDir.getAbsolutePath() + " already exists");
34         for (int i = 0; i < files.length; i++) {
35             File file = files[i];
36             File dest = new File(destDir, file.getName());
37             if (file.isFile())
38                 copyFile(new FileInputStream(file), dest, overwrite);
39             else
40                 copyDirectory(file, dest, overwrite);
41         }
42
43     }
44
45     public static void copyFile(File srcFile, File destFile) throws IOException {
46         InputStream input = new FileInputStream(srcFile);
47         copyFile(input, destFile);
48     }
49
50     public static void copyFile(InputStream srcStream, File destFile) throws IOException {
51         copyFile(srcStream, destFile, false);
52     }
53
54     public static void copyFile(InputStream srcStream, File destFile, boolean overwrite) throws IOException {
55         File parentFile = destFile.getParentFile();
56         if (!parentFile.isDirectory())
57             parentFile.mkdirs();
58         if (destFile.exists()) {
59             if (overwrite) {
60                 log.debug("Overwriting file at: " + destFile.getAbsolutePath());
61                 writeStreamToFile(srcStream, destFile);
62             } else {
63                 log.warn(destFile.getAbsolutePath() + " already exists");
64             }
65         } else {
66             destFile.createNewFile();
67             writeStreamToFile(srcStream, destFile);
68         }
69     }
70
71     private static void writeStreamToFile(InputStream srcStream, File destFile) throws IOException {
72         OutputStream output;
73         Exception JavaDoc exception;
74         InputStream input = null;
75         output = null;
76         try {
77             input = new BufferedInputStream(srcStream);
78             output = new BufferedOutputStream(new FileOutputStream(destFile));
79             int ch;
80             while ((ch = input.read()) != -1) output.write(ch);
81         } catch (IOException e) {
82             log.error("Error writing stream to file: " + destFile.getAbsolutePath());
83             throw e;
84         } finally {
85             input.close();
86         }
87         output.close();
88
89     }
90
91     public static void saveTextFile(String JavaDoc stringContent, File destFile) throws IOException {
92         ensureFileAndPathExist(destFile);
93         FileWriter writer = new FileWriter(destFile);
94         writer.write(stringContent);
95         writer.close();
96     }
97
98     public static void ensureFileAndPathExist(File file) throws IOException {
99         file.getParentFile().mkdirs();
100         file.createNewFile();
101     }
102
103     public static void createZipFile(File baseDir, File zipFile) throws Exception JavaDoc {
104         FolderArchiver compressor = new FolderArchiver(baseDir, zipFile);
105         compressor.doArchive();
106     }
107
108     public static List JavaDoc readResourcesAsList(String JavaDoc resource) {
109         List JavaDoc result = new ArrayList JavaDoc();
110         try {
111             InputStream is = ClassLoaderUtils.getResourceAsStream(resource, com.opensymphony.webwork.portlet.util.FileUtils.class);
112             BufferedReader in = new BufferedReader(new InputStreamReader(is));
113             do {
114                 String JavaDoc s;
115                 if ((s = in.readLine()) == null)
116                     break;
117                 String JavaDoc niceS = TextUtils.noNull(s).trim();
118                 if (TextUtils.stringSet(niceS) && niceS.charAt(0) != '#')
119                     result.add(s);
120             } while (true);
121             is.close();
122         } catch (IOException e) {
123             log.error("IOException reading stream: " + e, e);
124         }
125         return result;
126     }
127
128     public static String JavaDoc getResourceContent(String JavaDoc resource) {
129         InputStream is = ClassLoaderUtils.getResourceAsStream(resource, com.opensymphony.webwork.portlet.util.FileUtils.class);
130         return getInputStreamTextContent(is);
131     }
132
133     public static String JavaDoc getResourceContent(HttpServletRequest JavaDoc req, String JavaDoc resource) {
134         InputStream is = req.getSession().getServletContext().getResourceAsStream(resource);
135         String JavaDoc result = getInputStreamTextContent(is);
136         if (result == null)
137             result = "";
138         return result;
139     }
140
141     public static String JavaDoc getInputStreamTextContent(InputStream is) {
142         if (is == null)
143             return null;
144         String JavaDoc result = null;
145         try {
146             ByteArrayOutputStream baos = new ByteArrayOutputStream(is.available());
147             pump(is, baos);
148             result = new String JavaDoc(baos.toByteArray());
149             is.close();
150         } catch (IOException e) {
151             log.error("IOException reading stream: " + e, e);
152         }
153         return result;
154     }
155
156     private static void pump(InputStream is, OutputStream os) throws IOException {
157         byte buffer[] = new byte[1024];
158         int lengthRead;
159         while ((lengthRead = is.read(buffer)) >= 0) os.write(buffer, 0, lengthRead);
160     }
161
162     public static boolean deleteDir(File dir) {
163         if (dir == null)
164             return false;
165         File candir;
166         try {
167             candir = dir.getCanonicalFile();
168         } catch (IOException e) {
169             return false;
170         }
171         if (!candir.equals(dir.getAbsoluteFile()))
172             return false;
173         File files[] = candir.listFiles();
174         if (files != null) {
175             for (int i = 0; i < files.length; i++) {
176                 File file = files[i];
177                 boolean deleted = !file.delete();
178                 if (deleted && file.isDirectory())
179                     deleteDir(file);
180             }
181
182         }
183         return dir.delete();
184     }
185
186 }
187
188
Popular Tags