FileDocCategorySizeDatePackage
MimeType.javaAPI DocJava SE 5 API12405Fri Aug 26 14:56:48 BST 2005java.awt.datatransfer

MimeType

public class MimeType extends Object implements Externalizable, Cloneable
A Multipurpose Internet Mail Extension (MIME) type, as defined in RFC 2045 and 2046. THIS IS *NOT* - REPEAT *NOT* - A PUBLIC CLASS! DataFlavor IS THE PUBLIC INTERFACE, AND THIS IS PROVIDED AS A ***PRIVATE*** (THAT IS AS IN *NOT* PUBLIC) HELPER CLASS!

Fields Summary
static final long
serialVersionUID
private String
primaryType
private String
subType
private MimeTypeParameterList
parameters
private static final String
TSPECIALS
A string that holds all the special chars.
Constructors Summary
public MimeType()
Constructor for externalization; this constructor should not be called directly by an application, since the result will be an uninitialized, immutable MimeType object.


                                
      
    
public MimeType(String rawdata)
Builds a MimeType from a String.

param
rawdata text used to initialize the MimeType
throws
NullPointerException if rawdata is null

        parse(rawdata);
    
public MimeType(String primary, String sub)
Builds a MimeType with the given primary and sub type but has an empty parameter list.

param
primary the primary type of this MimeType
param
sub the subtype of this MimeType
throws
NullPointerException if either primary or sub is null

	this(primary, sub, new MimeTypeParameterList());
    
public MimeType(String primary, String sub, MimeTypeParameterList mtpl)
Builds a MimeType with a pre-defined and valid (or empty) parameter list.

param
primary the primary type of this MimeType
param
sub the subtype of this MimeType
param
mtpl the requested parameter list
throws
NullPointerException if either primary, sub or mtpl is null

        //    check to see if primary is valid
        if(isValidToken(primary)) {
            primaryType = primary.toLowerCase();
        } else {
            throw new MimeTypeParseException("Primary type is invalid.");
        }
        
        //    check to see if sub is valid
        if(isValidToken(sub)) {
            subType = sub.toLowerCase();
        } else {
            throw new MimeTypeParseException("Sub type is invalid.");
        }
        
	parameters = (MimeTypeParameterList)mtpl.clone();
    
Methods Summary
public java.lang.Objectclone()
Returns a clone of this object.

return
a clone of this object

	MimeType newObj = null;
	try {
	    newObj = (MimeType)super.clone();
	} catch (CloneNotSupportedException cannotHappen) {
	}
	newObj.parameters = (MimeTypeParameterList)parameters.clone();
	return newObj;
    
public booleanequals(java.lang.Object thatObject)
MimeTypes are equal if their primary types, subtypes, and parameters are all equal. No default values are taken into account.

param
thatObject the object to be evaluated as a MimeType
return
true if thatObject is a MimeType; otherwise returns false

	if (!(thatObject instanceof MimeType)) {
	    return false;
	}
	MimeType that = (MimeType)thatObject;
	boolean isIt = 
	    ((this.primaryType.equals(that.primaryType)) &&
	     (this.subType.equals(that.subType)) &&
	     (this.parameters.equals(that.parameters)));
	return isIt;
    
public java.lang.StringgetBaseType()
Return a String representation of this object without the parameter list.

        return primaryType + "/" + subType;
    
public java.lang.StringgetParameter(java.lang.String name)
Retrieve the value associated with the given name, or null if there is no current association.

        return parameters.get(name);
    
public java.awt.datatransfer.MimeTypeParameterListgetParameters()
Retrieve a copy of this object's parameter list.

	return (MimeTypeParameterList)parameters.clone();
    
public java.lang.StringgetPrimaryType()
Retrieve the primary type of this object.

        return primaryType;
    
public java.lang.StringgetSubType()
Retrieve the sub type of this object.

        return subType;
    
public inthashCode()


	// We sum up the hash codes for all of the strings. This
	// way, the order of the strings is irrelevant
	int code = 0;
	code += primaryType.hashCode();
	code += subType.hashCode();
	code += parameters.hashCode();
	return code;
    
private static booleanisTokenChar(char c)
Determines whether or not a given character belongs to a legal token.

        return ((c > 040) && (c < 0177)) && (TSPECIALS.indexOf(c) < 0);
    
private booleanisValidToken(java.lang.String s)
Determines whether or not a given string is a legal token.

throws
NullPointerException if s is null

        int len = s.length();
        if(len > 0) {
            for (int i = 0; i < len; ++i) {
                char c = s.charAt(i);
                if (!isTokenChar(c)) {
                    return false;
                }
            }
            return true;
        } else {
            return false;
        }
    
