Home | History | Annotate | Download | only in net
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package libcore.net;
     18 
     19 import java.util.HashMap;
     20 import java.util.Map;
     21 
     22 /**
     23  * Utilities for dealing with MIME types.
     24  * Used to implement java.net.URLConnection and android.webkit.MimeTypeMap.
     25  */
     26 public final class MimeUtils {
     27     private static final Map<String, String> mimeTypeToExtensionMap = new HashMap<String, String>();
     28 
     29     private static final Map<String, String> extensionToMimeTypeMap = new HashMap<String, String>();
     30 
     31     static {
     32         // The following table is based on /etc/mime.types data minus
     33         // chemical/* MIME types and MIME types that don't map to any
     34         // file extensions. We also exclude top-level domain names to
     35         // deal with cases like:
     36         //
     37         // mail.google.com/a/google.com
     38         //
     39         // and "active" MIME types (due to potential security issues).
     40 
     41         // Note that this list is _not_ in alphabetical order and must not be sorted.
     42         // The "most popular" extension must come first, so that it's the one returned
     43         // by guessExtensionFromMimeType.
     44 
     45         add("application/andrew-inset", "ez");
     46         add("application/dsptype", "tsp");
     47         add("application/epub+zip", "epub");
     48         add("application/hta", "hta");
     49         add("application/mac-binhex40", "hqx");
     50         add("application/mathematica", "nb");
     51         add("application/msaccess", "mdb");
     52         add("application/oda", "oda");
     53         add("application/ogg", "ogg");
     54         add("application/ogg", "oga");
     55         add("application/pdf", "pdf");
     56         add("application/pgp-keys", "key");
     57         add("application/pgp-signature", "pgp");
     58         add("application/pics-rules", "prf");
     59         add("application/pkix-cert", "cer");
     60         add("application/rar", "rar");
     61         add("application/rdf+xml", "rdf");
     62         add("application/rss+xml", "rss");
     63         add("application/zip", "zip");
     64         add("application/vnd.android.package-archive", "apk");
     65         add("application/vnd.cinderella", "cdy");
     66         add("application/vnd.ms-pki.stl", "stl");
     67         add("application/vnd.oasis.opendocument.database", "odb");
     68         add("application/vnd.oasis.opendocument.formula", "odf");
     69         add("application/vnd.oasis.opendocument.graphics", "odg");
     70         add("application/vnd.oasis.opendocument.graphics-template", "otg");
     71         add("application/vnd.oasis.opendocument.image", "odi");
     72         add("application/vnd.oasis.opendocument.presentation", "odp");
     73         add("application/vnd.oasis.opendocument.presentation-template", "otp");
     74         add("application/vnd.oasis.opendocument.spreadsheet", "ods");
     75         add("application/vnd.oasis.opendocument.spreadsheet-template", "ots");
     76         add("application/vnd.oasis.opendocument.text", "odt");
     77         add("application/vnd.oasis.opendocument.text-master", "odm");
     78         add("application/vnd.oasis.opendocument.text-template", "ott");
     79         add("application/vnd.oasis.opendocument.text-web", "oth");
     80         add("application/vnd.google-earth.kml+xml", "kml");
     81         add("application/vnd.google-earth.kmz", "kmz");
     82         add("application/msword", "doc");
     83         add("application/msword", "dot");
     84         add("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx");
     85         add("application/vnd.openxmlformats-officedocument.wordprocessingml.template", "dotx");
     86         add("application/vnd.ms-excel", "xls");
     87         add("application/vnd.ms-excel", "xlt");
     88         add("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx");
     89         add("application/vnd.openxmlformats-officedocument.spreadsheetml.template", "xltx");
     90         add("application/vnd.ms-powerpoint", "ppt");
     91         add("application/vnd.ms-powerpoint", "pot");
     92         add("application/vnd.ms-powerpoint", "pps");
     93         add("application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx");
     94         add("application/vnd.openxmlformats-officedocument.presentationml.template", "potx");
     95         add("application/vnd.openxmlformats-officedocument.presentationml.slideshow", "ppsx");
     96         add("application/vnd.rim.cod", "cod");
     97         add("application/vnd.smaf", "mmf");
     98         add("application/vnd.stardivision.calc", "sdc");
     99         add("application/vnd.stardivision.draw", "sda");
    100         add("application/vnd.stardivision.impress", "sdd");
    101         add("application/vnd.stardivision.impress", "sdp");
    102         add("application/vnd.stardivision.math", "smf");
    103         add("application/vnd.stardivision.writer", "sdw");
    104         add("application/vnd.stardivision.writer", "vor");
    105         add("application/vnd.stardivision.writer-global", "sgl");
    106         add("application/vnd.sun.xml.calc", "sxc");
    107         add("application/vnd.sun.xml.calc.template", "stc");
    108         add("application/vnd.sun.xml.draw", "sxd");
    109         add("application/vnd.sun.xml.draw.template", "std");
    110         add("application/vnd.sun.xml.impress", "sxi");
    111         add("application/vnd.sun.xml.impress.template", "sti");
    112         add("application/vnd.sun.xml.math", "sxm");
    113         add("application/vnd.sun.xml.writer", "sxw");
    114         add("application/vnd.sun.xml.writer.global", "sxg");
    115         add("application/vnd.sun.xml.writer.template", "stw");
    116         add("application/vnd.visio", "vsd");
    117         add("application/x-abiword", "abw");
    118         add("application/x-apple-diskimage", "dmg");
    119         add("application/x-bcpio", "bcpio");
    120         add("application/x-bittorrent", "torrent");
    121         add("application/x-cdf", "cdf");
    122         add("application/x-cdlink", "vcd");
    123         add("application/x-chess-pgn", "pgn");
    124         add("application/x-cpio", "cpio");
    125         add("application/x-debian-package", "deb");
    126         add("application/x-debian-package", "udeb");
    127         add("application/x-director", "dcr");
    128         add("application/x-director", "dir");
    129         add("application/x-director", "dxr");
    130         add("application/x-dms", "dms");
    131         add("application/x-doom", "wad");
    132         add("application/x-dvi", "dvi");
    133         add("application/x-font", "pfa");
    134         add("application/x-font", "pfb");
    135         add("application/x-font", "gsf");
    136         add("application/x-font", "pcf");
    137         add("application/x-font", "pcf.Z");
    138         add("application/x-freemind", "mm");
    139         // application/futuresplash isn't IANA, so application/x-futuresplash should come first.
    140         add("application/x-futuresplash", "spl");
    141         add("application/futuresplash", "spl");
    142         add("application/x-gnumeric", "gnumeric");
    143         add("application/x-go-sgf", "sgf");
    144         add("application/x-graphing-calculator", "gcf");
    145         add("application/x-gtar", "tgz");
    146         add("application/x-gtar", "gtar");
    147         add("application/x-gtar", "taz");
    148         add("application/x-hdf", "hdf");
    149         add("application/x-hwp", "hwp"); // http://b/18788282.
    150         add("application/x-ica", "ica");
    151         add("application/x-internet-signup", "ins");
    152         add("application/x-internet-signup", "isp");
    153         add("application/x-iphone", "iii");
    154         add("application/x-iso9660-image", "iso");
    155         add("application/x-jmol", "jmz");
    156         add("application/x-kchart", "chrt");
    157         add("application/x-killustrator", "kil");
    158         add("application/x-koan", "skp");
    159         add("application/x-koan", "skd");
    160         add("application/x-koan", "skt");
    161         add("application/x-koan", "skm");
    162         add("application/x-kpresenter", "kpr");
    163         add("application/x-kpresenter", "kpt");
    164         add("application/x-kspread", "ksp");
    165         add("application/x-kword", "kwd");
    166         add("application/x-kword", "kwt");
    167         add("application/x-latex", "latex");
    168         add("application/x-lha", "lha");
    169         add("application/x-lzh", "lzh");
    170         add("application/x-lzx", "lzx");
    171         add("application/x-maker", "frm");
    172         add("application/x-maker", "maker");
    173         add("application/x-maker", "frame");
    174         add("application/x-maker", "fb");
    175         add("application/x-maker", "book");
    176         add("application/x-maker", "fbdoc");
    177         add("application/x-mif", "mif");
    178         add("application/x-ms-wmd", "wmd");
    179         add("application/x-ms-wmz", "wmz");
    180         add("application/x-msi", "msi");
    181         add("application/x-ns-proxy-autoconfig", "pac");
    182         add("application/x-nwc", "nwc");
    183         add("application/x-object", "o");
    184         add("application/x-oz-application", "oza");
    185         add("application/x-pem-file", "pem");
    186         add("application/x-pkcs12", "p12");
    187         add("application/x-pkcs12", "pfx");
    188         add("application/x-pkcs7-certreqresp", "p7r");
    189         add("application/x-pkcs7-crl", "crl");
    190         add("application/x-quicktimeplayer", "qtl");
    191         add("application/x-shar", "shar");
    192         add("application/x-shockwave-flash", "swf");
    193         add("application/x-stuffit", "sit");
    194         add("application/x-sv4cpio", "sv4cpio");
    195         add("application/x-sv4crc", "sv4crc");
    196         add("application/x-tar", "tar");
    197         add("application/x-texinfo", "texinfo");
    198         add("application/x-texinfo", "texi");
    199         add("application/x-troff", "t");
    200         add("application/x-troff", "roff");
    201         add("application/x-troff-man", "man");
    202         add("application/x-ustar", "ustar");
    203         add("application/x-wais-source", "src");
    204         add("application/x-wingz", "wz");
    205         add("application/x-webarchive", "webarchive");
    206         add("application/x-webarchive-xml", "webarchivexml");
    207         add("application/x-x509-ca-cert", "crt");
    208         add("application/x-x509-user-cert", "crt");
    209         add("application/x-x509-server-cert", "crt");
    210         add("application/x-xcf", "xcf");
    211         add("application/x-xfig", "fig");
    212         add("application/xhtml+xml", "xhtml");
    213         add("audio/3gpp", "3gpp");
    214         add("audio/aac", "aac");
    215         add("audio/aac-adts", "aac");
    216         add("audio/amr", "amr");
    217         add("audio/amr-wb", "awb");
    218         add("audio/basic", "snd");
    219         add("audio/flac", "flac");
    220         add("application/x-flac", "flac");
    221         add("audio/imelody", "imy");
    222         add("audio/midi", "mid");
    223         add("audio/midi", "midi");
    224         add("audio/midi", "ota");
    225         add("audio/midi", "kar");
    226         add("audio/midi", "rtttl");
    227         add("audio/midi", "xmf");
    228         add("audio/mobile-xmf", "mxmf");
    229         // add ".mp3" first so it will be the default for guessExtensionFromMimeType
    230         add("audio/mpeg", "mp3");
    231         add("audio/mpeg", "mpga");
    232         add("audio/mpeg", "mpega");
    233         add("audio/mpeg", "mp2");
    234         add("audio/mpeg", "m4a");
    235         add("audio/mpegurl", "m3u");
    236         add("audio/prs.sid", "sid");
    237         add("audio/x-aiff", "aif");
    238         add("audio/x-aiff", "aiff");
    239         add("audio/x-aiff", "aifc");
    240         add("audio/x-gsm", "gsm");
    241         add("audio/x-matroska", "mka");
    242         add("audio/x-mpegurl", "m3u");
    243         add("audio/x-ms-wma", "wma");
    244         add("audio/x-ms-wax", "wax");
    245         add("audio/x-pn-realaudio", "ra");
    246         add("audio/x-pn-realaudio", "rm");
    247         add("audio/x-pn-realaudio", "ram");
    248         add("audio/x-realaudio", "ra");
    249         add("audio/x-scpls", "pls");
    250         add("audio/x-sd2", "sd2");
    251         add("audio/x-wav", "wav");
    252         // image/bmp isn't IANA, so image/x-ms-bmp should come first.
    253         add("image/x-ms-bmp", "bmp");
    254         add("image/bmp", "bmp");
    255         add("image/gif", "gif");
    256         // image/ico isn't IANA, so image/x-icon should come first.
    257         add("image/x-icon", "ico");
    258         add("image/ico", "cur");
    259         add("image/ico", "ico");
    260         add("image/ief", "ief");
    261         // add ".jpg" first so it will be the default for guessExtensionFromMimeType
    262         add("image/jpeg", "jpg");
    263         add("image/jpeg", "jpeg");
    264         add("image/jpeg", "jpe");
    265         add("image/pcx", "pcx");
    266         add("image/png", "png");
    267         add("image/svg+xml", "svg");
    268         add("image/svg+xml", "svgz");
    269         add("image/tiff", "tiff");
    270         add("image/tiff", "tif");
    271         add("image/vnd.djvu", "djvu");
    272         add("image/vnd.djvu", "djv");
    273         add("image/vnd.wap.wbmp", "wbmp");
    274         add("image/webp", "webp");
    275         add("image/x-cmu-raster", "ras");
    276         add("image/x-coreldraw", "cdr");
    277         add("image/x-coreldrawpattern", "pat");
    278         add("image/x-coreldrawtemplate", "cdt");
    279         add("image/x-corelphotopaint", "cpt");
    280         add("image/x-jg", "art");
    281         add("image/x-jng", "jng");
    282         add("image/x-photoshop", "psd");
    283         add("image/x-portable-anymap", "pnm");
    284         add("image/x-portable-bitmap", "pbm");
    285         add("image/x-portable-graymap", "pgm");
    286         add("image/x-portable-pixmap", "ppm");
    287         add("image/x-rgb", "rgb");
    288         add("image/x-xbitmap", "xbm");
    289         add("image/x-xpixmap", "xpm");
    290         add("image/x-xwindowdump", "xwd");
    291         add("model/iges", "igs");
    292         add("model/iges", "iges");
    293         add("model/mesh", "msh");
    294         add("model/mesh", "mesh");
    295         add("model/mesh", "silo");
    296         add("text/calendar", "ics");
    297         add("text/calendar", "icz");
    298         add("text/comma-separated-values", "csv");
    299         add("text/css", "css");
    300         add("text/html", "htm");
    301         add("text/html", "html");
    302         add("text/h323", "323");
    303         add("text/iuls", "uls");
    304         add("text/mathml", "mml");
    305         // add ".txt" first so it will be the default for guessExtensionFromMimeType
    306         add("text/plain", "txt");
    307         add("text/plain", "asc");
    308         add("text/plain", "text");
    309         add("text/plain", "diff");
    310         add("text/plain", "po");     // reserve "pot" for vnd.ms-powerpoint
    311         add("text/richtext", "rtx");
    312         add("text/rtf", "rtf");
    313         add("text/text", "phps");
    314         add("text/tab-separated-values", "tsv");
    315         add("text/xml", "xml");
    316         add("text/x-bibtex", "bib");
    317         add("text/x-boo", "boo");
    318         add("text/x-c++hdr", "hpp");
    319         add("text/x-c++hdr", "h++");
    320         add("text/x-c++hdr", "hxx");
    321         add("text/x-c++hdr", "hh");
    322         add("text/x-c++src", "cpp");
    323         add("text/x-c++src", "c++");
    324         add("text/x-c++src", "cc");
    325         add("text/x-c++src", "cxx");
    326         add("text/x-chdr", "h");
    327         add("text/x-component", "htc");
    328         add("text/x-csh", "csh");
    329         add("text/x-csrc", "c");
    330         add("text/x-dsrc", "d");
    331         add("text/x-haskell", "hs");
    332         add("text/x-java", "java");
    333         add("text/x-literate-haskell", "lhs");
    334         add("text/x-moc", "moc");
    335         add("text/x-pascal", "p");
    336         add("text/x-pascal", "pas");
    337         add("text/x-pcs-gcd", "gcd");
    338         add("text/x-setext", "etx");
    339         add("text/x-tcl", "tcl");
    340         add("text/x-tex", "tex");
    341         add("text/x-tex", "ltx");
    342         add("text/x-tex", "sty");
    343         add("text/x-tex", "cls");
    344         add("text/x-vcalendar", "vcs");
    345         add("text/x-vcard", "vcf");
    346         add("video/3gpp", "3gpp");
    347         add("video/3gpp", "3gp");
    348         add("video/3gpp2", "3gpp2");
    349         add("video/3gpp2", "3g2");
    350         add("video/avi", "avi");
    351         add("video/dl", "dl");
    352         add("video/dv", "dif");
    353         add("video/dv", "dv");
    354         add("video/fli", "fli");
    355         add("video/m4v", "m4v");
    356         add("video/mp2ts", "ts");
    357         add("video/mpeg", "mpeg");
    358         add("video/mpeg", "mpg");
    359         add("video/mpeg", "mpe");
    360         add("video/mp4", "mp4");
    361         add("video/mpeg", "VOB");
    362         add("video/quicktime", "qt");
    363         add("video/quicktime", "mov");
    364         add("video/vnd.mpegurl", "mxu");
    365         add("video/webm", "webm");
    366         add("video/x-la-asf", "lsf");
    367         add("video/x-la-asf", "lsx");
    368         add("video/x-matroska", "mkv");
    369         add("video/x-mng", "mng");
    370         add("video/x-ms-asf", "asf");
    371         add("video/x-ms-asf", "asx");
    372         add("video/x-ms-wm", "wm");
    373         add("video/x-ms-wmv", "wmv");
    374         add("video/x-ms-wmx", "wmx");
    375         add("video/x-ms-wvx", "wvx");
    376         add("video/x-sgi-movie", "movie");
    377         add("video/x-webex", "wrf");
    378         add("x-conference/x-cooltalk", "ice");
    379         add("x-epoc/x-sisx-app", "sisx");
    380     }
    381 
    382     private static void add(String mimeType, String extension) {
    383         // If we have an existing x -> y mapping, we do not want to
    384         // override it with another mapping x -> y2.
    385         // If a mime type maps to several extensions
    386         // the first extension added is considered the most popular
    387         // so we do not want to overwrite it later.
    388         if (!mimeTypeToExtensionMap.containsKey(mimeType)) {
    389             mimeTypeToExtensionMap.put(mimeType, extension);
    390         }
    391         if (!extensionToMimeTypeMap.containsKey(extension)) {
    392             extensionToMimeTypeMap.put(extension, mimeType);
    393         }
    394     }
    395 
    396     private MimeUtils() {
    397     }
    398 
    399     /**
    400      * Returns true if the given MIME type has an entry in the map.
    401      * @param mimeType A MIME type (i.e. text/plain)
    402      * @return True iff there is a mimeType entry in the map.
    403      */
    404     public static boolean hasMimeType(String mimeType) {
    405         if (mimeType == null || mimeType.isEmpty()) {
    406             return false;
    407         }
    408         return mimeTypeToExtensionMap.containsKey(mimeType);
    409     }
    410 
    411     /**
    412      * Returns the MIME type for the given extension.
    413      * @param extension A file extension without the leading '.'
    414      * @return The MIME type for the given extension or null iff there is none.
    415      */
    416     public static String guessMimeTypeFromExtension(String extension) {
    417         if (extension == null || extension.isEmpty()) {
    418             return null;
    419         }
    420         return extensionToMimeTypeMap.get(extension);
    421     }
    422 
    423     /**
    424      * Returns true if the given extension has a registered MIME type.
    425      * @param extension A file extension without the leading '.'
    426      * @return True iff there is an extension entry in the map.
    427      */
    428     public static boolean hasExtension(String extension) {
    429         if (extension == null || extension.isEmpty()) {
    430             return false;
    431         }
    432         return extensionToMimeTypeMap.containsKey(extension);
    433     }
    434 
    435     /**
    436      * Returns the registered extension for the given MIME type. Note that some
    437      * MIME types map to multiple extensions. This call will return the most
    438      * common extension for the given MIME type.
    439      * @param mimeType A MIME type (i.e. text/plain)
    440      * @return The extension for the given MIME type or null iff there is none.
    441      */
    442     public static String guessExtensionFromMimeType(String mimeType) {
    443         if (mimeType == null || mimeType.isEmpty()) {
    444             return null;
    445         }
    446         return mimeTypeToExtensionMap.get(mimeType);
    447     }
    448 }
    449