2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / gcj / io / MimeTypes.java
blobed08479f04603658aace57e3114eb4be104c0081
1 /* Copyright (C) 2000 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
9 package gnu.gcj.io;
11 import java.util.*;
12 import java.io.*;
14 public class MimeTypes
16 private static Hashtable mime_types;
18 public static void fillFromFile (Hashtable table, String fname)
19 throws IOException
21 LineNumberReader reader =
22 new LineNumberReader (new FileReader (fname));
24 while (reader.ready ())
26 StringTokenizer tokenizer =
27 new StringTokenizer (reader.readLine ());
29 try
31 String t = tokenizer.nextToken ();
33 if (! t.startsWith ("#"))
35 while (true)
37 // Read the next extension
38 String e = tokenizer.nextToken ();
39 if ((e != null) && (! e.startsWith ("#")))
40 table.put (e, t);
41 else
42 break;
45 } catch (NoSuchElementException ex) {
46 // Do nothing.
51 // This is the primary interface to this class.
52 public static String getMimeTypeFromExtension (String extension)
54 if (mime_types == null)
56 mime_types = new Hashtable ();
58 // First populate the hash table with the default mime type
59 // mappings.
60 int i = DefaultMimeTypes.types.length;
61 while (i > 1)
63 mime_types.put (DefaultMimeTypes.types[i - 2],
64 DefaultMimeTypes.types[i - 1]);
65 i = i - 2;
68 // Now read mime types from /etc/mime.types if it exists.
69 // This should override the default values.
70 try {
71 fillFromFile (mime_types, "/etc/mime.types");
72 } catch (IOException ex) {
73 // Do nothing.
76 // Now read mime types from ~/.mime.types.
77 // FIXME: We can't currently parse this file.
80 String type = (String) mime_types.get (extension);
81 if (type == null)
82 return ("application/octet-stream");
83 else
84 return (type);