5a68e319f72b10043b9bb93f63cea7d50404f830
[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       System.err.println("DDD: "+MirConfig.getProp("Home")+"content-types.properties");                             
34       fileNameMap = sun.net.www.MimeTable.loadTable();
35       producerStorageRoot = MirConfig.getProp("Producer.StorageRoot");
36     }
37
38   /**
39    * Privater Konstruktor, um versehentliche Instantiierung zu verhindern
40    */
41   private FileUtil () {
42   }
43         
44   public static boolean write(String filename, byte[] in) {
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 exception) {}
61         }
62                 return retVal;
63         }
64
65   public static boolean read(String filename, byte out[]) {
66
67         File f = null;
68         f = new File(filename);
69
70                 if (f.exists()) {
71                         try {
72                 if (out.length != f.length())
73                     return false;
74                                 FileInputStream inStream;
75                                 inStream = new FileInputStream(f);
76                                 inStream.read(out);
77                                 inStream.close();
78                         } catch(IOException exception) {
79                 return false;
80             }
81         } else {
82             return false;
83         }
84                 return true;
85         }
86     
87     public static long getSize(String filename) {
88         File f = null;
89         f = new File(filename);
90         long l=0;
91
92                 if (f.exists()) {
93                 return f.length();
94         } else {
95             return -1;
96         }
97     }
98
99     private static FileNameMap getFileNameMap() {
100         if ((fileNameMap == null) && !fileNameMapLoaded) {
101             fileNameMap = sun.net.www.MimeTable.loadTable();
102             fileNameMapLoaded = true;
103         }
104
105         return new FileNameMap() {
106             private FileNameMap map = fileNameMap;
107             public String getContentTypeFor(String fileName) {
108                 return map.getContentTypeFor(fileName);
109             }
110         };
111     }
112
113     public static void setFileNameMap(FileNameMap map) {
114         fileNameMap = map;
115     }
116
117     public static String guessContentTypeFromName(String fname) {
118         String contentType = null;
119                     
120         System.err.println("NAME: "+fname);                             
121         contentType = getFileNameMap().getContentTypeFor(fname);
122         System.err.println("TYPE: "+contentType);                             
123         return contentType;
124     }
125
126
127  
128   
129 }