Class MediaType


  • @Beta
    @GwtCompatible
    
    public final class MediaType
    extends 
    Represents an (also known as a MIME Type or Content Type). This class also supports the concept of media ranges . As such, the * character is treated as a wildcard and is used to represent any acceptable type or subtype value. A media type may not have wildcard type with a declared subtype. The * character has no special meaning as part of a parameter. All values for type, subtype, parameter attributes or parameter values must be valid according to RFCs and .

    All portions of the media type that are case-insensitive (type, subtype, parameter attributes) are normalized to lowercase. The value of the charset parameter is normalized to lowercase, but all others are left as-is.

    Note that this specifically does not represent the value of the MIME Content-Type header and as such has no support for header-specific considerations such as line folding and comments.

    For media types that take a charset the predefined constants default to UTF-8 and have a "_UTF_8" suffix. To get a version without a character set, use withoutParameters().

    Since:
    12.0
    Author:
    Gregory Kick
    • Method Detail

      • type

        public  type()
        Returns the top-level media type. For example, "text" in "text/plain".
      • subtype

        public  subtype()
        Returns the media subtype. For example, "plain" in "text/plain".
      • parameters

        public ,​> parameters()
        Returns a multimap containing the parameters of this media type.
      • charset

        public > charset()
        Returns an optional charset for the value of the charset parameter if it is specified.
        Throws:
        - if multiple charset values have been set for this media type
        - if a charset value is present, but illegal
        - if a charset value is present, but no support is available in this instance of the Java virtual machine
      • withoutParameters

        public MediaType withoutParameters()
        Returns a new instance with the same type and subtype as this instance, but without any parameters.
      • withParameters

        public ,​> parameters)
        Replaces all parameters with the given parameters.
        Throws:
        - if any parameter or value is invalid
      • withParameters

        public  attribute,
                                        <> values)
        Replaces all parameters with the given attribute with parameters using the given values. If there are no values, any existing parameters with the given attribute are removed.
        Throws:
        - if either attribute or values is invalid
        Since:
        24.0
      • withParameter

        public  attribute,
                                        value)
        Replaces all parameters with the given attribute with a single parameter with the given value. If multiple parameters with the same attributes are necessary use withParameters(String, Iterable). Prefer withCharset(java.nio.charset.Charset) for setting the charset parameter when using a object.
        Throws:
        - if either attribute or value is invalid
      • withCharset

        public  charset)
        Returns a new instance with the same type and subtype as this instance, with the charset parameter set to the of the given charset. Only one charset parameter will be present on the new instance regardless of the number set on this one.

        If a charset must be specified that is not supported on this JVM (and thus is not representable as a instance, use withParameter(java.lang.String, java.lang.String).

      • hasWildcard

        public boolean hasWildcard()
        Returns true if either the type or subtype is the wildcard.
      • is

        public boolean is​(MediaType mediaTypeRange)
        Returns true if this instance falls within the range (as defined by ) given by the argument according to three criteria:
        1. The type of the argument is the wildcard or equal to the type of this instance.
        2. The subtype of the argument is the wildcard or equal to the subtype of this instance.
        3. All of the parameters present in the argument are present in this instance.

        For example:

        
         PLAIN_TEXT_UTF_8.is(PLAIN_TEXT_UTF_8) // true
         PLAIN_TEXT_UTF_8.is(HTML_UTF_8) // false
         PLAIN_TEXT_UTF_8.is(ANY_TYPE) // true
         PLAIN_TEXT_UTF_8.is(ANY_TEXT_TYPE) // true
         PLAIN_TEXT_UTF_8.is(ANY_IMAGE_TYPE) // false
         PLAIN_TEXT_UTF_8.is(ANY_TEXT_TYPE.withCharset(UTF_8)) // true
         PLAIN_TEXT_UTF_8.withoutParameters().is(ANY_TEXT_TYPE.withCharset(UTF_8)) // false
         PLAIN_TEXT_UTF_8.is(ANY_TEXT_TYPE.withCharset(UTF_16)) // false
         

        Note that while it is possible to have the same parameter declared multiple times within a media type this method does not consider the number of occurrences of a parameter. For example, "text/plain; charset=UTF-8" satisfies "text/plain; charset=UTF-8; charset=UTF-8".

      • create

        public static  type,
                                        subtype)
        Creates a new media type with the given type and subtype.
        Throws:
        - if type or subtype is invalid or if a wildcard is used for the type, but not the subtype.
      • parse

        public static  input)
        Parses a media type from its string representation.
        Throws:
        - if the input is not parsable
      • equals

        public boolean   obj)
        Description copied from class: 
        Indicates whether some other object is "equal to" this one.

        The equals method implements an equivalence relation on non-null object references:

        • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
        • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
        • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
        • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
        • For any non-null reference value x, x.equals(null) should return false.

        The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

        Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

        Overrides:
         in class 
        Parameters:
        obj - the reference object with which to compare.
        Returns:
        true if this object is the same as the obj argument; false otherwise.
        See Also:
        ,
      • hashCode

        public int hashCode()
        Description copied from class: 
        Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by .

        The general contract of hashCode is:

        • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
        • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
        • It is not required that if two objects are unequal according to the method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

        As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (The hashCode may or may not be implemented as some function of an object's memory address at some point in time.)

        Overrides:
         in class 
        Returns:
        a hash code value for this object.
        See Also:
        ,
      • toString

        public  toString()
        Returns the string representation of this media type in the format described in .
        Overrides:
         in class 
        Returns:
        a string representation of the object.