public booleanmatch(java.awt.datatransfer.MimeType type)
Returns true if the primary type and the subtype of this object are the same as the specified type; otherwise returns false.

param
type the type to compare to this's type
return
true if the primary type and the subtype of this object are the same as the specified type; otherwise returns false

	if (type == null)
            return false;
        return primaryType.equals(type.getPrimaryType())
                    && (subType.equals("*")
                            || type.getSubType().equals("*")
                            || (subType.equals(type.getSubType())));
    
public booleanmatch(java.lang.String rawdata)
Returns true if the primary type and the subtype of this object are the same as the content type described in rawdata; otherwise returns false.

param
rawdata the raw data to be examined
return
true if the primary type and the subtype of this object are the same as the content type described in rawdata; otherwise returns false; if rawdata is null, returns false

        if (rawdata == null)
            return false;
        return match(new MimeType(rawdata));
    
private voidparse(java.lang.String rawdata)
A routine for parsing the MIME type out of a String.

throws
NullPointerException if rawdata is null

        int slashIndex = rawdata.indexOf('/");
        int semIndex = rawdata.indexOf(';");
        if((slashIndex < 0) && (semIndex < 0)) {
            //    neither character is present, so treat it
            //    as an error
            throw new MimeTypeParseException("Unable to find a sub type.");
        } else if((slashIndex < 0) && (semIndex >= 0)) {
            //    we have a ';' (and therefore a parameter list),
            //    but no '/' indicating a sub type is present
            throw new MimeTypeParseException("Unable to find a sub type.");
        } else if((slashIndex >= 0) && (semIndex < 0)) {
            //    we have a primary and sub type but no parameter list
            primaryType = rawdata.substring(0,
slashIndex).trim().toLowerCase();
            subType = rawdata.substring(slashIndex +
1).trim().toLowerCase();
            parameters = new MimeTypeParameterList();
        } else if (slashIndex < semIndex) {
            //    we have all three items in the proper sequence
            primaryType = rawdata.substring(0,
slashIndex).trim().toLowerCase();
            subType = rawdata.substring(slashIndex + 1,
semIndex).trim().toLowerCase();
            parameters = new
MimeTypeParameterList(rawdata.substring(semIndex));
        } else {
            //    we have a ';' lexically before a '/' which means we have a primary type
            //    & a parameter list but no sub type
            throw new MimeTypeParseException("Unable to find a sub type.");
        }
        
        //    now validate the primary and sub types
        
        //    check to see if primary is valid
        if(!isValidToken(primaryType)) {
            throw new MimeTypeParseException("Primary type is invalid.");
        }
        
        //    check to see if sub is valid
        if(!isValidToken(subType)) {
            throw new MimeTypeParseException("Sub type is invalid.");
        }
    
public voidreadExternal(java.io.ObjectInput in)
The object implements the readExternal method to restore its contents by calling the methods of DataInput for primitive types and readObject for objects, strings and arrays. The readExternal method must read the values in the same sequence and with the same types as were written by writeExternal.

exception
ClassNotFoundException If the class for an object being restored cannot be found.

        String s = in.readUTF();
        if (s == null || s.length() == 0) { // long mime type
            byte[] ba = new byte[in.readInt()];
            in.readFully(ba);
            s = new String(ba);
        }
        try {
            parse(s);
        } catch(MimeTypeParseException e) {
            throw new IOException(e.toString());
        }
    
public voidremoveParameter(java.lang.String name)
Remove any value associated with the given name.

throw
IllegalArgumentExcpetion if parameter may not be deleted

        parameters.remove(name);
    
public voidsetParameter(java.lang.String name, java.lang.String value)
Set the value to be associated with the given name, replacing any previous association.

throw
IllegalArgumentException if parameter or value is illegal

        parameters.set(name, value);
    
public java.lang.StringtoString()
Return the String representation of this object.

        return getBaseType() + parameters.toString();
    
public voidwriteExternal(java.io.ObjectOutput out)
The object implements the writeExternal method to save its contents by calling the methods of DataOutput for its primitive values or calling the writeObject method of ObjectOutput for objects, strings and arrays.

exception
IOException Includes any I/O exceptions that may occur

        String s = toString(); // contains ASCII chars only
        // one-to-one correspondence between ASCII char and byte in UTF string
        if (s.length() <= 65535) { // 65535 is max length of UTF string
            out.writeUTF(s);
        } else {
            out.writeByte(0);
            out.writeByte(0);
            out.writeInt(s.length());
            out.write(s.getBytes());
        }