FileDocCategorySizeDatePackage
MimeTable.javaAPI DocAndroid 1.5 API4906Wed May 06 22:41:04 BST 2009org.apache.harmony.luni.internal.net.www

MimeTable

public class MimeTable extends Object implements FileNameMap
Instances of this class map file extensions to MIME content types based on a default MIME table. The default values can be overridden by modifying the contents of the file "content-types.properties".

Fields Summary
public static final String
UNKNOWN
public static final Properties
types
A hash table containing the mapping between extensions and mime types.
Constructors Summary
public MimeTable()
Contructs a MIME table using the default values defined in this class. It then augments this table by reading pairs of extensions and corresponding content types from the file "content-types.properties", which is represented in standard java.util.Properties.load(...) format.


    // Default mapping.
     
        types.setProperty("text", "text/plain"); //$NON-NLS-1$ //$NON-NLS-2$
        types.setProperty("txt", "text/plain"); //$NON-NLS-1$ //$NON-NLS-2$
        types.setProperty("htm", "text/html"); //$NON-NLS-1$ //$NON-NLS-2$
        types.setProperty("html", "text/html"); //$NON-NLS-1$ //$NON-NLS-2$
    
        InputStream str = AccessController
                .doPrivileged(new PrivilegedAction<InputStream>() {
                    public InputStream run() {
                        return getContentTypes();
                    }
                });

        if (str != null) {
            try {
                try {
                    types.load(str);
                } finally {
                    str.close();
                }
            } catch (IOException ex) {
                // Ignore
            }
        }
    
Methods Summary
public java.lang.StringgetContentTypeFor(java.lang.String filename)
Determines the MIME type for the given filename.

param
filename The file whose extension will be mapped.
return
The mime type associated with the file's extension or null if no mapping is known.

        if (filename.endsWith("/")) { //$NON-NLS-1$
            // a directory, return html
            return (String) types.get("html"); //$NON-NLS-1$
        }
        int lastCharInExtension = filename.lastIndexOf('#");
        if (lastCharInExtension < 0) {
            lastCharInExtension = filename.length();
        }
        int firstCharInExtension = filename.lastIndexOf('.") + 1;
        String ext = ""; //$NON-NLS-1$
        if (firstCharInExtension > filename.lastIndexOf('/")) {
            ext = filename.substring(firstCharInExtension, lastCharInExtension);
        }
        return types.getProperty(ext.toLowerCase());
    
private java.io.InputStreamgetContentTypes()
Answer an InputStream over an external properties file containing the MIME types. Looks in the location specified in the user property, and then in the expected location.

return
the InputStream, or null if none.

        // User override?
        String userTable = System.getProperty("content.types.user.table"); //$NON-NLS-1$
        if (userTable != null) {
            try {
                return new FileInputStream(userTable);
            } catch (IOException e) {
                // Ignore invalid values
            }
        }

        // Standard location?
        String javahome = System.getProperty("java.home"); //$NON-NLS-1$
        File contentFile = new File(javahome, "lib" //$NON-NLS-1$
                + File.separator + "content-types.properties"); //$NON-NLS-1$
        try {
            return new FileInputStream(contentFile);
        } catch (IOException e) {
            // Not found or can't read
        }

        return null;