Package 

Class MediaType

  • All Implemented Interfaces:

    
    public final class MediaType
    
                        

    Represents a document format, identified by a unique RFC 6838 media type.

    MediaType handles:

    • components parsing – eg. type, subtype and parameters,

    • media types comparison.

    Comparing media types is more complicated than it looks, since they can contain parameters, such as charset=utf-8. We can't ignore them because some formats use parameters in their media type, for example application/atom+xml;profile=opds-catalog for an OPDS 1 catalog.

    Specification: https://tools.ietf.org/html/rfc6838

    • Constructor Detail

      • MediaType

        MediaType(String string, String name, String fileExtension)
        Parameters:
        string - String representation for this media type.
        name - A human readable name identifying the media type, which may be presented to the user.
        fileExtension - The default file extension to use for this media type.
    • Method Detail

      • getType

         final String getType()

        The type component, e.g. application in application/epub+zip.

      • getSubtype

         final String getSubtype()

        The subtype component, e.g. epub+zip in application/epub+zip.

      • canonicalMediaType

         final MediaType canonicalMediaType()

        Returns the canonical version of this media type, if it is known.

        This is useful to find the name and file extension of a known media type, or to get the canonical media type from an alias. For example, application/x-cbz is an alias of the canonical application/vnd.comicbook+zip.

        Non-significant parameters are also discarded.

      • toString

         String toString()

        The string representation of this media type.

      • equals

         Boolean equals(Object other)

        Returns whether two media types are equal, checking the type, subtype and parameters. Parameters order is ignored.

        WARNING: Strict media type comparisons can be a source of bug, if parameters are present. text/html != text/html;charset=utf-8 with strict equality comparison, which is most likely not the desired result. Instead, you can use matches to check if any of the media types is a parameterized version of the other one.

      • contains

         final Boolean contains(MediaType other)

        Returns whether the given other media type is included in this media type.

        For example, text/html contains text/html;charset=utf-8.

        • other must match the parameters in the parameters property, but extra parameters are ignored.

        • Order of parameters is ignored.

        • Wildcards are supported, meaning that image/* contains image/png and */* contains everything.

      • matches

         final Boolean matches(MediaType other)

        Returns whether this media type and other are the same, ignoring parameters that are not in both media types.

        For example, text/html matches text/html;charset=utf-8, but text/html;charset=ascii doesn't. This is basically like contains, but working in both direction.

      • matches

         final Boolean matches(String other)

        Returns whether this media type and other are the same, ignoring parameters that are not in both media types.

      • matchesAny

         final Boolean matchesAny(String others)

        Returns whether this media type matches any of the others media types.