## com.google.common.net
Class MediaType

    java.lang.Object
      com.google.common.net.MediaType

    @Beta
    @GwtCompatible
    @Immutable
    public final class MediaType

extends Object

Represents an Internet Media Type (also known as a MIME Type or Content Type). This class also supports the concept of media ranges defined by HTTP/1.1. 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 2045 and 2046.

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

Field Summary
`static MediaType` `ANY_APPLICATION_TYPE`
`static MediaType` `ANY_AUDIO_TYPE`
`static MediaType` `ANY_IMAGE_TYPE`
`static MediaType` `ANY_TEXT_TYPE`
`static MediaType` `ANY_TYPE`
`static MediaType` `ANY_VIDEO_TYPE`
`static MediaType` `ATOM_UTF_8`
`static MediaType` `BMP`
`static MediaType` `BZIP2`
`static MediaType` `CACHE_MANIFEST_UTF_8`
`static MediaType` `CSS_UTF_8`
`static MediaType` `CSV_UTF_8`
`static MediaType` `FORM_DATA`
`static MediaType` `GIF`
`static MediaType` `GZIP`
`static MediaType` `HTML_UTF_8`
`static MediaType` `I_CALENDAR_UTF_8`
`static MediaType` `ICO`
`static MediaType` `JAVASCRIPT_UTF_8` RFC 4329 declares this to be the correct media type for JavaScript, but `text/javascript` may be necessary in certain situations for compatibility.
`static MediaType` `JPEG`
`static MediaType` `JSON_UTF_8`
`static MediaType` `KML`
`static MediaType` `KMZ`
`static MediaType` `MBOX`
`static MediaType` `MICROSOFT_EXCEL`
`static MediaType` `MICROSOFT_POWERPOINT`
`static MediaType` `MICROSOFT_WORD`
`static MediaType` `MP4_AUDIO`
`static MediaType` `MP4_VIDEO`
`static MediaType` `MPEG_AUDIO`
`static MediaType` `MPEG_VIDEO`
`static MediaType` `OCTET_STREAM`
`static MediaType` `OGG_AUDIO`
`static MediaType` `OGG_CONTAINER`
`static MediaType` `OGG_VIDEO`
`static MediaType` `OOXML_DOCUMENT`
`static MediaType` `OOXML_PRESENTATION`
`static MediaType` `OOXML_SHEET`
`static MediaType` `OPENDOCUMENT_GRAPHICS`
`static MediaType` `OPENDOCUMENT_PRESENTATION`
`static MediaType` `OPENDOCUMENT_SPREADSHEET`
`static MediaType` `OPENDOCUMENT_TEXT`
`static MediaType` `PDF`
`static MediaType` `PLAIN_TEXT_UTF_8`
`static MediaType` `PNG`
`static MediaType` `POSTSCRIPT`
`static MediaType` `QUICKTIME`
`static MediaType` `RTF_UTF_8`
`static MediaType` `SHOCKWAVE_FLASH`
`static MediaType` `SKETCHUP`
`static MediaType` `SVG_UTF_8`
`static MediaType` `TAR`
`static MediaType` `TEXT_JAVASCRIPT_UTF_8` RFC 4329 declares `application/javascript` to be the correct media type for JavaScript, but this may be necessary in certain situations for compatibility.
`static MediaType` `TIFF`
`static MediaType` `VCARD_UTF_8`
`static MediaType` `WEBM_AUDIO`
`static MediaType` `WEBM_VIDEO`
`static MediaType` `WEBP`
`static MediaType` `WML_UTF_8`
`static MediaType` `WMV`
`static MediaType` `XHTML_UTF_8`
`static MediaType` `XML_UTF_8`
`static MediaType` `ZIP`

