com.hunnysoft.jmime
Class MediaType

java.lang.Object
  extended bycom.hunnysoft.jmime.Node
      extended bycom.hunnysoft.jmime.FieldBody
          extended bycom.hunnysoft.jmime.MediaType
All Implemented Interfaces:
java.lang.Cloneable

public class MediaType
extends FieldBody

Class that represents a MIME media-type.

MediaType represents a field body for the Content-Type header field as described in RFC 2045. This field body identifies the kind of data contained in the body of a message or body part. A media type is described by two keywords: a primary type (or just type) and a subtype. RFC 2046 specifies the seven primary types text, multipart, message, image, audio, video, and application. RFC 2077 adds the new primary type model.

MediaType has methods that allow you to set or get the type and subtype as byte strings. It also contains a list of Parameter objects that represent the parameters of the field body.


Field Summary
static int APPLICATION
          Enumerated value for application type
static int AUDIO
          Enumerated value for audio type
static int IMAGE
          Enumerated value for image type
static int MESSAGE
          Enumerated value for message type
static int MODEL
          Enumerated value for model type
static int MULTIPART
          Enumerated value for multipart type
static int NULL
          Enumerated value for unset type
static int TEXT
          Enumerated value for text type
static int UNKNOWN
          Enumerated value for unknown type
static int VIDEO
          Enumerated value for video type
 
Fields inherited from class com.hunnysoft.jmime.FieldBody
mText
 
Fields inherited from class com.hunnysoft.jmime.Node
mIsModified, mParent, mString
 
Constructor Summary
MediaType()
          Default constructor.
MediaType(ByteString bstr)
          Constructor that takes an initial byte string argument.
MediaType(MediaType other)
          Copy constructor.
 
Method Summary
 void addParameter(Parameter param)
          Adds a parameter to the end of the list.
 void assemble()
          Assembles the string representation.
 java.lang.String boundary()
          Gets the value of the boundary parameter, if present.
 java.lang.String charset()
          Gets the value of the charset parameter, if present.
 java.lang.Object clone()
          Creates a copy of this object.
 java.lang.String createBoundary(int level)
          Creates a string suitable for use as a boundary parameter.
 void deleteAllParameters()
          Deletes all parameters in the list.
 void insertParameterAt(int index, Parameter param)
          Inserts a parameter at the specified position.
 java.lang.String name()
          Gets the value of the name parameter, if present.
 int numParameters()
          Gets the number of parameters in the list.
 Parameter parameterAt(int index)
          Gets the parameter at the specified position.
 void parse()
          Parses the string representation.
 Parameter removeParameterAt(int index)
          Removes the parameter at the specified position.
 void setBoundary(java.lang.String str)
          Sets the value of the boundary parameter.
 void setCharset(java.lang.String str)
          Sets the value of the charset parameter.
 void setName(java.lang.String str)
          Sets the value of the name parameter.
 void setSubtype(java.lang.String subtype)
          Sets the subtype.
 void setType(java.lang.String type)
          Sets the primary type.
 java.lang.String subtype()
          Gets the subtype.
 java.lang.String type()
          Gets the primary type.
 int typeAsEnum()
          Gets the primary type as an enumerated value.
 void typeFromEnum(int type)
          Sets the primary type from an enumerated value.
 
Methods inherited from class com.hunnysoft.jmime.FieldBody
fold, isFoldingEnabled, maybeFold, setFoldingEnabled, setText, text, unfold
 
Methods inherited from class com.hunnysoft.jmime.Node
_setString, isModified, parent, setModified, setParent, setString, string, toString
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NULL

public static final int NULL
Enumerated value for unset type

See Also:
Constant Field Values

UNKNOWN

public static final int UNKNOWN
Enumerated value for unknown type

See Also:
Constant Field Values

TEXT

public static final int TEXT
Enumerated value for text type

See Also:
Constant Field Values

IMAGE

public static final int IMAGE
Enumerated value for image type

See Also:
Constant Field Values

AUDIO

public static final int AUDIO
Enumerated value for audio type

See Also:
Constant Field Values

VIDEO

public static final int VIDEO
Enumerated value for video type

See Also:
Constant Field Values

APPLICATION

public static final int APPLICATION
Enumerated value for application type

See Also:
Constant Field Values

MESSAGE

public static final int MESSAGE
Enumerated value for message type

See Also:
Constant Field Values

MULTIPART

public static final int MULTIPART
Enumerated value for multipart type

See Also:
Constant Field Values

MODEL

public static final int MODEL
Enumerated value for model type

See Also:
Constant Field Values
Constructor Detail

MediaType

public MediaType()
Default constructor.

This constructor sets the MediaType object's string representation to the empty string and sets its parent node to null. The list of Parameter objects is initially empty.


MediaType

public MediaType(MediaType other)
Copy constructor.

This constructor performs a deep copy of its argument. The parent node of the new MediaType object is set to null.

Parameters:
other - MediaType instance to copy

MediaType

public MediaType(ByteString bstr)
Constructor that takes an initial byte string argument.

This constructor sets the MediaType object's string representation to bstr and sets its parent node to null.

Normally, you call the parse() method immediately after this constructor to create the broken-down representation.

Parameters:
bstr - initial value for the string representation
Method Detail

clone

public java.lang.Object clone()
Creates a copy of this object.

This method, inherited from Object, creates a new MediaType object that has the same value as this MediaType object. The parent node of the new MediaType object is set to null.

