<commit_before>/*
 * Copyright 1997-2003 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package org.n52.movingcode.runtime.iodata;

import java.io.*;
import java.util.*;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.sun.activation.registries.MimeTypeEntry;

public class MimeTypeDatabase {
	private String fname = null;
	private Multimap<String,String> mimeToExtMap = ArrayListMultimap.create();
	private Multimap<String,String> extToMimeMap = ArrayListMultimap.create();

	/**
	 * The construtor that takes a filename as an argument.
	 *
	 * @param new_fname The file name of the mime types file.
	 */
	public MimeTypeDatabase(String new_fname) throws IOException {
		File mime_file = null;
		FileReader fr = null;

		fname = new_fname; // remember the file name

		mime_file = new File(fname); // get a file object

		fr = new FileReader(mime_file);

		try {
			parse(new BufferedReader(fr));
		} finally {
			try {
				fr.close(); // close it
			} catch (IOException e) {
				// ignore it
			}
		}
	}

	public MimeTypeDatabase(InputStream is) throws IOException {
		parse(new BufferedReader(new InputStreamReader(is, "iso-8859-1")));
	}

	/**
	 * Creates an empty DB.
	 */
	public MimeTypeDatabase() {
	}

	/**
	 * get the MimeTypeEntries based on the file extension
	 */
	public MimeTypeEntry[] getMimeTypeEntriesByExt(String file_ext) {
		Collection<String> mimetypes = mimeToExtMap.get(file_ext);
		MimeTypeEntry[] entries = new MimeTypeEntry[mimetypes.size()];
		int i = 0;
		for (String mt : mimetypes){
			entries[i] = new MimeTypeEntry(mt, file_ext);
			i++;
		}
		return entries;
	}
	
	/**
	 * get the MimeTypeEntries based on the file extension
	 */
	public MimeTypeEntry[] getMimeTypeEntriesByMime(String mime_type) {
		Collection<String> extensions = extToMimeMap.get(mime_type);
		MimeTypeEntry[] entries = new MimeTypeEntry[extensions.size()];
		int i = 0;
		for (String ext : extensions){
			entries[i] = new MimeTypeEntry(mime_type, ext);
			i++;
		}
		return entries;
	}

	/**
	 * Get the MIME type string array corresponding to the file extension.
	 */
	public String[] getMIMETypeStrings(String file_ext) {
		if (extToMimeMap.containsKey(file_ext)){
			return extToMimeMap.get(file_ext).toArray(new String[extToMimeMap.get(file_ext).size()]);
		} else {
			return null;
		}
	}
	
	/**
	 * Get the File Extension string array corresponding to the mimeType.
	 */
	public String[] getExtensionStrings(String mime_type) {
		if (mimeToExtMap.containsKey(mime_type)){
			return mimeToExtMap.get(mime_type).toArray(new String[mimeToExtMap.get(mime_type).size()]);
		}
		else return null;
	}

	/**
	 * Appends string of entries to the types registry, must be valid
	 * .mime.types format.
	 * A mime.types entry is one of two forms:
	 *
	 *  type/subtype    ext1 ext2 ...
	 * or
	 *  type=type/subtype desc="description of type" exts=ext1,ext2,...
	 *
	 * Example:
	 * # this is a test
	 * audio/basic            au
	 * text/plain             txt text
	 * type=application/postscript exts=ps,eps
	 */
	public void appendToRegistry(String mime_types) {
		try {
			parse(new BufferedReader(new StringReader(mime_types)));
		} catch (IOException ex) {<commit_msg>added a default file extension in case no mimeType is applicable<commit_after>/*
 * Copyright 1997-2003 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package org.n52.movingcode.runtime.iodata;

import java.io.*;
import java.util.*;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.sun.activation.registries.MimeTypeEntry;

public class MimeTypeDatabase {
	private String fname = null;
	private Multimap<String,String> mimeToExtMap = ArrayListMultimap.create();
	private Multimap<String,String> extToMimeMap = ArrayListMultimap.create();
	
	private static final String[] defaultFileExt = {"dat"};

	/**
	 * The construtor that takes a filename as an argument.
	 *
	 * @param new_fname The file name of the mime types file.
	 */
	public MimeTypeDatabase(String new_fname) throws IOException {
		File mime_file = null;
		FileReader fr = null;

		fname = new_fname; // remember the file name

		mime_file = new File(fname); // get a file object

		fr = new FileReader(mime_file);

		try {
			parse(new BufferedReader(fr));
		} finally {
			try {
				fr.close(); // close it
			} catch (IOException e) {
				// ignore it
			}
		}
	}

	public MimeTypeDatabase(InputStream is) throws IOException {
		parse(new BufferedReader(new InputStreamReader(is, "iso-8859-1")));
	}

	/**
	 * Creates an empty DB.
	 */
	public MimeTypeDatabase() {
	}

	/**
	 * get the MimeTypeEntries based on the file extension
	 */
	public MimeTypeEntry[] getMimeTypeEntriesByExt(String file_ext) {
		Collection<String> mimetypes = mimeToExtMap.get(file_ext);
		MimeTypeEntry[] entries = new MimeTypeEntry[mimetypes.size()];
		int i = 0;
		for (String mt : mimetypes){
			entries[i] = new MimeTypeEntry(mt, file_ext);
			i++;
		}
		return entries;
	}
	
	/**
	 * get the MimeTypeEntries based on the file extension
	 */
	public MimeTypeEntry[] getMimeTypeEntriesByMime(String mime_type) {
		Collection<String> extensions = extToMimeMap.get(mime_type);
		MimeTypeEntry[] entries = new MimeTypeEntry[extensions.size()];
		int i = 0;
		for (String ext : extensions){
			entries[i] = new MimeTypeEntry(mime_type, ext);
			i++;
		}
		return entries;
	}

	/**
	 * Get the MIME type string array corresponding to the file extension.
	 */
	public String[] getMIMETypeStrings(String file_ext) {
		if (extToMimeMap.containsKey(file_ext)){
			return extToMimeMap.get(file_ext).toArray(new String[extToMimeMap.get(file_ext).size()]);
		} else {
			return null;
		}
	}
	
	/**
	 * Get the File Extension string array corresponding to the mimeType.
	 */
	public String[] getExtensionStrings(String mime_type) {
		if (mimeToExtMap.containsKey(mime_type)){
			return mimeToExtMap.get(mime_type).toArray(new String[mimeToExtMap.get(mime_type).size()]);
		}
		else {
			return defaultFileExt;
		}
	}

	/**
	 * Appends string of entries to the types registry, must be valid
	 * .mime.types format.
	 * A mime.types entry is one of two forms:
	 *
	 *  type/subtype    ext1 ext2 ...
	 * or
	 *  type=type/subtype desc="description of type" exts=ext1,ext2,...
	 *
	 * Example:
	 * # this is a test
	 * audio/basic            au
	 * text/plain             txt text
	 * type=application/postscript exts=ps,eps
	 */
	public void appendToRegistry(String mime_types) {
		try {
			parse(new BufferedReader(new StringReader(mime_types)));
		} catch (IOException ex) {
{'fraction_non_alphanumeric': 0.09658129301590883, 'fraction_numerical': 0.009928917973598104, 'mean_word_length': 3.7451820128479656, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7060516', 'n_tokens_mistral': 2977, 'n_tokens_neox': 2674, 'n_words': 920}