1 16 package org.apache.cocoon.util; 17 18 import java.io.BufferedInputStream ; 19 import java.io.BufferedReader ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileNotFoundException ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.InputStreamReader ; 26 import java.io.Reader ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.util.StringTokenizer ; 30 31 40 public class MIMEUtils { 41 42 private static final String MIME_MAPPING_FILE = "org/apache/cocoon/util/mime.types"; 43 44 45 final private static Map extMap = new HashMap(); 46 47 final private static Map mimeMap = new HashMap(); 48 49 52 static { 53 try { 54 final InputStream is = MIMEUtils.class.getClassLoader().getResourceAsStream(MIME_MAPPING_FILE); 55 if (null == is) { 56 throw new RuntimeException ("Cocoon cannot load MIME type mappings from " + MIME_MAPPING_FILE); 57 } 58 loadMimeTypes(new InputStreamReader (is), extMap, mimeMap); 59 } catch (IOException ioe) { 60 throw new RuntimeException ("Cocoon cannot load MIME type mappings from " + MIME_MAPPING_FILE); 61 } 62 } 63 64 70 public static String getMIMEType(final File file) 71 throws FileNotFoundException , IOException { 72 BufferedInputStream in = null; 73 74 try { 75 in = new BufferedInputStream (new FileInputStream (file)); 76 byte[] buf = new byte[3]; 77 int count = in.read(buf, 0, 3); 78 79 if (count < 3) { 80 return (null); 81 } 82 83 if ((buf[0]) == (byte)'G' && (buf[1]) == (byte)'I' && (buf[2]) == (byte)'F') { 84 return ("image/gif"); 85 } 86 87 if ((buf[0]) == (byte)0xFF && (buf[1]) == (byte)0xD8) { 88 return ("image/jpeg"); 89 } 90 91 } finally { 92 if (in != null) { 93 try { 94 in.close(); 95 } catch (IOException e) { 96 } 97 } 98 } 99 final String name = file.getName(); 100 int index = name.lastIndexOf("."); 101 String fileExt = "."; 102 if (index != -1) { 103 fileExt = name.substring(index); 104 } 105 return getMIMEType(fileExt); 106 } 107 108 115 public static String getMIMEType(final String ext) { 116 return (String )mimeMap.get(ext); 117 } 118 119 126 public static String getDefaultExtension(final String type) { 127 return (String )extMap.get(type); 128 } 129 130 146 public static void loadMimeTypes(Reader in, Map extMap, Map mimeMap) throws IOException { 147 BufferedReader br = new BufferedReader (in); 148 String line; 149 while ((line = br.readLine()) != null) { 150 if (line.startsWith("#")) { 151 continue; 152 } 153 if (line.trim().equals("")) { 154 continue; 155 } 156 StringTokenizer tok = new StringTokenizer (line, " \t"); 157 String mimeType = tok.nextToken(); 158 if (tok.hasMoreTokens()) { 159 String defaultExt = tok.nextToken(); 160 if (!extMap.containsKey(mimeType)) { 161 extMap.put(mimeType, "." + defaultExt); 162 } 163 if (!mimeMap.containsKey("." + defaultExt)) { 164 mimeMap.put("." + defaultExt, mimeType); 165 } 166 while (tok.hasMoreTokens()) { 167 String ext = tok.nextToken(); 168 if (!mimeMap.containsKey("." + ext)) { 169 mimeMap.put("." + ext, mimeType); 170 } 171 } 172 } 173 } 174 } 175 } 176 | Popular Tags |