Class ContentType

  • All Implemented Interfaces:
    Serializable

    public final class ContentType
    extends Object
    implements Serializable
    Represents a file Content-Type (also called MIME-Type or Media Type).

    To detect the content type of a file, consider using an open-source library such as Apache Tika.

    To provide your own extension mappings or display names, copy the appropriate .properties file to your classpath root, with the word "custom" inserted: ContentType-custom-[...]. The actual custom names and classpath location are:

    Original vs custom mapping files.
    Original Custom
    com.norconex.commmons.lang.file.ContentType-extensions.properties ContentType-custom-extensions.properties
    com.norconex.commmons.lang.file.ContentType-name[_locale].properties ContentType-custom-name[_locale].propertiess
    Since:
    1.4.0
    Author:
    Pascal Essiembre
    See Also:
    Serialized Form
    • Method Detail

      • valueOf

        public static ContentType valueOf​(String contentType)
        Creates a new content type. Returns an existing instance if the same content type is requested more than once.
        Parameters:
        contentType - the official media type name
        Returns:
        content type instance or null if content type string is null or blank.
      • valuesOf

        public static ContentType[] valuesOf​(String... contentTypes)
        Creates a null-safe array of content types. The same number of elements as the supplied strings are returned. A null value will return an empty array. Each content types are individually obtained by invoking valueOf(String).
        Parameters:
        contentTypes - the official media type names
        Returns:
        content type array.
      • valuesOf

        public static List<ContentType> valuesOf​(List<String> contentTypes)
        Creates a null-safe array of content types. The same number of elements as the supplied strings are returned. A null value will return an empty array. Each content types are individually obtained by invoking valueOf(String).
        Parameters:
        contentTypes - the official media type names
        Returns:
        content type array.
        Since:
        2.0.0
      • getDisplayName

        public String getDisplayName()
        Gets a name for the content type suitable for display to a user. The system locale is used to defined the language of the display name. If no name has been defined for a content type, the raw content type is returned (equivalent to toString()).
        Returns:
        display name
      • getDisplayName

        public String getDisplayName​(Locale locale)
        Gets a name for the content type suitable for display to a user. If the locale is null, the system locale is used. If no name has been defined for a content type with the provided locale, the name defaults to English. If no name has been defined for any locale, the raw content type is returned (equivalent to toString()).
        Parameters:
        locale - the locale to use to get the display name
        Returns:
        display name
      • getExtension

        public String getExtension()
        Gets the file extension usually associated with this content type. If the content type has more than one extension, the first one is returned.
        Returns:
        file extension or empty string if no extension is defined
      • getExtensions

        public String[] getExtensions()
        Gets the file extensions usually associated with this content type. Most content types only have one commonly used extension.
        Returns:
        file extension or empty array if no extension is defined
      • matches

        public boolean matches​(String contentType)
        Whether the given string matches this content type.
        Parameters:
        contentType - the content type
        Returns:
        true if the given string matches this content type
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • toString

        public String toString()
        Returns the raw content-type representation.
        Overrides:
        toString in class Object
        Returns:
        content type as string
      • toBaseTypeString

        public String toBaseTypeString()
        Returns the raw content-type representation without any parameters (removes ";" and any values afterwards).
        Returns:
        content type as string without parameters
        Since:
        1.14.0
      • toBaseType

        public ContentType toBaseType()
        Returns a content-type without any parameters (removes ";" and any values afterwards). Invoking a content type without parameters will return itself.
        Returns:
        content type without parameters
        Since:
        1.14.0