package com.limegroup.gnutella.malware;

import java.util.Arrays;

/**
 * Defines the mime type of a file based on patterns occurring at certain
 * offsets. For example, all ASF files start with the bytes
 * 0x3026B2758E66CF11A6D900AA0062CE6C, so the mime type named "video/x-ms-asf"
 * consists of that pattern at offset zero.
 */
class MimeType {

    final String name;
    final Long[] offsets;
    final byte[][] patterns;

    MimeType(String name, Long[] offsets, byte[][] patterns) {
        assert offsets.length == patterns.length;
        this.name = name;
        this.offsets = offsets;
        this.patterns = patterns;
    }

    @Override
    public boolean equals(Object o) {
        if(o instanceof MimeType) {
            MimeType m = (MimeType)o;
            if(!name.equals(m.name))
                return false;
            if(!Arrays.equals(offsets, m.offsets))
                return false;
            for(int i = 0; i < patterns.length; i++) {
                if(!Arrays.equals(patterns[i], m.patterns[i]))
                    return false;
            }
            return true;
        }
        return false;
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }
}
