/*
 * @(#)RIFFRecognizer.java  1.0  2005-01-09
 *
 * Copyright (c) 2005 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.riff.RIFFParser;

/**
 * This recognizer can identify RIFF files.
 *
 * @author  Werner Randelshofer, Hausmatt 10, CH-6405 Immensee, Switzerland.
 * @version 1.0 2005-01-09 Created.
 */
public class RIFFRecognizer implements Recognizer {
    private final static RIFFRecognizer instance = new RIFFRecognizer();
    
    private RIFFRecognizer() {}
    
    public static RIFFRecognizer 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-riff/*contenttype*" for RIFF FORM files, where as
     * *contenttype* stands for the FORM type. e.g. returns "x-riff/AVI"
     * for RIFF AVI 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 (fileID==RIFFParser.RIFF_ID) {
            if (RIFFParser.isGroupType(contentID)) {
                return "x-riff/" + RIFFParser.idToString(contentID).replace(' ', '_');
            }
        }
        return Recognizer.CONTENT_TYPE_OCTET_STREAM;
    }
}
