Package com.globalmentor.net
Class MediaType
java.lang.Object
com.globalmentor.net.MediaType
An encapsulation of an Internet media content type as previously defined in RFC 2046: MIME Part 2: Media
Types; and most recently in RFC 6838: Media Type Specifications and Registration
Procedures. The full syntax for a media type and its parameters are found in RFC 2045: MIME
Part 1: Format of Internet Message Bodies
- Author:
- Garret Wilson
- API Note:
- Neither
javax.activation.MimeType
norjavax.mail.internet.ContentType
correctly implementsObject.equals(Object)
and therefore cannot reliably be used in sets and maps. Furthermore,javax.mail.internet.ContentType
as of JDK 6 is not included in default JDK distributions.javax.activation.MimeType
was recently added to JDK distributions, so in an earlier implementation this class provided appropriate factory methods, to provide specialjavax.activation.MimeType
instances that provide correct equality checking. Because thejavax.activation
package is not included in the Android Development Kit, however, and seeing that neitherjavax.activation.MimeType
norjavax.mail.internet.ContentType
are in common use, the current implementation provides a fully independent version., Internet media types are currently governed by RFC 6838: Media Type Specifications and Registration Procedures. RFC 6532: Internationalized Email Headers § 3.2. Syntax Extensions to RFC 5322 extends extends the syntax to support UTF-8. According to the WhatWG, a quoted string follows RFC 7230: Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing § 3.2.6. Field Value Components. - Implementation Specification:
- This class normalizes type, subtype, and parameter names, which are ASCII case-insensitive, to ASCII lowercase; along with the
charset
parameter value. All other parameter values are left as-is. All other parameter values that are case-insensitive should be passed as lowercase to ensure correct equality comparisons., This implementation does not support parameter values containing control characters except for the horizontal tab character, permitted by RFC 7230 § 3.2.6.., This implementation supports empty parameter values if they are quoted. - Implementation Note:
- Compare this implementation to that of Guava's
com.google.common.net.MediaType
which, in addition to normalizing type, subtype, and parameter names to lowercase, also normalizes the value of thecharset
attribute to lowercase. Note also that RFC 7231 § 3.1.1.1. Media Type indicates that a lowercase form ofcharset
value is preferred, e.g.text/html;charset=utf-8
. In addition RFC 2046 § 4.1.2. Charset Parameter indicates that if non-text
types specify acharset
value, "the same syntax and values should be used". - See Also:
-
- RFC 2045: Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies
- RFC 2046: Multipurpose Internet Mail Extensions (MIME) Part Two: Media Types
- RFC 6838: Media Type Specifications and Registration Procedures
- RFC 7231: Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content § 3.1.1.1. Media Type
- IANA Media Types
- XHTML Media Types
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
A media type parameter name/value pair. -
Field Summary
Modifier and TypeFieldDescriptionstatic final MediaType
The sharedapplication/octet-stream
media type.static final String
Theapplication
top-level type.static final String
Theaudio
top-level type.static final String
The character set parameters.static final String
The pseudo top-level type used by JavaURLConnection
to indicate unknown content bycontent/unknown
.static final char
The character delimiting a facet name from the rest of the name as per RFC 6838.static final Characters
The characters of RFC 2045 which are considered illegal in tokens such as non-quoted strings.static final String
Theimage
top-level type.static final String
Themessage
top-level type.static final String
Themultipart
top-level type.static final String
A stream of bytes.static final char
The character used to assign parameter values.static final char
The delimiter character separating parameters from the base media type and from each other.static final Pattern
A pattern for checking the basic form of a parameter, including the ';' delimiter that precedes and separates each parameter.static final Pattern
A pattern for checking the basic form of a parameter, including the ';' delimiter that precedes and separates each parameter.static final int
The parameters pattern matching group for the parameter name.static final int
The parameters pattern matching group for the parameter name.static final Pattern
A pattern for checking the basic form of regular expressions.static final int
The pattern matching group for all the parameters, with delimiters.static final int
The pattern matching group for the primary type.static final int
The pattern matching group for the subtype.static final Characters
The control characters not allowed in a quoted string as per RFC 7230.static final Characters
Characters required to be escaped in a quoted string.static final Characters
Therestricted-name-chars
characters of RFC 6838, making up arestricted-name
.static final byte
The maximum number ofrestricted-name-chars
of arestricted-name
according to RFC 6838.static final Characters
Therestricted-name-first
characters of RFC 6838, defining the first character of arestricted-name
.static final byte
The maximum length of arestricted-name
according to RFC 6838.static final Pattern
The regular expression pattern defining arestricted-name
as per RFC 6838.static final Characters
Thetspecials
characters of RFC 2045, which require a string to be quoted in a parameter value.static final char
The character for escaping a quoted string.static final char
The character for quoting a string, such as a parameter value with special characters.static final String
The separator character that begins a non-standard extension type.static final char
The separator character that delimits a subtype suffix.static final String
Thetext
top-level type.static final char
The divider character for media type strings.static final char
The wildcard character.static final String
The pseudo subtype used by JavaURLConnection
to indicate unknown content bycontent/unknown
.static final String
Thevideo
top-level type.static final String
The wildcard subtype, matching any subtype.static final String
A Java object. -
Method Summary
Modifier and TypeMethodDescriptionstatic <C extends CharSequence>
CcheckArgumentRestrictedName
(C input) Confirms that the given input conforms to the rules forrestricted-name
according to RFC 6838, returning the given input.static MediaType
create
(CharSequence charSequence) Deprecated.static MediaType
create
(String primaryType, String subType, MediaType.Parameter... parameters) Deprecated.in favor ofof(String, String, Parameter...)
; to be removed in next major version.static String
createSubTypeSuffix
(String... suffixes) Creates a media type suffix by prepending '+' to each suffix and concatenating the suffixes.boolean
Convenience method for returning the "charset" parameter.Deprecated.to be removed in favor oftoBaseType()
.Deprecated.to be removed in favor oftoBaseTypeString()
.getParameter
(String name) Retrieve the parameter value associated with the given parameter name.boolean
hasBaseType
(MediaType mediaType) Checks to see if the given media type has the same primary type and subtype as this media type.boolean
hasBaseType
(String primaryType, String subType) Matches a media type against a primary type and subtype with no wildcard support.int
hashCode()
boolean
hasSubTypeSuffix
(String... suffixes) Determines if the subtype of the media type has the given suffix.boolean
Deprecated.to be replaced withmatches(String, String)
.boolean
matches
(CharSequence text) Matches a media type against a primary type and subtype string representation.boolean
Matches a media type against the "application" primary type and "x-java-object" subtype, with a "class" parameter indicating the given object class.boolean
Matches a media type against a primary type and subtype.boolean
Matches a media type against a primary type and subtype, with a class parameter indicating the given object class.static MediaType
of
(CharSequence charSequence) Deprecated.in favor ofparse(CharSequence)
; to be removed in next major version.static MediaType
of
(String primaryType, String subType, MediaType.Parameter... parameters) Returns a media type object from primary type, a subtype, and optional parameters.static MediaType
of
(String primaryType, String subType, Set<MediaType.Parameter> parameters) Returns a media type object from primary type, a subtype, and optional parameters.static MediaType
parse
(CharSequence text) Parses a media type object from a sequence of characters.static Set<MediaType.Parameter>
parseParameters
(CharSequence text) Parses parameters of a media type from a sequence of characters.Returns an instance of the "base type"; that is, the media type with no parameters.Returns a string representation of the base type; that is, the same primary and subtype as the media type, but with no parameterstoString()
toString
(boolean formatted) Returns a possibly formatted string version of the given media type.static String
toString
(String primaryType, String subType, MediaType.Parameter... parameters) Constructs a string representing a media type.static String
toString
(String primaryType, String subType, Iterable<MediaType.Parameter> parameters) Constructs a string representing a media type in canonical form with no extra whitespace or other formatting.static String
toString
(String primaryType, String subType, Iterable<MediaType.Parameter> parameters, boolean formatted) Constructs a string representing a media type.withCharset
(Charset charset) Returns a media type with the given charset as a "charset" parameter, replacing any existing parameter with the same name.withParameter
(MediaType.Parameter newParameter) Returns a media type with the given parameter, replacing any existing parameter with the same name.withParameter
(String name, String value) Returns a media type with the given parameter, replacing any existing parameter with the same name.
-
Field Details
-
TYPE_DIVIDER
public static final char TYPE_DIVIDERThe divider character for media type strings.- See Also:
-
PARAMETER_DELIMITER_CHAR
public static final char PARAMETER_DELIMITER_CHARThe delimiter character separating parameters from the base media type and from each other.- See Also:
-
PARAMETER_ASSIGNMENT_CHAR
public static final char PARAMETER_ASSIGNMENT_CHARThe character used to assign parameter values.- See Also:
-
STRING_QUOTE_CHAR
public static final char STRING_QUOTE_CHARThe character for quoting a string, such as a parameter value with special characters.- See Also:
-
STRING_ESCAPE_CHAR
public static final char STRING_ESCAPE_CHARThe character for escaping a quoted string.- See Also:
-
FACET_DELIMITER_CHAR
public static final char FACET_DELIMITER_CHARThe character delimiting a facet name from the rest of the name as per RFC 6838.- See Also:
-
SUBTYPE_EXTENSION_PREFIX
The separator character that begins a non-standard extension type.- See Also:
-
SUBTYPE_SUFFIX_DELIMITER_CHAR
public static final char SUBTYPE_SUFFIX_DELIMITER_CHARThe separator character that delimits a subtype suffix.- See Also:
-
TYPE_WILDCARD_CHAR
public static final char TYPE_WILDCARD_CHARThe wildcard character.- See Also:
-
WILDCARD_SUBTYPE
The wildcard subtype, matching any subtype. -
RESTRICTED_NAME_FIRST_CHARACTERS
Therestricted-name-first
characters of RFC 6838, defining the first character of arestricted-name
. -
RESTRICTED_NAME_CHARACTERS
Therestricted-name-chars
characters of RFC 6838, making up arestricted-name
. -
RESTRICTED_NAME_CHARS_MAX_LENGTH
public static final byte RESTRICTED_NAME_CHARS_MAX_LENGTHThe maximum number ofrestricted-name-chars
of arestricted-name
according to RFC 6838.- See Also:
-
RESTRICTED_NAME_MAX_LENGTH
public static final byte RESTRICTED_NAME_MAX_LENGTHThe maximum length of arestricted-name
according to RFC 6838.- See Also:
-
RESTRICTED_NAME_PATTERN
The regular expression pattern defining arestricted-name
as per RFC 6838. -
SPECIAL_CHARACTERS
Thetspecials
characters of RFC 2045, which require a string to be quoted in a parameter value. -
ILLEGAL_TOKEN_CHARACTERS
The characters of RFC 2045 which are considered illegal in tokens such as non-quoted strings.- Implementation Specification:
- This currently does not include the control characters other than the horizontal tab. A future version may detect this using the
\p{Cntrl}
character class; for now the implementation checks manually and forbids control characters except for tab. - See Also:
-
QUOTED_STRING_PROHIBITED_CONTROL_CHARACTERS
The control characters not allowed in a quoted string as per RFC 7230.- See Also:
-
QUOTED_STRING_REQUIRED_ESCAPED_CHARACTERS
Characters required to be escaped in a quoted string. -
PARAMETER_PATTERN
A pattern for checking the basic form of a parameter, including the ';' delimiter that precedes and separates each parameter. The pattern may be repeated. The two matching groups are the name and value. -
PARAMETER_ITERATE_PATTERN
A pattern for checking the basic form of a parameter, including the ';' delimiter that precedes and separates each parameter. Suitable for iterating through parameters usingMatcher.find()
. The two matching groups are the name and value. -
PARAMETER_PATTERN_NAME_GROUP
public static final int PARAMETER_PATTERN_NAME_GROUPThe parameters pattern matching group for the parameter name.- See Also:
-
PARAMETER_PATTERN_VALUE_GROUP
public static final int PARAMETER_PATTERN_VALUE_GROUPThe parameters pattern matching group for the parameter name.- See Also:
-
PATTERN
A pattern for checking the basic form of regular expressions. This pattern does not take into account all aspects of a regular expression, e.g. special characters. The parameters group will benull
if there are no parameters at all. -
PATTERN_PRIMARY_TYPE_GROUP
public static final int PATTERN_PRIMARY_TYPE_GROUPThe pattern matching group for the primary type.- See Also:
-
PATTERN_SUBTYPE_GROUP
public static final int PATTERN_SUBTYPE_GROUPThe pattern matching group for the subtype.- See Also:
-
PATTERN_PARAMETERS_GROUP
public static final int PATTERN_PARAMETERS_GROUPThe pattern matching group for all the parameters, with delimiters.- See Also:
-
TEXT_PRIMARY_TYPE
Thetext
top-level type.- See Also:
-
IMAGE_PRIMARY_TYPE
Theimage
top-level type.- See Also:
-
AUDIO_PRIMARY_TYPE
Theaudio
top-level type.- See Also:
-
VIDEO_PRIMARY_TYPE
Thevideo
top-level type.- See Also:
-
APPLICATION_PRIMARY_TYPE
Theapplication
top-level type.- See Also:
-
MULTIPART_PRIMARY_TYPE
Themultipart
top-level type.- See Also:
-
MESSAGE_PRIMARY_TYPE
Themessage
top-level type.- See Also:
-
CONTENT_PRIMARY_TYPE
The pseudo top-level type used by JavaURLConnection
to indicate unknown content bycontent/unknown
.- See Also:
-
CHARSET_PARAMETER
The character set parameters.- See Also:
-
UNKNOWN_SUBTYPE
The pseudo subtype used by JavaURLConnection
to indicate unknown content bycontent/unknown
.- See Also:
-
OCTET_STREAM_SUBTYPE
A stream of bytes.- See Also:
-
X_JAVA_OBJECT
A Java object.- See Also:
-
APPLICATION_OCTET_STREAM_MEDIA_TYPE
The sharedapplication/octet-stream
media type.
-
-
Method Details
-
checkArgumentRestrictedName
Confirms that the given input conforms to the rules forrestricted-name
according to RFC 6838, returning the given input.- API Note:
- This method is useful for checking a type, subtype, or parameter name.
- Type Parameters:
C
- The type of character sequence input.- Parameters:
input
- The character sequence to check.- Returns:
- The given input.
- Throws:
NullPointerException
- if the given input isnull
.IllegalArgumentException
- if the given input does not conform to the rules forrestricted-name
.- See Also:
-
getPrimaryType
- Returns:
- The primary type of the media type.
-
getSubType
- Returns:
- The subtype of the media type.
-
getParameters
- Returns:
- The set of parameters, which may be empty, but will never be
null
.
-
findCharset
Convenience method for returning the "charset" parameter. If there are multiple "charset" parameters, it is undefined which one is used to return the value.- Returns:
- The charset specified by one of the "charset" parameters, if any.
- Throws:
UnsupportedCharsetException
- If no support for the named charset is available in this instance of the Java virtual machine
-
create
Deprecated.in favor ofparse(CharSequence)
; to be removed in next major version.Parses a media type object from a sequence of characters.- Implementation Specification:
- The primary type, subtype, and parameter names, if any, are each normalized to lowercase. The value of the "charset" parameter, if present, is normalized to lowercase.
- Parameters:
charSequence
- The character sequence representation of the media type.- Returns:
- A new media type object parsed from the string.
- Throws:
IllegalArgumentException
- if the primary type, subtype, and/or a parameter name does not conform to theRESTRICTED_NAME_PATTERN
pattern.
-
of
Deprecated.in favor ofparse(CharSequence)
; to be removed in next major version.Parses a media type object from a sequence of characters.- Implementation Specification:
- The primary type, subtype, and parameter names, if any, are each normalized to lowercase. The value of the "charset" parameter, if present, is normalized to lowercase.
- Parameters:
charSequence
- The character sequence representation of the media type.- Returns:
- A new media type object parsed from the string.
- Throws:
IllegalArgumentException
- if the name does not conform to theRESTRICTED_NAME_PATTERN
pattern.IllegalArgumentException
- if the primary type, subtype, and/or a parameter name does not conform to theRESTRICTED_NAME_PATTERN
pattern.
-
create
@Deprecated public static MediaType create(String primaryType, String subType, MediaType.Parameter... parameters) Deprecated.in favor ofof(String, String, Parameter...)
; to be removed in next major version.Creates a media type object from primary type, a subtype, and optional parameters.- Implementation Specification:
- The primary type and subtype are each normalized to lowercase.
- Parameters:
primaryType
- The primary type.subType
- The subtype.parameters
- Optional name-value pairs representing parameters of the media type.- Returns:
- A new media type object constructed from the given information.
- Throws:
IllegalArgumentException
- if the primary type and/or subtype does not conform to theRESTRICTED_NAME_PATTERN
pattern.
-
of
Returns a media type object from primary type, a subtype, and optional parameters.- Implementation Specification:
- The primary type and subtype are each normalized to lowercase.
- Parameters:
primaryType
- The primary type.subType
- The subtype.parameters
- Optional name-value pairs representing parameters of the media type.- Returns:
- A new media type object constructed from the given information.
- Throws:
IllegalArgumentException
- if the primary type and/or subtype does not conform to theRESTRICTED_NAME_PATTERN
pattern.
-
of
Returns a media type object from primary type, a subtype, and optional parameters.- Implementation Specification:
- The primary type and subtype are each normalized to lowercase.
- Parameters:
primaryType
- The primary type.subType
- The subtype.parameters
- Zero or more name-value pairs representing parameters of the media type.- Returns:
- A new media type object constructed from the given information.
- Throws:
NullPointerException
- if the given parameters set isnull
.IllegalArgumentException
- if the primary type and/or subtype does not conform to theRESTRICTED_NAME_PATTERN
pattern.
-
parse
Parses a media type object from a sequence of characters.- Implementation Specification:
- The primary type, subtype, and parameter names, if any, are each normalized to lowercase. The value of the "charset" parameter, if present, is normalized to lowercase.
- Parameters:
text
- The character sequence representation of the media type.- Returns:
- A new media type object parsed from the string.
- Throws:
IllegalArgumentException
- if the primary type, subtype, and/or a parameter name does not conform to theRESTRICTED_NAME_PATTERN
pattern.
-
parseParameters
Parses parameters of a media type from a sequence of characters.- Implementation Specification:
- The parameter names are each normalized to lowercase. The value of the "charset" parameter, if present, is normalized to lowercase.
- Parameters:
text
- The character sequence representing the parameters of the media type, not including the ';' delimiter.- Returns:
- Media type parameters parsed from the string.
- Throws:
IllegalArgumentException
- if the string is not syntactically correct parameters, or if a parameter name does not conform to theRESTRICTED_NAME_PATTERN
pattern.
-
getParameter
Retrieve the parameter value associated with the given parameter name. Names are comparisons are ASCII case-insensitive.- Parameters:
name
- The name of the parameter.- Returns:
- The (always unquoted) value associated with the given name, or
null
if there is no parameter with the given name. - Throws:
NullPointerException
- if the given parameter name isnull
.
-
matches
Matches a media type against a primary type and subtype string representation. Comparisons are ASCII case-insensitive. This method supports wildcard subtypes.- Parameters:
text
- The primary type and subtype with which to compare the media type, such astext/plain
orimage/*
.- Returns:
true
if the media type has a primary type and a subtype matching those given.- Throws:
IllegalArgumentException
- if the given match type is not in the correct format, such as missing a type separator slash.- See Also:
-
matches
Matches a media type against a primary type and subtype. Comparisons are ASCII case-insensitive. This method supports wildcard subtypes.- Parameters:
primaryType
- The primary type with which to compare the media type.subType
- The subtype with which to compare the media type.- Returns:
true
if the media type has a primary type and a subtype matching those given.- See Also:
-
match
Deprecated.to be replaced withmatches(String, String)
.Matches a media type against a primary type and subtype. Comparisons are ASCII case-insensitive. This method supports wildcard subtypes.- Parameters:
primaryType
- The primary type with which to compare the media type.subType
- The subtype with which to compare the media type.- Returns:
true
if the media type has a primary type and a subtype matching those given.- See Also:
-
matches
public boolean matches(@Nonnull String primaryType, @Nonnull String subType, @Nonnull Class<?> objectClass) Matches a media type against a primary type and subtype, with a class parameter indicating the given object class. This method supports wildcard subtypes.- Parameters:
primaryType
- The primary type with which to compare the media type.subType
- The subtype with which to compare the media type.objectClass
- The class for which to check in the parameters under the key "class".- Returns:
true
if the media type has the same primary type and subtype as that given, along with a class parameter.- See Also:
-
matches
Matches a media type against the "application" primary type and "x-java-object" subtype, with a "class" parameter indicating the given object class. This method supports wildcard subtypes.- Parameters:
objectClass
- The class for which to check in the parameters under the key "class".- Returns:
true
if the media type has the same primary type and subtype as that given, along with a class parameter.- See Also:
-
getBaseType
Deprecated.to be removed in favor oftoBaseTypeString()
.- Returns:
- A string representation of the the base media type, that is, the same primary and subtype as the media type, but with no parameters.
-
getBaseContentType
Deprecated.to be removed in favor oftoBaseType()
.Determines the base media type, with no parameters, of the media type. Useful for making comparisons or for storing in canonical form in a hash table. If this media type is already a base media type, this media type is returned.- Returns:
- A media type with the same primary and subtype as the media type, but with no parameters.
-
toBaseType
Returns an instance of the "base type"; that is, the media type with no parameters.- API Note:
- The base type is useful for making comparisons or for storing in canonical form in a hash table.
- Implementation Specification:
- If this media type is already a base media type, this media type is returned.
- Returns:
- A media type with the same primary and subtype as the media type, but with no parameters.
- See Also:
-
hasBaseType
Checks to see if the given media type has the same primary type and subtype as this media type. The parameters of the this media type and the given media type are ignored. This method does not support wildcards.- Parameters:
mediaType
- The media type with which to compare this media type.- Returns:
true
if the primary types and base types of the two media types are equal.- See Also:
-
hasBaseType
Matches a media type against a primary type and subtype with no wildcard support. Comparisons are ASCII case-insensitive.- Parameters:
primaryType
- The primary type with which to compare the media type.subType
- The subtype with which to compare the media type.- Returns:
true
if the media type has the same primary type and subtype as that given.- Throws:
NullPointerException
- if the primary type and/or subtype isnull
.- See Also:
-
hasSubTypeSuffix
Determines if the subtype of the media type has the given suffix.- Implementation Specification:
- Suffixes are compared on an ASCII case-insensitive basis.
- Implementation Note:
- This implementation restricts each suffix to the
RESTRICTED_NAME_PATTERN
pattern, the same pattern applicable to a subtype, although this requirement is not explicit in RFC 6838. - Parameters:
suffixes
- The suffix strings that will be checked, after they are combined into a single suffix, each part prepended with '+'.- Returns:
true
if the media type's subtype has the given suffixes.- Throws:
IllegalArgumentException
- if a suffix does not conform to theRESTRICTED_NAME_PATTERN
pattern, including if it already begins with the '+' delimiter.
-
withParameter
Returns a media type with the given parameter, replacing any existing parameter with the same name. If this media type already has the given parameter, this media type will be returned. If this media type has a parameter with the same name but with a different value, the parameter will be replaced with the one given. Otherwise, the parameter will be added to the parameters.- Implementation Specification:
- Parameter name comparisons are ASCII case-insensitive. The comparison of the "charset" parameter value is performed in an ASCII case-insensitive manner. All other parameter values are compared with case sensitivity.
- Parameters:
newParameter
- The new parameter to add or replace.- Returns:
- A media type with the given parameter.
-
withParameter
Returns a media type with the given parameter, replacing any existing parameter with the same name. If this media type already has the given parameter, this media type will be returned. If this media type has a parameter with the same name but with a different value, the parameter will be replaced with the one given. Otherwise, the parameter will be added to the parameters.- Implementation Specification:
- Parameter name comparisons are ASCII case-insensitive. The comparison of the "charset" parameter value is performed in an ASCII
case-insensitive manner. All other parameter values are compared with case sensitivity., This implementation delegates to
withParameter(Parameter)
. - Parameters:
name
- The parameter name to add or replace.value
- The parameter value.- Returns:
- A media type with the given parameter.
- Throws:
NullPointerException
- if the given name and/or value isnull
.IllegalArgumentException
- if the parameter name does not conform to theRESTRICTED_NAME_PATTERN
pattern.- See Also:
-
withCharset
Returns a media type with the given charset as a "charset" parameter, replacing any existing parameter with the same name. If this media type already has the given parameter, this media type will be returned. If this media type has a parameter with the same name but with a different value, the parameter will be replaced with the one given. Otherwise, the parameter will be added to the parameters.- Implementation Specification:
- Parameter name comparisons are ASCII case-insensitive. The comparison of the "charset" parameter value is performed in an ASCII
case-insensitive manner., This implementation delegates to
withParameter(String, String)
. - Parameters:
charset
- The charset value to add or replace.- Returns:
- A media type with the given parameter.
- Throws:
NullPointerException
- if the given charset isnull
.- See Also:
-
hashCode
public int hashCode()- Overrides:
hashCode
in classObject
- Implementation Specification:
- This implementation returns the hash code of the primary type, the subtype, the parameter names, and the "charset" parameter value in a case insensitive manner, as these items have already been normalized by this class.
- Returns:
- A hash code value for this object.
- See Also:
-
equals
- Overrides:
equals
in classObject
- Implementation Specification:
- This implementation considers an object equal if it is another
MediaType
with the same primary types and subtypes, the same number of parameters, and a matching parameter value for every parameter of this media type. Comparisons are ASCII case-insensitive for the primary type, the subtype, the parameter names, and the "charset" parameter value, as these items have already been normalized by this class. - Parameters:
object
- The reference object with which to compare.- See Also:
-
toString
Returns a possibly formatted string version of the given media type.- Parameters:
formatted
- Whether the resulting string should be formatted with extra whitespace for human readability.- Returns:
- The canonical representation of the media type according to RFC 6838.
-
toString
-
toBaseTypeString
Returns a string representation of the base type; that is, the same primary and subtype as the media type, but with no parameters- API Note:
- Compare with
javax.activiation.MimeType.getBaseType()
. - Returns:
- A string representation of the media type with no parameters.
- See Also:
-
toString
public static String toString(@Nonnull String primaryType, @Nonnull String subType, MediaType.Parameter... parameters) Constructs a string representing a media type.- Parameters:
primaryType
- The primary type.subType
- The subtype.parameters
- Optional name-value pairs representing parameters of the media type.- Returns:
- A string representing the type in the form "primaryType/subType[;parameters]".
-
toString
public static String toString(@Nonnull String primaryType, @Nonnull String subType, @Nonnull Iterable<MediaType.Parameter> parameters) Constructs a string representing a media type in canonical form with no extra whitespace or other formatting. The parameters will be placed in the string in iteration order.- Parameters:
primaryType
- The primary type.subType
- The subtype.parameters
- Any name-value pairs representing parameters of the media type.- Returns:
- A string representing the type in the form "primaryType/subType[;parameters]".
- Throws:
NullPointerException
- if the given parameters set isnull
.
-
toString
public static String toString(@Nonnull String primaryType, @Nonnull String subType, @Nonnull Iterable<MediaType.Parameter> parameters, boolean formatted) Constructs a string representing a media type. The parameters will be placed in the string in iteration order.- Parameters:
primaryType
- The primary type.subType
- The subtype.parameters
- Any name-value pairs representing parameters of the media type.formatted
- Whether the resulting string should be formatted with extra whitespace for human readability.- Returns:
- A string representing the type in the form "primaryType/subType[;parameters]".
- Throws:
NullPointerException
- if the given parameters set isnull
.- See Also:
-
createSubTypeSuffix
Creates a media type suffix by prepending '+' to each suffix and concatenating the suffixes.- Implementation Specification:
- Each suffix is normalized to lowercase.
- Implementation Note:
- This implementation restricts each suffix to the
RESTRICTED_NAME_PATTERN
pattern, the same pattern applicable to a subtype, although this requirement is not explicit in RFC 6838. - Parameters:
suffixes
- The suffix strings to combine into a suffix.- Returns:
- A suffix composed of the given suffix strings.
- Throws:
IllegalArgumentException
- if a suffix does not conform to theRESTRICTED_NAME_PATTERN
pattern, including if it already begins with the '+' delimiter.- See Also:
-
parse(CharSequence)
; to be removed in next major version.