Module methanol

Class MediaType

java.lang.Object
com.github.mizosoft.methanol.MediaType

public final class MediaType extends Object
A MIME type. The text representation of this class can be used as the value of the Content-Type HTTP header.

A MediaType also defines a media range. A media range has either both wildcard type and subtype, both concrete type and subtype, or a concrete type and a wildcard subtype (but not a wildcard type and a concrete subtype), with the character * denoting a wildcard. Inclusion in media ranges can be tested using any of includes(MediaType) or isCompatibleWith(MediaType), with the latter being symmetric among operands.

Case insensitive attributes such as the type, subtype, parameter names or the value of the charset parameter are converted into lower-case.

  • Field Details

    • ANY

      public static final MediaType ANY
      */*
    • APPLICATION_ANY

      public static final MediaType APPLICATION_ANY
      application/*
    • IMAGE_ANY

      public static final MediaType IMAGE_ANY
      image/*
    • TEXT_ANY

      public static final MediaType TEXT_ANY
      text/*
    • APPLICATION_FORM_URLENCODED

      public static final MediaType APPLICATION_FORM_URLENCODED
      application/x-www-form-urlencoded
    • APPLICATION_JSON

      public static final MediaType APPLICATION_JSON
      application/json
    • APPLICATION_OCTET_STREAM

      public static final MediaType APPLICATION_OCTET_STREAM
      application/octet-stream
    • APPLICATION_XHTML_XML

      public static final MediaType APPLICATION_XHTML_XML
      application/xhtml+xml
    • APPLICATION_XML

      public static final MediaType APPLICATION_XML
      application/xml
    • APPLICATION_X_PROTOBUF

      public static final MediaType APPLICATION_X_PROTOBUF
      application/x-protobuf
    • IMAGE_GIF

      public static final MediaType IMAGE_GIF
      image/gif
    • IMAGE_JPEG

      public static final MediaType IMAGE_JPEG
      image/jpeg
    • IMAGE_PNG

      public static final MediaType IMAGE_PNG
      image/png
    • TEXT_HTML

      public static final MediaType TEXT_HTML
      text/html
    • TEXT_MARKDOWN

      public static final MediaType TEXT_MARKDOWN
      text/markdown
    • TEXT_PLAIN

      public static final MediaType TEXT_PLAIN
      text/plain
    • TEXT_XML

      public static final MediaType TEXT_XML
      text/xml
  • Method Details

    • type

      public String type()
      Returns the general type.
    • subtype

      public String subtype()
      Returns the subtype.
    • parameters

      public Map<String,String> parameters()
      Returns an immutable map representing the parameters.
    • charset

      public Optional<Charset> charset()
      Returns an Optional representing the value of the charset parameter. An empty Optional is returned if no such parameter exists.
      Throws:
      IllegalCharsetNameException - if a charset parameter exists the value of which is invalid
      UnsupportedCharsetException - if a charset parameter exists the value of which is not supported in this JVM
    • charsetOrDefault

      public Charset charsetOrDefault(Charset defaultCharset)
      Returns either the value of the charset parameter or the given default charset if no such parameter exists or if the charset has no support in this JVM.
      Parameters:
      defaultCharset - the charset to fall back to
      Throws:
      IllegalCharsetNameException - if a charset parameter exists the value of which is invalid
    • hasWildcard

      public boolean hasWildcard()
      Return true if this media type is */* or if it has a wildcard subtype.
    • includes

      public boolean includes(MediaType other)
      Returns whether this media type includes the given one. A media type includes the other if the former's parameters is a subset of the latter's and either the former is a wildcard type that includes the latter or both have equal concrete type and subtype.
      Parameters:
      other - the other media type
    • isCompatibleWith

      public boolean isCompatibleWith(MediaType other)
      Returns whether this media type is compatible with the given one. Two media types are compatible if either of them includes the other.
      Parameters:
      other - the other media type
    • withCharset

      public MediaType withCharset(Charset charset)
      Returns a new MediaType with this instance's type, subtype and parameters but with the name of the given charset as the value of the charset parameter.
      Parameters:
      charset - the new type's charset
    • withParameter

      public MediaType withParameter(String name, String value)
      Returns a new MediaType with this instance's type, subtype and parameters but with the value of the parameter specified by the given name set to the given value.
      Parameters:
      name - the parameter's name
      value - the parameter's value
      Throws:
      IllegalArgumentException - if the given name or value is invalid
    • withParameters

      public MediaType withParameters(Map<String,String> parameters)
      Returns a new MediaType with this instance's type, subtype and parameters but with each of the given parameters' names set to their corresponding values.
      Parameters:
      parameters - the parameters to add or replace
      Throws:
      IllegalArgumentException - if any of the given parameters is invalid
    • equals

      public boolean equals(@Nullable Object obj)
      Tests the given object for equality with this instance. true is returned if the given object is a MediaType and both instances's type, subtype and parameters are equal.
      Overrides:
      equals in class Object
      Parameters:
      obj - the object to test for equality
    • hashCode

      public int hashCode()
      Returns a hashcode for this media type.
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Returns a text representation of this media type that is compatible with the value of the Content-Type header.
      Overrides:
      toString in class Object
    • of

      public static MediaType of(String type, String subtype)
      Returns a new MediaType with the given type and subtype.
      Parameters:
      type - the general type
      subtype - the subtype
      Throws:
      IllegalArgumentException - if the given type or subtype is invalid
    • of

      public static MediaType of(String type, String subtype, Map<String,String> parameters)
      Returns a new MediaType with the given type, subtype and parameters.
      Parameters:
      type - the general type
      subtype - the subtype
      parameters - the parameters
      Throws:
      IllegalArgumentException - if the given type, subtype or any of the given parameters is invalid
    • parse

      public static MediaType parse(String value)
      Parses the given string into a MediaType instance.
      Parameters:
      value - the media type string
      Throws:
      IllegalArgumentException - if the given string is an invalid media type