Here you can find the source of loadMimeTypes(Reader in, Map extMap, Map mimeMap)
mime.types
file, and generates mappings between MIME types and extensions.
Parameter | Description |
---|---|
in | Reader of bytes from <code>mime.types</code> file content |
extMap | Empty map of default extensions, keyed by MIME type. Will be filled in by this method. |
mimeMap | Empty map of MIME types, keyed by extension. Will be filled in by this method. |
public static void loadMimeTypes(Reader in, Map extMap, Map mimeMap) throws IOException
//package com.java2s; /*//from ww w . j a v a2 s . c o m * Copyright 1999-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; import java.util.Map; import java.util.StringTokenizer; public class Main { /** * Parses a <code>mime.types</code> file, and generates mappings between * MIME types and extensions. * For example, if a line contains: * <pre>text/html html htm</pre> * Then 'html' will be the default extension for text/html, and both 'html' * and 'htm' will have MIME type 'text/html'. * Lines starting with '#' are treated as comments and ignored. If an * extension is listed for two MIME types, the first will be chosen. * * @param in Reader of bytes from <code>mime.types</code> file content * @param extMap Empty map of default extensions, keyed by MIME type. Will * be filled in by this method. * @param mimeMap Empty map of MIME types, keyed by extension. Will be * filled in by this method. */ public static void loadMimeTypes(Reader in, Map extMap, Map mimeMap) throws IOException { BufferedReader br = new BufferedReader(in); String line; while ((line = br.readLine()) != null) { if (line.startsWith("#")) { continue; } if (line.trim().length() == 0) { continue; } StringTokenizer tok = new StringTokenizer(line, " \t"); String mimeType = tok.nextToken(); if (tok.hasMoreTokens()) { String defaultExt = tok.nextToken(); if (!extMap.containsKey(mimeType)) { extMap.put(mimeType, "." + defaultExt); } if (!mimeMap.containsKey("." + defaultExt)) { mimeMap.put("." + defaultExt, mimeType); } while (tok.hasMoreTokens()) { String ext = tok.nextToken(); if (!mimeMap.containsKey("." + ext)) { mimeMap.put("." + ext, mimeType); } } } } } }