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

@@ -963,16 +963,22 @@
  static 
+final 
 Tika tik
@@ -1048,16 +1048,16 @@
 ad-safe%0A
-
     priv
@@ -1135,16 +1135,22 @@
 private 
+final 
 byte buf
@@ -1375,32 +1375,56 @@
 %7B%0A        try %7B%0A
+            byte buf%5B%5D;%0A
             int 
@@ -1472,16 +1472,213 @@
 _SIZE);%0A
+            if (len %3C BUFFER_SIZE) %7B%0A                buf = new byte%5Blen%5D;%0A                System.arraycopy(buffer, 0, buf, 0, len);%0A            %7D else %7B%0A                buf = buffer;%0A            %7D%0A
         
@@ -1887,27 +1887,24 @@
 w String(buf
-fer
 , 0, 5);%0A   
@@ -2140,16 +2140,16 @@
     %7D %0A%0A
+
         
@@ -2189,19 +2189,16 @@
 tect(buf
-fer
 , abstra

Fix buffering of file data in TikaFileTypeDetector class