- java.lang.Object
-
- com.github.mizosoft.methanol.MediaType
-
public final class MediaType extends java.lang.ObjectA MIME type. The text representation of this class can be used as the value of theContent-TypeHTTP header.A
MediaTypealso 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 ofincludes(MediaType)orisCompatibleWith(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 Summary
Fields Modifier and Type Field Description static MediaTypeANY*/*static MediaTypeAPPLICATION_ANYapplication/*static MediaTypeAPPLICATION_FORM_URLENCODEDapplication/x-www-form-urlencodedstatic MediaTypeAPPLICATION_JSONapplication/jsonstatic MediaTypeAPPLICATION_OCTET_STREAMapplication/octet-streamstatic MediaTypeAPPLICATION_X_PROTOBUFapplication/x-protobufstatic MediaTypeAPPLICATION_XHTML_XMLapplication/xhtml+xmlstatic MediaTypeAPPLICATION_XMLapplication/xmlstatic MediaTypeIMAGE_ANYimage/*static MediaTypeIMAGE_GIFimage/gifstatic MediaTypeIMAGE_JPEGimage/jpegstatic MediaTypeIMAGE_PNGimage/pngstatic MediaTypeTEXT_ANYtext/*static MediaTypeTEXT_HTMLtext/htmlstatic MediaTypeTEXT_MARKDOWNtext/markdownstatic MediaTypeTEXT_PLAINtext/plainstatic MediaTypeTEXT_XMLtext/xml
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.Optional<java.nio.charset.Charset>charset()Returns anOptionalrepresenting the value of the charset parameter.java.nio.charset.CharsetcharsetOrDefault(java.nio.charset.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.java.nio.charset.CharsetcharsetOrUtf8()Equivalent to callingcharsetOrDefault(StandardCharsets.UTF_8).booleanequals(@Nullable java.lang.Object obj)Tests the given object for equality with this instance.inthashCode()Returns a hashcode for this media type.booleanhasWildcard()Returnstrueif this media type is*/*or if it has a wildcard subtype.booleanincludes(MediaType other)Returns whether this media type includes the given one.booleanisCompatibleWith(MediaType other)Returns whether this media type is compatible with the given one.static MediaTypeof(java.lang.String type, java.lang.String subtype)Returns a newMediaTypewith the given type and subtype.static MediaTypeof(java.lang.String type, java.lang.String subtype, java.util.Map<java.lang.String,java.lang.String> parameters)Returns a newMediaTypewith the given type, subtype and parameters.java.util.Map<java.lang.String,java.lang.String>parameters()Returns an immutable map representing the parameters.static MediaTypeparse(java.lang.String value)Parses the given string into aMediaTypeinstance.java.lang.Stringsubtype()Returns the subtype.java.lang.StringtoString()Returns a text representation of this media type that is compatible with the value of theContent-Typeheader.java.lang.Stringtype()Returns the general type.MediaTypewithCharset(java.nio.charset.Charset charset)Returns a newMediaTypewith this instance's type, subtype and parameters but with the name of the given charset as the value of the charset parameter.MediaTypewithParameter(java.lang.String name, java.lang.String value)Returns a newMediaTypewith this instance's type, subtype and parameters but with the value of the parameter specified by the given name set to the given value.MediaTypewithParameters(java.util.Map<java.lang.String,java.lang.String> parameters)Returns a newMediaTypewith this instance's type, subtype and parameters but with each of the given parameters' names set to their corresponding values.
-
-
-
Field Detail
-
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 Detail
-
type
public java.lang.String type()
Returns the general type.
-
subtype
public java.lang.String subtype()
Returns the subtype.
-
parameters
public java.util.Map<java.lang.String,java.lang.String> parameters()
Returns an immutable map representing the parameters.
-
charset
public java.util.Optional<java.nio.charset.Charset> charset()
Returns anOptionalrepresenting the value of the charset parameter. An emptyOptionalis returned if no such parameter exists.- Throws:
java.nio.charset.IllegalCharsetNameException- if a charset parameter exists the value of which is invalidjava.nio.charset.UnsupportedCharsetException- if a charset parameter exists the value of which is not supported in this JVM
-
charsetOrDefault
public java.nio.charset.Charset charsetOrDefault(java.nio.charset.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.- Throws:
java.nio.charset.IllegalCharsetNameException- if a charset parameter exists the value of which is invalid
-
charsetOrUtf8
public java.nio.charset.Charset charsetOrUtf8()
Equivalent to callingcharsetOrDefault(StandardCharsets.UTF_8).
-
hasWildcard
public boolean hasWildcard()
Returnstrueif 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 awildcard typethat includes the latter or both have equal concrete type and subtype.
-
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 themincludesthe other.
-
withCharset
public MediaType withCharset(java.nio.charset.Charset charset)
Returns a newMediaTypewith this instance's type, subtype and parameters but with the name of the given charset as the value of the charset parameter.
-
withParameter
public MediaType withParameter(java.lang.String name, java.lang.String value)
Returns a newMediaTypewith this instance's type, subtype and parameters but with the value of the parameter specified by the given name set to the given value.- Throws:
java.lang.IllegalArgumentException- if the given name or value is invalid
-
withParameters
public MediaType withParameters(java.util.Map<java.lang.String,java.lang.String> parameters)
Returns a newMediaTypewith this instance's type, subtype and parameters but with each of the given parameters' names set to their corresponding values.- Throws:
java.lang.IllegalArgumentException- if any of the given parameters is invalid
-
equals
public boolean equals(@Nullable java.lang.Object obj)
Tests the given object for equality with this instance.trueis returned if the given object is aMediaTypeand both instances' type, subtype and parameters are equal.- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- the object to test for equality
-
hashCode
public int hashCode()
Returns a hashcode for this media type.- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
Returns a text representation of this media type that is compatible with the value of theContent-Typeheader.- Overrides:
toStringin classjava.lang.Object
-
of
public static MediaType of(java.lang.String type, java.lang.String subtype)
Returns a newMediaTypewith the given type and subtype.- Throws:
java.lang.IllegalArgumentException- if the given type or subtype is invalid
-
of
public static MediaType of(java.lang.String type, java.lang.String subtype, java.util.Map<java.lang.String,java.lang.String> parameters)
Returns a newMediaTypewith the given type, subtype and parameters.- Throws:
java.lang.IllegalArgumentException- if the given type, subtype or any of the given parameters is invalid
-
parse
public static MediaType parse(java.lang.String value)
Parses the given string into aMediaTypeinstance.- Throws:
java.lang.IllegalArgumentException- if the given string is an invalid media type
-
-