Method Summary
`Optional<Charset>` `charset()` Returns an optional charset for the value of the charset parameter if it is specified.
`static MediaType` `create(String type, String subtype)` Creates a new media type with the given type and subtype.
`boolean` `equals(Object obj)`
`int` `hashCode()`
`boolean` `hasWildcard()` Returns true if either the type or subtype is the wildcard.
`boolean` `is(MediaType mediaTypeRange)` Returns `true` if this instance falls within the range (as defined by the HTTP Accept header) given by the argument according to three criteria: The type of the argument is the wildcard or equal to the type of this instance.
`ImmutableListMultimap<String,String>` `parameters()` Returns a multimap containing the parameters of this media type.
`static MediaType` `parse(String input)` Parses a media type from its string representation.
`String` `subtype()` Returns the media subtype.
`String` `toString()` Returns the string representation of this media type in the format described in RFC 2045.
`String` `type()` Returns the top-level media type.
`MediaType` `withCharset(Charset charset)` Returns a new instance with the same type and subtype as this instance, with the `charset` parameter set to the `name` of the given charset.
`MediaType` `withoutParameters()` Returns a new instance with the same type and subtype as this instance, but without any parameters.
`MediaType` `withParameter(String attribute, String value)` Replaces all parameters with the given attribute with a single parameter with the given value.
`MediaType` `withParameters(Multimap<String,String> parameters)` Replaces all parameters with the given parameters.