Overrides:
clone in class FieldBody
Returns:
a copy of this object

type

public java.lang.String type()
Gets the primary type.

Returns:
primary type

setType

public void setType(java.lang.String type)
Sets the primary type.

Parameters:
type - primary type

subtype

public java.lang.String subtype()
Gets the subtype.

Returns:
subtype

setSubtype

public void setSubtype(java.lang.String subtype)
Sets the subtype.

Parameters:
subtype - subtype

typeAsEnum

public int typeAsEnum()
Gets the primary type as an enumerated value.

Returns:
primary type enumerated value

typeFromEnum

public void typeFromEnum(int type)
Sets the primary type from an enumerated value.

Parameters:
type - primary type enumerated value

boundary

public java.lang.String boundary()
Gets the value of the boundary parameter, if present.

The boundary parameter is required if the primary type is multipart. If there is no boundary parameter, this method returns an empty string.

This method is a convenience method that searches the list of Parameter objects.

Returns:
value of the boundary parameter; or the empty string if there is no boundary parameter

setBoundary

public void setBoundary(java.lang.String str)
Sets the value of the boundary parameter.

The boundary parameter is required if the primary type is multipart. If no boundary parameter exists when you call this method, the method creates a new boundary parameter and adds it to the list.

This method is a convenience method that accesses the list of Parameter objects.

Parameters:
str - value of the boundary parameter

createBoundary

public java.lang.String createBoundary(int level)
Creates a string suitable for use as a boundary parameter.

This method is a convenience method that creates a random string suitable for use as a multipart boundary. The method sets newly created string as the value of the boundary field.

Parameters:
level - indicates the level of nesting
Returns:
the newly created boundary string

name

public java.lang.String name()
Gets the value of the name parameter, if present.

The name parameter is often found in several media types, including the application/octet-stream media type. It suggests a file name for saving the content to a disk file. (The filename parameter in the content-disposition header field is an alternative way to indicate a file name.)

If there is no name parameter, the method returns an empty string.

This method is a convenience method that searches the list of Parameter objects.

Returns:
value of the name parameter; or the empty string if there is no name parameter

setName

public void setName(java.lang.String str)
Sets the value of the name parameter.

The name parameter is often found in several media types, including the application/octet-stream media type. It suggests a file name for saving the content to a disk file. (The filename parameter in the content-disposition header field is an alternative way to indicate a file name.)

If a name parameter is not already present, this method adds it.

This method is a convenience method that accesses the list of Parameter objects.

Parameters:
str - value of the name parameter

charset

public java.lang.String charset()
Gets the value of the charset parameter, if present.

The charset parameter is often found in text media types. It indicates the character encoding of the text content.

If there is no charset parameter, the method returns an empty string.

This method is a convenience method that searches the list of Parameter objects.

Returns:
value of the charset parameter; or the empty string if there is no charset parameter

setCharset

public void setCharset(java.lang.String str)
Sets the value of the charset parameter.

The charset parameter is often found in text media types. It indicates the character encoding of the text content.

If a charset parameter is not already present, this method adds it.

This method is a convenience method that accesses the list of Parameter objects.

Parameters:
str - value of the charset parameter

parse

public void parse()
Parses the string representation.

This method, inherited from Node, executes the parse operation for MediaType objects. The parse operation creates or updates the broken-down representation from the string representation. For MediaType objects, the parse operation parses the string representation to extract the type, subtype, and the list of Parameter objects. This method also calls the parse() method of each Parameter object in its list.

You should call this method after you set or modify the string representation, and before you access the type, subtype, or any of the contained Parameter objects.

This method clears the is-modified flag.

Overrides:
parse in class FieldBody

assemble

public void assemble()
Assembles the string representation.

This method, inherited from Node, executes the assemble operation for MediaType objects. The assemble operation creates or updates the string representation from the broken-down representation. For MediaType objects, the assemble operation builds the string representation from the type, subtype, and list of Parameter objects. Before it builds the string representation for the MediaType object, this method first calls the assemble() method of each Parameter object in its list.

You should call this method after you set or modify the type, subtype, or any of the contained Parameter objects, and before you retrieve the string representation.

This method clears the is-modified flag.

Overrides:
assemble in class FieldBody

numParameters

public int numParameters()
Gets the number of parameters in the list.

Returns:
number of parameters in the list

addParameter

public void addParameter(Parameter param)
Adds a parameter to the end of the list.

Parameters:
param - Parameter object to add

deleteAllParameters

public void deleteAllParameters()
Deletes all parameters in the list.


parameterAt

public Parameter parameterAt(int index)
Gets the parameter at the specified position.

Parameters:
index - position of the Parameter object to get (0 <= index < numParameters())
Returns:
Parameter object at the specified position
Throws:
java.lang.IndexOutOfBoundsException - if index is out of range

insertParameterAt

public void insertParameterAt(int index,
                              Parameter param)
Inserts a parameter at the specified position.

Inserting at position 0 inserts the parameter at the beginning. Inserting at position numParameters() appends the parameter to the end.

Parameters:
index - position in the list to insert the Parameter object (0 <= index <= numParameters())
param - Parameter object to insert
Throws:
java.lang.IndexOutOfBoundsException - if index is out of range

removeParameterAt

public Parameter removeParameterAt(int index)
Removes the parameter at the specified position.

Parameters:
index - position of the Parameter object to remove (0 <= index < numParameters())
Returns:
the removed Parameter object
Throws:
java.lang.IndexOutOfBoundsException - if index is out of range