1:
8:
9: package ;
10:
11: import ;
12: import ;
13:
14: public class MimeTypes
15: {
16: private static Hashtable mime_types;
17:
18: public static void fillFromFile (Hashtable table, String fname)
19: throws IOException
20: {
21: LineNumberReader reader =
22: new LineNumberReader (new FileReader (fname));
23:
24: while (reader.ready ())
25: {
26: StringTokenizer tokenizer =
27: new StringTokenizer (reader.readLine ());
28:
29: try
30: {
31: String t = tokenizer.nextToken ();
32:
33: if (! t.startsWith ("#"))
34: {
35: while (true)
36: {
37:
38: String e = tokenizer.nextToken ();
39: if ((e != null) && (! e.startsWith ("#")))
40: table.put (e, t);
41: else
42: break;
43: }
44: }
45: } catch (NoSuchElementException ex) {
46:
47: }
48: }
49: }
50:
51:
52: public static String getMimeTypeFromExtension (String extension)
53: {
54: if (mime_types == null)
55: {
56: mime_types = new Hashtable ();
57:
58:
59:
60: int i = DefaultMimeTypes.types.length;
61: while (i > 1)
62: {
63: mime_types.put (DefaultMimeTypes.types[i - 2],
64: DefaultMimeTypes.types[i - 1]);
65: i = i - 2;
66: }
67:
68:
69:
70: try {
71: fillFromFile (mime_types, "/etc/mime.types");
72: } catch (IOException ex) {
73:
74: }
75:
76:
77:
78: }
79:
80: String type = (String) mime_types.get (extension);
81: if (type == null)
82: return ("application/octet-stream");
83: else
84: return (type);
85: }
86: }