/*
 * Autopsy Forensic Browser
 *
 * Copyright 2011-2014 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 *
 * 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.
 */
package org.sleuthkit.autopsy.modules.filetypeid;

import java.util.SortedSet;
import org.apache.tika.Tika;
import org.apache.tika.mime.MediaType;
import org.apache.tika.mime.MimeTypes;

import org.sleuthkit.datamodel.AbstractFile;


class TikaFileTypeDetector {

    private static Tika tikaInst = new Tika(); //calling detect() with this should be thread-safe
    private final int BUFFER_SIZE = 64 * 1024; //how many bytes to pass in
    private byte buffer[] = new byte[BUFFER_SIZE];
            
    /**
     * 
     * @param abstractFile
     * @return mime type of detected format or null
     */
    public synchronized String attemptMatch(AbstractFile abstractFile) {
        try {
            int len = abstractFile.read(buffer, 0, BUFFER_SIZE);
            
            // the xml detection in Tika tries to parse the entire file and throws exceptions
            // for files that are not valid XML
            try {
                String tagHeader = new String(buffer, 0, 5);
                if (tagHeader.equals("<?xml")) { //NON-NLS    
                    return "text/xml"; //NON-NLS
                }
            }
            catch (IndexOutOfBoundsException e) {
                // do nothing
            } 

            String mimetype = tikaInst.detect(buffer, abstractFile.getName());
            // Remove tika's name out of the general types like msoffice and ooxml
            return mimetype.replace("tika-", ""); //NON-NLS
        } catch (Exception ex) {
            //do nothing
        }
        return null;
    }

    /**
     * Validate if a given mime type is in the registry. For Tika, we remove the
     * string "tika" from all MIME names, e.g. use "application/x-msoffice" NOT
     * "application/x-tika-msoffice"
     *
     * @param mimeType Full string of mime type, e.g. "text/html"
     * @return true if detectable
     */
    public boolean isMimeTypeDetectable(String mimeType) {
        boolean ret = false;

        SortedSet<MediaType> m = MimeTypes.getDefaultMimeTypes().getMediaTypeRegistry().getTypes();
        String[] split = mimeType.split("/");

        if (split.length == 2) {
            String type = split[0];
            String subtype = split[1];
            MediaType mediaType = new MediaType(type, subtype);
            ret = m.contains(mediaType);
        }

        return ret;
    }
}

@@ -1042,16 +1042,18 @@
 ad-safe%0A
+//
     priv
@@ -1119,16 +1119,85 @@
 pass in%0A
+    private final int BUFFER_SIZE = 100; //how many bytes to pass in%0A
     priv
@@ -1981,16 +1981,18 @@
     %7D %0A%0A
+//
         
@@ -2062,16 +2062,71 @@
 ame());%0A
+            String mimetype = tikaInst.detect(buffer);%0A
         

Remove passing of additional data to Tika from file type identification module