?? mmencoder.java
字號:
}
try {
SimpleDateFormat formatter = dateFormats[i++];
date = formatter.parse(time);
} catch (Exception e) {
//log.debug(
// "encodeDate: "
// + e.toString()
// + ", using pattern: "
// + dateFormats[i
// - 1].toPattern());
continue;
}
success = true;
}
if (success)
encodeDate(res, date);
}
protected static void encodeDateVariable(
ByteArrayOutputStream res,
String time,
boolean absolute)
throws Exception {
Date date = null;
SimpleDateFormat[] dateFormats =
{
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"),
// as specified in the specs
new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.US),
new SimpleDateFormat()};
// default locale
int i = 0;
boolean success = false;
while (!success) {
if (i > dateFormats.length - 1) {
//log.error("encodeDateVariable: Unable to parse: " + time);
break;
}
try {
SimpleDateFormat formatter = dateFormats[i++];
date = formatter.parse(time);
} catch (Exception e) {
//log.debug(
// "encodeDateVariable: "
// + e.toString()
// + ", using pattern: "
// + dateFormats[i
// - 1].toPattern());
continue;
}
success = true;
}
if (success)
encodeDateVariable(res, date, absolute);
}
/**
* encode a Date class into a long (4 octets)
*/
protected static void encodeDateVariable(
ByteArrayOutputStream res,
Date date,
boolean absolute)
throws Exception {
long l = date.getTime() / 1000; // seconds since 1.1.1970, 00:00:00 GMT
if (!absolute) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(0x81); // not absolute
encodeLong(baos, l);
byte[] tmp = baos.toByteArray();
res.write(tmp.length);
res.write(tmp);
} else {
res.write((byte) 0x06); // length of multi-octet integer (4 bytes)
encodeInt(res, 128);
encodeLong(res, l);
}
}
/**
* encode a string
*/
protected static void encodeString(ByteArrayOutputStream res, String s)
throws Exception {
byte[] b = s.getBytes();
if ((b == null) || (b.length == 0))
return; // no content
if (b[0] >= 128) {
res.write((byte) 127); // quote
res.write(b); //, 1, b.length-1);
} else
res.write(b);
res.write((byte) 0x00); // end of string
}
/**
* encode a short int
*/
protected static void encodeInt(ByteArrayOutputStream res, int i)
throws Exception {
byte b = (byte) (i & 0xFF);
if (b < 128)
b |= 128;
res.write(b);
}
/**
* encode a boolean (with Boolean as input)
*/
protected static void encodeBoolean(ByteArrayOutputStream res, Boolean b)
throws Exception {
if (b.booleanValue())
res.write(128);
else // true
res.write(129); // false
}
/**
* encode a boolean (with String as input)
*/
protected static void encodeBoolean(ByteArrayOutputStream res, String b)
throws Exception {
if (b.equalsIgnoreCase("true"))
res.write(128);
else // true
res.write(129); // false
}
/**
* encode content type
*
* format for content types is defined in [WAPWSP], 8.4.2.24:
*
* Content-type-value = Constrained-media | Content-general-form
* Content-general-form = Value-length Media-type
* Media-type = (Well-known-media | Extension-Media) *(Parameter)
*
* @param ct Content-type, with parameters separated with ";" (semi-colon)
*/
protected static void encodeContentType(ByteArrayOutputStream res, String ct)
throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
boolean hasParameters = false;
StringTokenizer st = new StringTokenizer(ct, ";");
int i = 0;
String token = st.nextToken(); // content-type
for (i = 0; i < MMConstants.CONTENT_TYPES.length; i++)
if (MMConstants.CONTENT_TYPES[i].equalsIgnoreCase(token)) {
encodeInt(baos, i);
break;
}
if (i == MMConstants.CONTENT_TYPES.length) { // not well known encoding found
encodeString(baos, token); // encoded version not found
//res.write(token.getBytes());
}
// encode parameters
while (st.hasMoreTokens()) {
token = st.nextToken(); // name-value pair
String name = token.substring(0, token.indexOf('=')).trim();
String value = token.substring(token.indexOf('=') + 1).trim();
if (value.startsWith("\""))
value = value.substring(1, value.length() - 1);
encodeParameter(baos, name, value);
hasParameters = true;
}
byte[] dta = baos.toByteArray();
baos = null;
// write length
if ((hasParameters) && (dta.length > 1)) {
if (dta.length > 30) {
res.write(31);
encodeUintvar(res, dta.length);
} else
res.write(dta.length);
}
res.write(dta);
}
/**
* get message class from string, it's a byte if pre-defined
*/
protected static void encodeMessageClass(
ByteArrayOutputStream res,
String msgClass)
throws Exception {
if (msgClass.equalsIgnoreCase("Personal"))
encodeInt(res, MMConstants.MESSAGE_CLASS_PERSONAL);
else if (msgClass.equalsIgnoreCase("Advertisement"))
encodeInt(res, MMConstants.MESSAGE_CLASS_ADVERTISEMENT);
else if (msgClass.equalsIgnoreCase("Informational"))
encodeInt(res, MMConstants.MESSAGE_CLASS_INFORMATIONAL);
else if (msgClass.equalsIgnoreCase("Auto"))
encodeInt(res, MMConstants.MESSAGE_CLASS_AUTO);
else {
encodeString(res, msgClass);
}
}
/**
* get message type from string, it's a byte if predefined
*/
protected static void encodeMessageType(ByteArrayOutputStream res, String msgType)
throws Exception {
if (msgType.equalsIgnoreCase("m-send-req"))
encodeInt(res, MMConstants.MESSAGE_TYPE_M_SEND_REQ);
else if (msgType.equalsIgnoreCase("m-send-conf"))
encodeInt(res, MMConstants.MESSAGE_TYPE_M_SEND_CONF);
else if (msgType.equalsIgnoreCase("m-notification-ind"))
encodeInt(res, MMConstants.MESSAGE_TYPE_M_NOTIFICATION_IND);
else if (msgType.equalsIgnoreCase("m-notifyresp-ind"))
encodeInt(res, MMConstants.MESSAGE_TYPE_M_NOTIFYRESP_IND);
else if (msgType.equalsIgnoreCase("m-retrieve-conf"))
encodeInt(res, MMConstants.MESSAGE_TYPE_M_RETRIEVE_CONF);
else if (msgType.equalsIgnoreCase("m-acknowledge-ind"))
encodeInt(res, MMConstants.MESSAGE_TYPE_M_ACKNOWLEDGE_IND);
else if (msgType.equalsIgnoreCase("m-delivery-ind"))
encodeInt(res, MMConstants.MESSAGE_TYPE_M_DELIVERY_IND);
else {
encodeString(res, msgType);
}
}
/**
* get message class from string, it's a byte if pre-defined
*/
protected static void encodePriority(ByteArrayOutputStream res, String pri)
throws Exception {
if (pri.equalsIgnoreCase("Low"))
encodeInt(res, MMConstants.PRIORITY_LOW);
else if (pri.equalsIgnoreCase("Normal"))
encodeInt(res, MMConstants.PRIORITY_NORMAL);
else if (pri.equalsIgnoreCase("High"))
encodeInt(res, MMConstants.PRIORITY_HIGH);
else {
encodeString(res, pri);
}
}
/**
* get response status, it's a byte if predefined
*/
protected static void encodeResponseStatus(ByteArrayOutputStream res, String resp)
throws Exception {
if (resp.equalsIgnoreCase("ok"))
encodeInt(res, MMConstants.RESPONSE_STATUS_OK);
else if (resp.equalsIgnoreCase("Error-unspecified"))
encodeInt(res, MMConstants.RESPONSE_STATUS_ERROR_UNSPECIFIED);
else if (resp.equalsIgnoreCase("Error-service-denied"))
encodeInt(res, MMConstants.RESPONSE_STATUS_ERROR_SERVICE_DENIED);
else if (resp.equalsIgnoreCase("Error-message-format-corrupt"))
encodeInt(res, MMConstants.RESPONSE_STATUS_ERROR_MESSAGE_FORMAT_CORRUPT);
else if (resp.equalsIgnoreCase("Error-sending-address-unresolved"))
encodeInt(res, MMConstants.RESPONSE_STATUS_ERROR_SENDING_ADDRESS_UNSPECIFIED);
else if (resp.equalsIgnoreCase("Error-message-not-found"))
encodeInt(res, MMConstants.RESPONSE_STATUS_ERROR_MESSAGE_NOT_FOUND);
else if (resp.equalsIgnoreCase("Error-network-problem"))
encodeInt(res, MMConstants.RESPONSE_STATUS_ERROR_NETWORK_PROBLEM);
else if (resp.equalsIgnoreCase("Error-content-not-accepted"))
encodeInt(res, MMConstants.RESPONSE_STATUS_ERROR_CONTENT_NOT_ACCEPTED);
else if (resp.equalsIgnoreCase("Error-unsupported-message"))
encodeInt(res, MMConstants.RESPONSE_STATUS_ERROR_UNSUPPORTED_MESSAGE);
else {
encodeString(res, resp);
}
}
/**
* get response status, it's a byte if predefined
*/
protected static void encodeStatus(ByteArrayOutputStream res, String resp)
throws Exception {
if (resp.equalsIgnoreCase("Expired"))
encodeInt(res, MMConstants.STATUS_EXPIRED);
else if (resp.equalsIgnoreCase("Retrieved"))
encodeInt(res, MMConstants.STATUS_RETRIEVED);
else if (resp.equalsIgnoreCase("Rejected"))
encodeInt(res, MMConstants.STATUS_REJECTED);
else if (resp.equalsIgnoreCase("Deferred"))
encodeInt(res, MMConstants.STATUS_DEFERRED);
else if (resp.equalsIgnoreCase("Unrecognised"))
encodeInt(res, MMConstants.STATUS_UNRECOGNIZED);
else {
encodeString(res, resp);
}
}
/**
* encode from element
*/
protected static void encodeFrom(ByteArrayOutputStream res, String from)
throws Exception {
encodeUintvar(res, from.length() + 2);
// address-present-token + str length + null
encodeInt(res, 0); // address-present-token
encodeString(res, from);
}
/**
* encode charset
*/
protected static void encodeCharset(ByteArrayOutputStream res, String value)
throws Exception {
for (int c = 0; c < MMConstants.WELLKNOWN_CHARSETS.length; c++) {
if (((String) MMConstants.WELLKNOWN_CHARSETS[c][0]).equalsIgnoreCase(value)) {
encodeInteger(
res,
((Integer) MMConstants.WELLKNOWN_CHARSETS[c][1]).intValue());
return;
}
}
// no well-known substitute
encodeString(res, value);
}
/**
* validate given message
*/
protected static void validateMMMessage(MMMessage msg) throws MMEncodingException {
if (!msg.isTransactionIdAvailable())
throw new MMEncodingException("X-MMS-Transaction-ID missing or invalid");
if (!msg.isVersionAvailable())
throw new MMEncodingException("X-MMS-Version missing or invalid");
// validate
switch (msg.getMessageType()) {
case MMConstants.MESSAGE_TYPE_M_SEND_REQ :
if (!msg.isFromAvailable())
throw new MMEncodingException("From (sender) missing or invalid");
if (!msg.isContentTypeAvailable())
throw new MMEncodingException("Content-Type missing or invalid");
break;
case MMConstants.MESSAGE_TYPE_M_SEND_CONF :
if (!msg.isResponseStatusAvailable())
throw new MMEncodingException("X-MMS-Response-Status missing or invalid");
break;
case MMConstants.MESSAGE_TYPE_M_NOTIFICATION_IND :
if (!msg.isMessageClassAvailable())
throw new MMEncodingException("X-MMS-Message-Class missing or invalid");
if (!msg.isContentLocationAvailable())
throw new MMEncodingException("Content-Location missing or invalid");
break;
case MMConstants.MESSAGE_TYPE_M_NOTIFYRESP_IND :
if (!msg.isStatusAvailable())
throw new MMEncodingException("X-MMS-Status missing or invalid");
break;
case MMConstants.MESSAGE_TYPE_M_RETRIEVE_CONF :
if (!msg.isDateAvailable())
throw new MMEncodingException("Date missing or invalid");
if (!msg.isContentTypeAvailable())
throw new MMEncodingException("Content-Type missing or invalid");
break;
case MMConstants.MESSAGE_TYPE_M_ACKNOWLEDGE_IND :
break;
case MMConstants.MESSAGE_TYPE_M_DELIVERY_IND :
if (!msg.isMessageIdAvailable())
throw new MMEncodingException("Message-ID missing or invalid");
if (!msg.isToAvailable())
throw new MMEncodingException("To (recipient) missing or invalid");
if (!msg.isStatusAvailable())
throw new MMEncodingException("X-MMS-Status missing or invalid");
break;
default :
throw new MMEncodingException("Unknown X-MMS-Message-Type");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -