/*
 * @(#)ZipRecognizer.java  1.0  2009-12-22
 *
 * Copyright (c) 2009 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;

/**
 * This recognizer can identify Zip files.
 *
 * @author  Werner Randelshofer, Hausmatt 10, CH-6405 Immensee, Switzerland.
 * @version 1.0 2009-12-22 Created.
 */
public class ZipRecognizer
implements Recognizer {
    private final static ZipRecognizer instance = new ZipRecognizer();

    private ZipRecognizer() {}

    public static ZipRecognizer getInstance() {
        return instance;
    }

    /**
     * Return the number of bytes needed to
     * determine the content type.
     */
    public int getMinimalDeterminableDataLength() {
        return 4;
    }

    /**
     * Return the base MIME Type of this data. This method is
     * expected to ALWAYS return a valid (non-null) MIME Type.
     *
     * <p>Returns "application/zip" if the file starts with "PK".
     *
     * @param  dataSnippet the first few bytes of the data file.
     * @return MIME Type or application/octet-stream' if content type
     * can not be determined.
     */
    public String getContentType(byte[] dataSnippet) {
        int fileID =
        (dataSnippet[0] & 0xff) << 24 |
        (dataSnippet[1] & 0xff) << 16 |
        (dataSnippet[2] & 0xff) << 8 |
        (dataSnippet[3] & 0xff);

        if (fileID==0x504b0304) {
            return "application/zip";
        }

        return Recognizer.CONTENT_TYPE_OCTET_STREAM;
    }
}
