@Beta @GwtCompatible @Immutable public final class MediaType extends Object
*
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()
.
Modifier and Type | Method and Description |
---|---|
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)
Indicates whether some other object is "equal to" this one.
|
int |
hashCode()
Returns a hash code value for the object.
|
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.
|
public static final MediaType ANY_TEXT_TYPE
public static final MediaType ANY_IMAGE_TYPE
public static final MediaType ANY_AUDIO_TYPE
public static final MediaType ANY_VIDEO_TYPE
public static final MediaType ANY_APPLICATION_TYPE
public static final MediaType CACHE_MANIFEST_UTF_8
public static final MediaType HTML_UTF_8
public static final MediaType I_CALENDAR_UTF_8
public static final MediaType PLAIN_TEXT_UTF_8
public static final MediaType TEXT_JAVASCRIPT_UTF_8
application/javascript
to be the correct media type for JavaScript,
but this may be necessary in certain situations for compatibility.public static final MediaType VCARD_UTF_8
public static final MediaType XML_UTF_8
text/xml
) is used for XML documents that are "readable by casual users."
APPLICATION_XML_UTF_8
is provided for documents that are intended for applications.public static final MediaType CRW
crw
files), a widely-used "raw image" format for cameras. It is
found in /etc/mime.types
, e.g. in public static final MediaType PSD
psd
files) as defined by IANA, and found in
/etc/mime.types
, e.g. of the Apache
HTTPD project; for the specification, see
This is a more recent replacement for the older, experimental type
x-photoshop
: RFC-2046.6.
public static final MediaType MPEG_AUDIO
public static final MediaType WEBM_AUDIO
public static final MediaType MPEG_VIDEO
public static final MediaType WEBM_VIDEO
public static final MediaType APPLICATION_XML_UTF_8
public static final MediaType ATOM_UTF_8
public static final MediaType EOT
public static final MediaType EPUB
public static final MediaType KEY_ARCHIVE
public static final MediaType APPLICATION_BINARY
OCTET_STREAM
for binary data that is not being served to a browser.public static final MediaType JAVASCRIPT_UTF_8
text/javascript
may be
necessary in certain situations for compatibility.public static final MediaType JSON_UTF_8
public static final MediaType APPLE_MOBILE_CONFIG
public static final MediaType MICROSOFT_EXCEL
public static final MediaType MICROSOFT_POWERPOINT
public static final MediaType MICROSOFT_WORD
public static final MediaType OCTET_STREAM
public static final MediaType OGG_CONTAINER
public static final MediaType OOXML_DOCUMENT
public static final MediaType OOXML_PRESENTATION
public static final MediaType OOXML_SHEET
public static final MediaType OPENDOCUMENT_GRAPHICS
public static final MediaType OPENDOCUMENT_PRESENTATION
public static final MediaType OPENDOCUMENT_SPREADSHEET
public static final MediaType OPENDOCUMENT_TEXT
public static final MediaType POSTSCRIPT
public static final MediaType RDF_XML_UTF_8
public static final MediaType SFNT
public static final MediaType SHOCKWAVE_FLASH
public static final MediaType WOFF
public static final MediaType XHTML_UTF_8
public static final MediaType XRD_UTF_8
public ImmutableListMultimap<String,String> parameters()
public Optional<Charset> charset()
IllegalStateException
- if multiple charset values have been set for this media typeIllegalCharsetNameException
- if a charset value is present, but illegalUnsupportedCharsetException
- if a charset value is present, but no support is available
in this instance of the Java virtual machinepublic MediaType withoutParameters()
public MediaType withParameters(Multimap<String,String> parameters)
IllegalArgumentException
- if any parameter or value is invalidpublic MediaType withParameter(String attribute, String value)
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.IllegalArgumentException
- if either attribute
or value
is invalidpublic MediaType withCharset(Charset charset)
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)
.
public boolean hasWildcard()
public boolean is(MediaType mediaTypeRange)
true
if this instance falls within the range (as defined by
the HTTP Accept header)
given by the argument according to three criteria:
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"
.
public static MediaType create(String type, String subtype)
IllegalArgumentException
- if type or subtype is invalid or if a wildcard is used for the
type, but not the subtype.public static MediaType parse(String input)
IllegalArgumentException
- if the input is not parsablepublic boolean equals(@Nullable Object obj)
java.lang.Object
The equals
method implements an equivalence relation
on non-null object references:
x
, x.equals(x)
should return
true
.
x
and y
, x.equals(y)
should return true
if and only if
y.equals(x)
returns true
.
x
, y
, and z
, if
x.equals(y)
returns true
and
y.equals(z)
returns true
, then
x.equals(z)
should return true
.
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.
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.
equals
in class Object
obj
- the reference object with which to compare.true
if this object is the same as the obj
argument; false
otherwise.Object.hashCode()
,
HashMap
public int hashCode()
java.lang.Object
HashMap
.
The general contract of hashCode
is:
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.
equals(Object)
method, then calling the hashCode
method on each of
the two objects must produce the same integer result.
Object.equals(java.lang.Object)
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. (This is typically implemented by converting the internal
address of the object into an integer, but this implementation
technique is not required by the
JavaTM programming language.)
hashCode
in class Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
Copyright © 2010-2014. All Rights Reserved.