/*
 * @(#)IFFRecognizer.java  1.2  2002-02-12
 *
 * Copyright (c) 1999 Werner Randelshofer, Immensee, Switzerland.
 * All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the
 * license agreement you entered into with Werner Randelshofer.
 * For details see accompanying license terms.
 */
package ch.randelshofer.activation;

import org.monte.media.iff.IFFParser;

/**
 * This recognizer can identify IFF files.
 *
 * @author  Werner Randelshofer, Hausmatt 10, CH-6405 Immensee, Switzerland.
 * @version 1.2 2002-02-12 Return application/octet-stream' for unknown data types.
 * <br>1.1 2000-10-03 Constructor made private. Method getInstance added.
 * <br> 1.0  1999-10-19
 */
public class IFFRecognizer
implements Recognizer {
    private final static IFFRecognizer instance = new IFFRecognizer();
    
    private IFFRecognizer() {}
    
    public static IFFRecognizer getInstance() {
        return instance;
    }
    
    /**
     * Return the number of bytes needed to
     * determine the content type.
     */
    @Override
    public int getMinimalDeterminableDataLength() {
        return 12;
    }
    
    /**
     * Return the base MIME Type of this data. This method is
     * expected to ALWAYS return a valid (non-null) MIME Type.
     *
     * <p>Returns "x-iff/*contenttype*" for IFF FORM files, where as
     * *contenttype* stands for the FORM type. e.g. returns "x-iff/ANIM"
     * for IFF ANIM files.  .
     *  <br>Returns "x-iff/LIST" for IFF LIST files.
     *  <br>Returns "x-iff/CAT" for IFF CAT files.
     *
     *
     * @param  dataSnippet the first few bytes of the data file.
     * @return MIME Type or application/octet-stream' if content type
     * can not be determined.
     */
    @Override
    public String getContentType(byte[] dataSnippet) {
        int fileID =
        (dataSnippet[0] & 0xff) << 24 |
        (dataSnippet[1] & 0xff) << 16 |
        (dataSnippet[2] & 0xff) << 8 |
        (dataSnippet[3] & 0xff);
        int contentID =
        (dataSnippet[8] & 0xff) << 24 |
        (dataSnippet[9] & 0xff) << 16|
        (dataSnippet[10] & 0xff) << 8 |
        (dataSnippet[11] & 0xff);
        
        if (IFFParser.isFormType(contentID)) {
            if (fileID == IFFParser.ID_FORM) {
                return "x-iff/" + IFFParser.idToString(contentID);
            } else if (fileID == IFFParser.ID_CAT) {
                return "x-iff/cat";
            } else if (fileID == IFFParser.ID_LIST) {
                return "x-iff/" + IFFParser.idToString(contentID);
            }
        }
        return Recognizer.CONTENT_TYPE_OCTET_STREAM;
    }
}
