Java BufferedReader Read loadMimeTypes(Reader in, Map extMap, Map mimeMap)

Here you can find the source of loadMimeTypes(Reader in, Map extMap, Map mimeMap)

Description

Parses a mime.types file, and generates mappings between MIME types and extensions.

License

Apache License

Parameter

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.

Declaration

public static void loadMimeTypes(Reader in, Map extMap, Map mimeMap) throws IOException 

Method Source Code

//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);
                    }
                }
            }
        }
    }
}

Related

  1. loadList(String listName)
  2. loadListFromTextFile(String filename, String defaultitem, String linePrefix, String lineSuffix)
  3. loadLongArray(BufferedReader in)
  4. loadMetadata(File file)
  5. loadMetaFile(File metaFile)
  6. loadModel(String fileName, Hashtable dictionary, Hashtable> modelPairs)
  7. loadNameMap(String mapName, boolean reverseMap)
  8. loadNumUsers(String fileName)
  9. loadPartitions(String fileName)