first cut of merge of STABLE-pre1_0 into HEAD. I won't even guarantee that it
[mir.git] / source / mir / misc / FileUtil.java
1
2 /*
3  * put your module comment here
4  */
5
6
7 package mir.misc;
8
9 import  java.lang.*;
10 import  java.util.*;
11 import  java.io.*;
12 import  java.net.*;
13 import  freemarker.template.*;
14 import  mir.entity.*;
15 import  mir.storage.*;
16 import javax.servlet.http.*;
17
18
19 /**
20  * Hilfsklasse zum Mergen von Template und Daten
21  */
22 public final class FileUtil {
23
24     private static boolean fileNameMapLoaded = false;
25     private static FileNameMap fileNameMap;
26     private static String producerStorageRoot;
27
28     //
29     // Initialisierung
30
31     static {
32       System.setProperty("content.types.user.table", MirConfig.getProp("Home")+"content-types.properties");
33       fileNameMap = sun.net.www.MimeTable.loadTable();
34       producerStorageRoot = MirConfig.getProp("Producer.StorageRoot");
35     }
36
37   /**
38    * Privater Konstruktor, um versehentliche Instantiierung zu verhindern
39    */
40   private FileUtil () {
41   }
42         
43   public static boolean write(String filename, byte[] in)
44     throws IOException {
45
46                 boolean retVal = false;
47
48                 if (in!=null) {
49                         try {
50                 File f = null;
51                 f = new File(filename);
52                                 File dir = new File(f.getParent());
53                                 dir.mkdirs();
54
55                                 FileOutputStream outStream;
56                                 outStream = new FileOutputStream(f);
57                                 outStream.write(in);
58                                 outStream.close();
59                                 retVal = true;
60                         } catch(IOException e) {
61                 throw new IOException(e.toString());
62             }
63         }
64                 return retVal;
65         }
66
67   public static boolean read(String filename, byte out[])
68     throws IOException {
69
70         File f = null;
71         f = new File(filename);
72
73                 if (f.exists()) {
74                         try {
75                 if (out.length != f.length())
76                     return false;
77                                 FileInputStream inStream;
78                                 inStream = new FileInputStream(f);
79                                 inStream.read(out);
80                                 inStream.close();
81                         } catch(IOException e) {
82                 throw new IOException(e.toString());
83             }
84         } else {
85             return false;
86         }
87                 return true;
88         }
89     
90     public static long getSize(String filename) {
91         File f = null;
92         f = new File(filename);
93         long l=0;
94
95                 if (f.exists()) {
96                 return f.length();
97         } else {
98             return -1;
99         }
100     }
101
102     private static FileNameMap getFileNameMap() {
103         if ((fileNameMap == null) && !fileNameMapLoaded) {
104             fileNameMap = sun.net.www.MimeTable.loadTable();
105             fileNameMapLoaded = true;
106         }
107
108         return new FileNameMap() {
109             private FileNameMap map = fileNameMap;
110             public String getContentTypeFor(String fileName) {
111                 return map.getContentTypeFor(fileName);
112             }
113         };
114     }
115
116     public static void setFileNameMap(FileNameMap map) {
117         fileNameMap = map;
118     }
119
120     public static String guessContentTypeFromName(String fname) {
121         String contentType = null;
122                     
123         contentType = getFileNameMap().getContentTypeFor(fname);
124                                     
125         return contentType;
126     }
127
128
129  
130   
131 }