KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > util > FileManager


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.util;
6
7 import java.io.ByteArrayOutputStream JavaDoc;
8 import java.io.File JavaDoc;
9 import java.io.FileNotFoundException JavaDoc;
10 import java.io.FileOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13 import java.io.OutputStream JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17 import org.apache.commons.logging.Log;
18
19 import xdoclet.XDocletMessages;
20
21 /**
22  * A utility class for handling common filing operations. It also caches loaded files.
23  *
24  * @author Ara Abrahamian (ara_e@email.com)
25  * @author Hani Suleiman (hani@formicary.net)
26  * @created Aug 5, 2001
27  * @version $Revision: 1.14 $
28  * @todo Deal with Translator.getString()'s exception better (throw it so the build stops?) in
29  * getFileContent(String)
30  */

31 public final class FileManager
32 {
33
34     private final static int BUFFER_SIZE = 10240;
35
36     private final static Map JavaDoc urlCache = new HashMap JavaDoc();
37
38     /**
39      * Gets the URLContent attribute of the FileManager class
40      *
41      * @param url Describe what the parameter does
42      * @return The URLContent value
43      */

44     public static synchronized String JavaDoc getURLContent(URL JavaDoc url)
45     {
46         Log log = LogUtil.getLog(FileManager.class, "getURLContent");
47
48         if (url == null) {
49             throw new IllegalArgumentException JavaDoc("url shouldn't be null!");
50         }
51
52         String JavaDoc content = (String JavaDoc) urlCache.get(url);
53
54         if (content != null) {
55             return content;
56         }
57
58         try {
59             InputStream JavaDoc is = null;
60
61             if ("file".equals(url.getProtocol())) {
62                 is = new java.io.FileInputStream JavaDoc(url.getFile());
63             }
64             else {
65                 is = url.openStream();
66             }
67
68             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(is.available());
69
70             pump(is, baos);
71
72             content = new String JavaDoc(baos.toByteArray());
73             urlCache.put(url, content);
74
75             return content;
76         }
77         catch (FileNotFoundException JavaDoc e) {
78             return null;
79         }
80         catch (Exception JavaDoc e) {
81             e.printStackTrace();
82             log.error(Translator.getString(XDocletMessages.class, XDocletUtilMessages.EXCEPTION_READING_MERGE_FILE, new String JavaDoc[]{e.toString()}));
83             return null;
84         }
85     }
86
87     /**
88      * Describe what the method does
89      *
90      * @param url Describe what the parameter does
91      * @param destination Describe what the parameter does
92      * @exception IOException Describe the exception
93      */

94     public static synchronized void writeURLContent(URL JavaDoc url, File JavaDoc destination) throws IOException JavaDoc
95     {
96         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(destination);
97
98         pump(url.openStream(), fos);
99         fos.flush();
100         fos.close();
101     }
102
103     /**
104      * Describe what the method does
105      *
106      * @param is Describe what the parameter does
107      * @param os Describe what the parameter does
108      * @exception IOException Describe the exception
109      */

110     private static void pump(InputStream JavaDoc is, OutputStream JavaDoc os) throws IOException JavaDoc
111     {
112         byte[] buffer = new byte[BUFFER_SIZE];
113         int lengthRead;
114
115         while ((lengthRead = is.read(buffer)) >= 0) {
116             os.write(buffer, 0, lengthRead);
117         }
118     }
119 }
120
Popular Tags