Methods inherited from class java.lang.Object
`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

Field Detail

### ANY_TYPE

    public static final MediaType ANY_TYPE

### ANY_TEXT_TYPE

    public static final MediaType ANY_TEXT_TYPE

### ANY_IMAGE_TYPE

    public static final MediaType ANY_IMAGE_TYPE

### ANY_AUDIO_TYPE

    public static final MediaType ANY_AUDIO_TYPE

### ANY_VIDEO_TYPE

    public static final MediaType ANY_VIDEO_TYPE

### ANY_APPLICATION_TYPE

    public static final MediaType ANY_APPLICATION_TYPE

### CACHE_MANIFEST_UTF_8

    public static final MediaType CACHE_MANIFEST_UTF_8

### CSS_UTF_8

    public static final MediaType CSS_UTF_8

### CSV_UTF_8

    public static final MediaType CSV_UTF_8

### HTML_UTF_8

    public static final MediaType HTML_UTF_8

### I_CALENDAR_UTF_8

    public static final MediaType I_CALENDAR_UTF_8

### PLAIN_TEXT_UTF_8

    public static final MediaType PLAIN_TEXT_UTF_8

### TEXT_JAVASCRIPT_UTF_8

    public static final MediaType TEXT_JAVASCRIPT_UTF_8

  RFC 4329 declares `application/javascript` to be the correct media type for JavaScript, but this may be necessary in certain situations for compatibility.

### VCARD_UTF_8

    public static final MediaType VCARD_UTF_8

### WML_UTF_8

    public static final MediaType WML_UTF_8

### XML_UTF_8

    public static final MediaType XML_UTF_8

### BMP

    public static final MediaType BMP

### GIF

    public static final MediaType GIF

### ICO

    public static final MediaType ICO

### JPEG

    public static final MediaType JPEG

### PNG

    public static final MediaType PNG

### SVG_UTF_8

    public static final MediaType SVG_UTF_8

### TIFF

    public static final MediaType TIFF

### WEBP

    public static final MediaType WEBP

### MP4_AUDIO

    public static final MediaType MP4_AUDIO

### MPEG_AUDIO

    public static final MediaType MPEG_AUDIO

### OGG_AUDIO

    public static final MediaType OGG_AUDIO

### WEBM_AUDIO

    public static final MediaType WEBM_AUDIO

### MP4_VIDEO

    public static final MediaType MP4_VIDEO

### MPEG_VIDEO

    public static final MediaType MPEG_VIDEO

### OGG_VIDEO

    public static final MediaType OGG_VIDEO

### QUICKTIME

    public static final MediaType QUICKTIME

### WEBM_VIDEO

    public static final MediaType WEBM_VIDEO

### WMV

    public static final MediaType WMV

### ATOM_UTF_8

    public static final MediaType ATOM_UTF_8

### BZIP2

    public static final MediaType BZIP2

### FORM_DATA

    public static final MediaType FORM_DATA

### GZIP

    public static final MediaType GZIP

### JAVASCRIPT_UTF_8

    public static final MediaType JAVASCRIPT_UTF_8

  RFC 4329 declares this to be the correct media type for JavaScript, but `text/javascript` may be necessary in certain situations for compatibility.

### JSON_UTF_8

    public static final MediaType JSON_UTF_8

### KML

    public static final MediaType KML

### KMZ

    public static final MediaType KMZ

### MBOX

    public static final MediaType MBOX

### MICROSOFT_EXCEL

    public static final MediaType MICROSOFT_EXCEL

### MICROSOFT_POWERPOINT

    public static final MediaType MICROSOFT_POWERPOINT

### MICROSOFT_WORD

    public static final MediaType MICROSOFT_WORD

### OCTET_STREAM

    public static final MediaType OCTET_STREAM

### OGG_CONTAINER

    public static final MediaType OGG_CONTAINER

### OOXML_DOCUMENT

    public static final MediaType OOXML_DOCUMENT

### OOXML_PRESENTATION

    public static final MediaType OOXML_PRESENTATION

### OOXML_SHEET

    public static final MediaType OOXML_SHEET

### OPENDOCUMENT_GRAPHICS

    public static final MediaType OPENDOCUMENT_GRAPHICS

### OPENDOCUMENT_PRESENTATION

    public static final MediaType OPENDOCUMENT_PRESENTATION

### OPENDOCUMENT_SPREADSHEET

    public static final MediaType OPENDOCUMENT_SPREADSHEET

### OPENDOCUMENT_TEXT

    public static final MediaType OPENDOCUMENT_TEXT

### PDF

    public static final MediaType PDF

### POSTSCRIPT

    public static final MediaType POSTSCRIPT

### RTF_UTF_8

    public static final MediaType RTF_UTF_8

### SHOCKWAVE_FLASH

    public static final MediaType SHOCKWAVE_FLASH

### SKETCHUP

    public static final MediaType SKETCHUP

### TAR

    public static final MediaType TAR

### XHTML_UTF_8

    public static final MediaType XHTML_UTF_8

### ZIP

    public static final MediaType ZIP

Method Detail

### type

    public String type()

  Returns the top-level media type. For example, `"text"` in `"text/plain"`.

### subtype

    public String subtype()

  Returns the media subtype. For example, `"plain"` in `"text/plain"`.

### parameters

    public ImmutableListMultimap<String,String> parameters()

  Returns a multimap containing the parameters of this media type.

### charset

    public Optional<Charset> charset()

  Returns an optional charset for the value of the charset parameter if it is specified.

  Throws:
    `IllegalStateException` - if multiple charset values have been set for this media type
    `IllegalCharsetNameException` - if a charset value is present, but illegal
    `UnsupportedCharsetException` - 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 MediaType withParameters(Multimap<String,String> parameters)

  Replaces all parameters with the given parameters.

  Throws:
    `IllegalArgumentException` - if any parameter or value is invalid

### withParameter

    public MediaType withParameter(String attribute,
                                   String 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(com.google.common.collect.Multimap<java.lang.String, java.lang.String>)`. Prefer `withCharset(java.nio.charset.Charset)` for setting the `charset` parameter when using a `Charset` object.

  Throws:
    `IllegalArgumentException` - if either `attribute` or `value` is invalid

### withCharset

    public MediaType withCharset(Charset charset)

  Returns a new instance with the same type and subtype as this instance, with the `charset` parameter set to the `name` 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 `Charset` 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 the HTTP Accept header) 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 MediaType create(String type,
                                   String subtype)

  Creates a new media type with the given type and subtype.

  Throws:
    `IllegalArgumentException` - if type or subtype is invalid or if a wildcard is used for the type, but not the subtype.

### parse

    public static MediaType parse(String input)

  Parses a media type from its string representation.

  Throws:
    `IllegalArgumentException` - if the input is not parsable

### equals

    public boolean equals(@Nullable
                          Object obj)

  Overrides:
    `equals` in class `Object`

### hashCode

    public int hashCode()

  Overrides:
    `hashCode` in class `Object`

### toString

    public String toString()

  Returns the string representation of this media type in the format described in RFC 2045.

  Overrides:
    `toString` in class `Object`

Copyright © 2010-2012. All Rights Reserved.