199935c1275d8c8c5467269cb7f960d32e1de9f8
[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
45                 boolean retVal = false;
46
47                 if (in!=null) {
48                         try {
49                 File f = null;
50                 f = new File(filename);
51                                 File dir = new File(f.getParent());
52                                 dir.mkdirs();
53
54                                 FileOutputStream outStream;
55                                 outStream = new FileOutputStream(f);
56                                 outStream.write(in);
57                                 outStream.close();
58                                 retVal = true;
59                         } catch(IOException exception) {}
60         }
61                 return retVal;
62         }
63
64   public static boolean read(String filename, byte out[]) {
65
66         File f = null;
67         f = new File(filename);
68
69                 if (f.exists()) {
70                         try {
71                 if (out.length != f.length())
72                     return false;
73                                 FileInputStream inStream;
74                                 inStream = new FileInputStream(f);
75                                 inStream.read(out);
76                                 inStream.close();
77                         } catch(IOException exception) {
78                 return false;
79             }
80         } else {
81             return false;
82         }
83                 return true;
84         }
85     
86     public static long getSize(String filename) {
87         File f = null;
88         f = new File(filename);
89         long l=0;
90
91                 if (f.exists()) {
92                 return f.length();
93         } else {
94             return -1;
95         }
96     }
97
98     private static FileNameMap getFileNameMap() {
99         if ((fileNameMap == null) && !fileNameMapLoaded) {
100             fileNameMap = sun.net.www.MimeTable.loadTable();
101             fileNameMapLoaded = true;
102         }
103
104         return new FileNameMap() {
105             private FileNameMap map = fileNameMap;
106             public String getContentTypeFor(String fileName) {
107                 return map.getContentTypeFor(fileName);
108             }
109         };
110     }
111
112     public static void setFileNameMap(FileNameMap map) {
113         fileNameMap = map;
114     }
115
116     public static String guessContentTypeFromName(String fname) {
117         String contentType = null;
118                     
119         contentType = getFileNameMap().getContentTypeFor(fname);
120                                     
121         return contentType;
122     }
123
124
125  
126   
127 }