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 .
parse(rawdata);
|
public MimeType(String primary, String sub)Builds a MimeType with the given primary and sub
type but has an empty parameter list.
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.
// 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.Object | clone()Returns a clone of this object.
MimeType newObj = null;
try {
newObj = (MimeType)super.clone();
} catch (CloneNotSupportedException cannotHappen) {
}
newObj.parameters = (MimeTypeParameterList)parameters.clone();
return newObj;
|
public boolean | equals(java.lang.Object thatObject)MimeType s are equal if their primary types,
subtypes, and parameters are all equal. No default values
are taken into account.
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.String | getBaseType()Return a String representation of this object
without the parameter list.
return primaryType + "/" + subType;
|
public java.lang.String | getParameter(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.MimeTypeParameterList | getParameters()Retrieve a copy of this object's parameter list.
return (MimeTypeParameterList)parameters.clone();
|
public java.lang.String | getPrimaryType()Retrieve the primary type of this object.
return primaryType;
|
public java.lang.String | getSubType()Retrieve the sub type of this object.
return subType;
|
public int | hashCode()
// 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 boolean | isTokenChar(char c)Determines whether or not a given character belongs to a legal token.
return ((c > 040) && (c < 0177)) && (TSPECIALS.indexOf(c) < 0);
|
private boolean | isValidToken(java.lang.String s)Determines whether or not a given string is a legal token.
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 boolean | match(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 .
if (type == null)
return false;
return primaryType.equals(type.getPrimaryType())
&& (subType.equals("*")
|| type.getSubType().equals("*")
|| (subType.equals(type.getSubType())));
|
public boolean | match(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 .
if (rawdata == null)
return false;
return match(new MimeType(rawdata));
|
private void | parse(java.lang.String rawdata)A routine for parsing the MIME type out of a String.
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 void | readExternal(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.
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 void | removeParameter(java.lang.String name)Remove any value associated with the given name.
parameters.remove(name);
|
public void | setParameter(java.lang.String name, java.lang.String value)Set the value to be associated with the given name, replacing
any previous association.
parameters.set(name, value);
|
public java.lang.String | toString()Return the String representation of this object.
return getBaseType() + parameters.toString();
|
public void | writeExternal(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.
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());
}
|