Class ContentSniffer

java.lang.Object
com.iizix.util.ContentSniffer

public final class ContentSniffer extends Object
Detects a file's media type from its leading bytes ("magic bytes"), independently of any name, extension or declared type.

The sniffer is deliberately small and deliberately strict: it knows a handful of binary formats whose first bytes are a fixed signature, and answers null for everything else. Text formats (SVG, XML, CSV, plain text) are never sniffed, because a byte prefix cannot prove them. A caller therefore treats null as "unknown to the sniffer", never as "not a file of the declared type".

Stateless and thread-safe; all methods are static.

Author:
Christopher Mindus
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The number of leading bytes the sniffer needs; more are ignored, fewer may still match a shorter signature.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    isKnown(String mediaType)
    Checks if a media type is one the sniffer can recognize, i.e.
    static String
    sniff(byte[] head)
    Sniffs the media type from the leading bytes of a file.
    static String
    sniff(byte[] head, int length)
    Sniffs the media type from the leading bytes of a file.
    static String
    Reads the head of a stream and sniffs it.
  • Field Details

    • HEAD_LENGTH

      public static final int HEAD_LENGTH
      The number of leading bytes the sniffer needs; more are ignored, fewer may still match a shorter signature.
      See Also:
  • Method Details

    • sniff

      public static String sniff(byte[] head, int length)
      Sniffs the media type from the leading bytes of a file.
      Parameters:
      head - The leading bytes, at most HEAD_LENGTH are looked at.
      length - The number of valid bytes in head.
      Returns:
      The media type in lower case, or null if no known signature matches.
      Throws:
      NullPointerException - If head is null.
      IllegalArgumentException - If length is negative or exceeds the array.
    • sniff

      public static String sniff(byte[] head)
      Sniffs the media type from the leading bytes of a file.
      Parameters:
      head - The leading bytes; the whole array is valid.
      Returns:
      The media type in lower case, or null if no known signature matches.
    • sniff

      public static String sniff(InputStream in) throws IOException
      Reads the head of a stream and sniffs it. At most HEAD_LENGTH bytes are consumed; the stream is not closed and is not reset.
      Parameters:
      in - The stream, positioned at the first byte of the content.
      Returns:
      The media type in lower case, or null if no known signature matches.
      Throws:
      IOException - For read errors.
    • isKnown

      public static boolean isKnown(String mediaType)
      Checks if a media type is one the sniffer can recognize, i.e. one for which a null answer from sniff(byte[],int) means "the bytes are NOT of this type" rather than "unknown".
      Parameters:
      mediaType - The media type, case-insensitive, may be null.
      Returns:
      true if the sniffer knows a signature for the type.