com.upokecenter.mail.MediaType

com.upokecenter.mail.MediaType

public final class MediaType extends Object

Specifies what kind of data a message body is.

A media type consists of a top-level type (the general category of the data), a subtype (the specific type), and an optional list of parameters. For example, the media type text/plain;charset=utf-8 is a text media type ("text"), namely, a plain text type ("plain"), and the parameters say that the data uses UTF-8, a Unicode character encoding ("charset = utf-8"). Other top-level types include "audio", "video", and "application".

A media type is sometimes known as a "MIME type", for Multipurpose Internet Mail Extensions, the standard that introduced media types.

This type is immutable, meaning its values can't be changed once it's created. To create a changeable media type object, use the MediaType.Builder class.

Note: According to RFC 2049, unrecognized subtypes of the top-level type multipart must be treated as multipart/mixed and unrecognized media types as the media type application/octet-stream.

Nested Classes

Fields

Methods

Field Details

TextPlainAscii

public static final MediaType TextPlainAscii

Specifies the media type "text/plain" and the "charset" parameter "US-ASCII", used for plain text data that contains only characters within the basic Latin range (U+0000 to U+007F).

TextPlainUtf8

public static final MediaType TextPlainUtf8

Specifies the media type "text/plain" and the "charset" parameter "utf-8", used for plain text data that may contain characters outside the basic Latin range (U+0000 to U+007F).

MessageRfc822

public static final MediaType MessageRfc822

Specifies the media type "message/rfc822", used for Internet mail messages.

ApplicationOctetStream

public static final MediaType ApplicationOctetStream

Specifies the media type "application/octet-stream", used for arbitrary binary data.

Method Details

getTopLevelType

public final String getTopLevelType()

Gets the name of this media type's top-level type (such as "text" in "text/plain", or "audio" in "audio/basic"). The resulting string will be in lower case; that is, with its basic upper-case letters ("A" to "Z") converted to basic lower-case letters ("a" to "z").

Returns:

HasStructuredSuffix

public boolean HasStructuredSuffix(String suffix)

Returns whether this media type's subtype has the given structured syntax suffix.

Parameters:

Returns:

equals

public boolean equals(Object obj)

Determines whether this object and another object are equal.

Overrides:

Parameters:

Returns:

hashCode

public int hashCode()

Calculates the hash code of this object. The exact algorithm used by this method may change between versions of this library, and no application or process IDs are used in the hash code calculation.

Overrides:

Returns:

getSubType

public final String getSubType()

Gets this media type's subtype (for example, "plain" in "text/plain"). The resulting string will be in lower case; that is, with its basic upper-case letters ("A" to "Z") converted to basic lower-case letters ("a" to "z").

Returns:

isText

public final boolean isText()

Gets a value indicating whether this is a text media type ("text/*").

Returns:

isMultipart

public final boolean isMultipart()

Gets a value indicating whether this is a multipart media type.

Returns:

getParameters

public final Map<String,String> getParameters()

Gets a list of the parameter names contained in this media type object and their values. Each parameter name will be in lower case; that is, with its basic upper-case letters ("A" to "Z") converted to basic lower-case letters ("a" to "z").

Returns:

toString

public String toString()

Converts this media type to a text string form suitable for inserting in email headers. Notably, the string contains the value of a Content-Type header field (without the text necessarily starting with "Content-Type" followed by a space), and consists of one or more lines.

Overrides:

Returns:

ToSingleLineString

public String ToSingleLineString()

Converts this media type to a text string form suitable for inserting in HTTP headers. Notably, the string contains the value of a Content-Type header field (without the text necessarily starting with "Content-Type" followed by a space), and consists of a single line.

Returns:

ToUriSafeString

public String ToUriSafeString()

Converts this media type to a text string form suitable for data URIs. Notably, the string contains the value of a Content-Type header field (without the text necessarily starting with "Content-Type" followed by a space), consists of a single line, and uses percent-encoding as necessary or convenient so that the resulting string can validly appear in a URI path.

Returns:

GetCharset

public String GetCharset()

Gets this media type's "charset" parameter, naming a character encoding used to represent text in the data that uses this media type.

Returns:

GetParameter

public String GetParameter(String name)

Gets the value of a parameter in this media type, such as "charset" or "format".

Parameters:

Returns:

Throws:

getTypeAndSubType

public final String getTypeAndSubType()

Gets the top level type and subtype of this media type, separated by a slash; for example, "text/plain". The resulting string will be in lowercase letters.

Returns:

Parse

public static MediaType Parse(String mediaTypeValue)

Parses a media type string and returns a media type object. For further information, see the overload taking a MediaType parameter.

Parameters:

Returns:

Parse

public static MediaType Parse(String str, MediaType defaultValue)

Parses a media type string and returns a media type object, or the default value if the string is invalid. This method checks the syntactic validity of the string, but not whether it has all parameters it's required to have or whether the parameters themselves are set to valid values for the parameter.

This method assumes the given media type string was directly extracted from the Content-Type header field (as defined for email messages) and follows the syntax given in RFC 2045. Accordingly, among other things, the media type string can contain comments (delimited by parentheses).

RFC 2231 extensions allow each media type parameter to be associated with a character encoding and/or language, and support parameter values that span two or more key-value pairs. Parameters making use of RFC 2231 extensions have names with an asterisk ("*"). Such a parameter will be ignored if it is ill-formed because of RFC 2231's rules (except for illegal percent-decoding or undecodable sequences for the given character encoding). Examples of RFC 2231 extensions follow (both examples encode the same "filename" parameter):

text/example; filename*=utf-8'en'filename.txt

text/example; filename*0*=utf-8'en'file; filename*1*=name%2Etxt

This implementation ignores keys (in parameter key-value pairs) that appear more than once in the media type. Nothing in RFCs 2045, 2183, 2231, 6266, or 7231 explicitly disallows such keys, or otherwise specifies error-handling behavior for such keys.

Parameters:

Returns:

Throws:

Back to MailLib start page.