⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmdecoder.java

📁 用于开发mms应用的Java库
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				value = decodeDateStr(in);
				break;	
			case 6:		// TYPE_MSGCLASS
				value = decodeMessageClass(in);
				break;	
			case 7:		// TYPE_MSGTYPE
				value = decodeMessageType(in);
				break;
			case 8:		// TYPE_PRIORITY
				value = decodePriority(in);
				break;
			case 9:		// TYPE_RESPONSESTATUS
				value = decodeResponseStatus(in);
				break;				
			case 10:		// TYPE_STATUS
				value = decodeStatus(in);
				break;			
			case 11:		// TYPE_CONTENTTYPE
				value = decodeContentType(in);
				break;	
			case 12:		// TYPE_VERSION
				value = decodeVersion(in);
				break;	
			case 13:		// TYPE_FROM
				value = decodeFrom(in);
				break;			
			case 14:		// TYPE_NOVALUE:
				value = "";
				break;
			case 15: 		// TYPE_CHARSET
				value = decodeCharset(in);
				break;
			case 16:		// TYPE_Q
				value = "";
				break;		// TYPE_INTEGER		
			case 17:
				value = String.valueOf(decodeInteger(in));
				break;
			default: value = decodeString(in); // store string representation
		}
		
		return value;
	}


	
	
	
	/*****************************************************************************************
	 * Functions for decoding values according to [MMSEncapsulation] and [WAPWSP]
	 */
	
	/**
	 * decode a long value
	 */
	protected static long decodeLong(ByteArrayInputStream res) throws Exception {
		int len = 0;
		byte[] buf = new byte[1];
		
		// first byte specifies length		
		res.read(buf);
		len = buf[0];
		
		int r = 0;
		// most significant bit first
		for (int i = 0; i < len; i++) {
			res.read(buf);
			
			r = r << 8;
			r |= (buf[0] & 0xFF);
		}
		return r;
	}
	
	/**
	 * encode a short int
	 */
	protected static int decodeInt(ByteArrayInputStream res) throws Exception {
		byte[] b = new byte[1];
		res.read(b);
		
		if ((b[0] & 0xFF) >= 128) b[0] &= 127;	
		
		return (int) (b[0] & 0xFF);
	}

	/**
	 * decode a integer (short or long)
	 */
	protected static int decodeInteger(ByteArrayInputStream res) throws Exception {
		byte[] b = new byte[1];
		res.read(b);
		if ((b[0] & 0xFF) >= 128) { // short form
			b[0] &= 127;	
			return (int) (b[0] & 0xFF);
		}
		int len = b[0];
		
		int r = 0;
		// most significant bit first
		for (int i = 0; i < len; i++) {
			res.read(b);
			
			r = r << 8;
			r |= (b[0] & 0xFF);
		}
		return r;
	}	

	/**
	 * decode a Uintvar (variable length int). Max 32bits
	 */
	protected static int decodeUintvar(ByteArrayInputStream res) throws Exception {
		byte[] buf = new byte[1];
		int r = 0;
		
		do {
			res.read(buf);
						
			r = (r << 7) | (buf[0] & 0x7F);
		} while ((buf[0] & 0x80) != 0);

		return r;
	}
	
	/**
	 * encode a Date class into a long (4 octets)
	 */
	protected static Date decodeDate(ByteArrayInputStream res) throws Exception {
		long l = decodeLong(res) * 1000;
		return new Date(l);
	}
	
	/**
	 * encode a Date class into a String
	 */
	protected static String decodeDateStr(ByteArrayInputStream res) throws Exception {
		Date d = decodeDate(res);
			
		return formatDate(d); 
	}

	/**
	 * encode a Date class into a long (4 octets)
	 */
	protected static Object[] decodeDateVariable(ByteArrayInputStream res) throws Exception {
		Object[] result = new Object[2];
		
		int len = decodeInt(res);
		
		byte[] tmp = new byte[len]; 
		res.read(tmp);
		
		ByteArrayInputStream bais = new ByteArrayInputStream(tmp);
		
		if (len > 4) {
			int abs = decodeInt(bais);
			long l = decodeLong(bais) * 1000;
			result[0] = new Date(l);
			result[1] = new Boolean(abs == 0);
		} else {
			long l = decodeLong(bais) * 1000;
			result[0] = new Date(l);
			result[1] = new Boolean(false);
		}
		return result;
	}
	
	/**
	 * encode a Date class into a String
	 */
	protected static String decodeDateStrVariable(ByteArrayInputStream res) throws Exception {
		Object[] tmp = decodeDateVariable(res);
			
		return formatDate((Date) tmp[0]); 
	}
	
			
	/**
	 * decode a string
	 */
	protected static String decodeString(ByteArrayInputStream res) throws Exception {
		StringBuffer r = new StringBuffer();
		
		byte[] b = new byte[1];
		if (res.read(b) == -1) return null; // eos
		
		if (((b[0] & 0xFF) != 127) && (b[0] != 0)) r.append((char) b[0]);	// quote

		while ((b[0] & 0xFF) != 0) {
			if (res.read(b) == -1) break; // eos			
			if (b[0] != 0) r.append((char) b[0]);
		}
		
		if (r.length() == 0) return null;
		
		return new String(r);
	}
	
	
	/**
	 * decode a boolean (with Boolean as input)
	 */
	protected static Boolean decodeBoolean(ByteArrayInputStream res) throws Exception {
		byte[] b = new byte[1];
		res.read(b);
		
		if ((b[0] & 0xFF) == 128) return new Boolean(true); else
		  return new Boolean(false);	
	}
	
	/**
	 * Decode 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)
	 */
	protected static String decodeContentType(ByteArrayInputStream res) throws Exception {		
		byte[] buf = new byte[1];
	  	
		if (res.read(buf) == -1) return null; // eos
	  	
		int i = (int) (buf[0] & 0xFF);
	  	
		String ct = "";
		if (i == 0) return null; // none
	  	
		if (i < 128) { // Content-general-form
			int len = i;
	  		
			if (i > 31) { // string
				ct = (char) i + decodeString(res);
			} else {
				if (i == 31) // length quote, when length > 30
					len = decodeUintvar(res); 
	  	  			
				byte[] dta = new byte[len];
				res.read(dta);
			
				ByteArrayInputStream in = new ByteArrayInputStream(dta);
				in.read(buf);
			
				int c = (int) (buf[0] & 0xFF);
				if (c < 128) {
					String tmp = decodeString(in);
					if (tmp != null) ct = (char) c + tmp; else return null;
				} else ct = MMConstants.CONTENT_TYPES[(c & 0x7F)]; 			
  	
				// the inputstream might contain some parameter, parse them
				ct += decodeParameters(in);					
			}
	  					
	  		
			return ct;

		} else i &= 127; // short form
		
		if (i < MMConstants.CONTENT_TYPES.length)
			ct = MMConstants.CONTENT_TYPES[i];
		else {		
			ct = (char) i + decodeString(res);	// encoded version not found
		}		
		
		return ct;
	}

	/**
	 * get message class from string, it's a byte if pre-defined
	 */
	protected static String decodeMessageClass(ByteArrayInputStream res) throws Exception {
		int i = (decodeInt(res) & 0xFF);
	
		if (i < MMConstants.MESSAGE_CLASSES.length) return MMConstants.MESSAGE_CLASSES[i];
			
		return (char) i + decodeString(res);	// encoded version not found						
	}
	
	/**
	 * get message class (int) given a string 
	 */
	protected static int getMessageClass(String s) {
		for (int i = 0; i < MMConstants.MESSAGE_CLASSES.length; i++)
			if (MMConstants.MESSAGE_CLASSES[i].equalsIgnoreCase(s)) return i;
		return -1;
	}
	

	/**
	 * get message type from string, it's a byte if predefined
	 */
	protected static String decodeMessageType(ByteArrayInputStream res) throws Exception {
		int i = (decodeInt(res) & 0xFF);
			
		if (i < MMConstants.MESSAGE_TYPES.length) return MMConstants.MESSAGE_TYPES[i];
		
		return (char) i + decodeString(res);	// encoded version not found		
	}
	
	/**
	 * get message type (int) given a string 
	 */
	protected static int getMessageType(String s) {
		for (int i = 0; i < MMConstants.MESSAGE_TYPES.length; i++)
			if (MMConstants.MESSAGE_TYPES[i].equalsIgnoreCase(s)) return i;
		return -1;
	}

	/**
	 * get message class from string, it's a byte if pre-defined
	 */
	protected static String decodePriority(ByteArrayInputStream res) throws Exception {
		int i = (decodeInt(res) & 0xFF);
	
		if (i < MMConstants.PRIORITIES.length) return MMConstants.PRIORITIES[i];
			
		return (char) i + decodeString(res);	// encoded version not found		
	}
	
	/**
	 * get message type (int) given a string 
	 */
	protected static int getPriority(String s) {
		for (int i = 0; i < MMConstants.PRIORITIES.length; i++)
			if (MMConstants.PRIORITIES[i].equalsIgnoreCase(s)) return i;
		return -1;
	}
	
	/**
	 * get response status, it's a byte if predefined
	 */
	protected static String decodeResponseStatus(ByteArrayInputStream res) throws Exception {
		int i = (decodeInt(res) & 0xFF);

		if (i < MMConstants.RESPONSE_STATUSES.length) return MMConstants.RESPONSE_STATUSES[i];
	
		return (char) i + decodeString(res);	// encoded version not found		
	}	
	
	/**
	 * get message type (int) given a string 
	 */
	protected static int getResponseStatus(String s) {
		for (int i = 0; i < MMConstants.RESPONSE_STATUSES.length; i++)
			if (MMConstants.RESPONSE_STATUSES[i].equalsIgnoreCase(s)) return i;
		return -1;
	}	
	
	/**
	 * get response status, it's a byte if predefined
	 */
	protected static String decodeStatus(ByteArrayInputStream res) throws Exception {
		int i = decodeInt(res);

		if (i < MMConstants.STATUSES.length) return MMConstants.STATUSES[i];

		return (char) i + decodeString(res);	// encoded version not found				
	}	
	
	/**
	 * get message status (int) given a string 
	 */
	protected static int getMessageStatus(String s) {
		for (int i = 0; i < MMConstants.STATUSES.length; i++)
			if (MMConstants.STATUSES[i].equalsIgnoreCase(s)) return i;
		return -1;
	}
	
	
	/**
	 * get mms version
	 */
	protected static String decodeVersion(ByteArrayInputStream in) throws Exception {
		int i = decodeInt(in);
		
		String major = String.valueOf((i >> 4));
		String minor = "0";
		
		return major + "." + minor;
	}
	
	/**
	 * get from
	 */
	protected static String decodeFrom(ByteArrayInputStream res) throws Exception {
		int i = decodeUintvar(res); // length
		int token = decodeInt(res);
		
		if ((token & 0xFF) == 0) // address-present-token
			return decodeString(res);
			
		return null;		
	}	
		
	/**
	 * get charset
	 */
	protected static String decodeCharset(ByteArrayInputStream res) throws Exception {
		int i = decodeInteger(res);
		
		for (int c = 0; c < MMConstants.WELLKNOWN_CHARSETS.length; c++) {
			if (((Integer) MMConstants.WELLKNOWN_CHARSETS[c][1]).intValue() == i)
				return (String) MMConstants.WELLKNOWN_CHARSETS[c][0];
		}
		
		// something we don't understand
			
		return decodeString(res);		
	}
	
	/**
	 * Misc helper functions
	 */
	
	protected static void setAttribute(String name, String value, ArrayList names, ArrayList values) {
			int idx = names.indexOf(name);
			if (idx != -1) {
				names.set(idx, name);
				values.set(idx, value);
			} else {
				names.add(name);
				values.add(value);
			}
		}

	protected static String versionToString(int version) {
			if (version == 0) version = MMConstants.MMS_VERSION_1_0;
		
			String major = String.valueOf((version >> 4));
			String minor = "0";
		
			return major + "." + minor;	
		}
	
	protected static String formatDate(Date d) {
			// transform to UTC (GMT)
			SimpleTimeZone stz = new SimpleTimeZone(0, "GMT");
			Calendar cal = Calendar.getInstance(stz);
		
			SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'");
			formatter.setCalendar(cal);
		
			return formatter.format(d); 
		}	
		
	protected static Date dateFromString(String s) {
		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) 				
				break;
			
			try {
				SimpleDateFormat formatter = dateFormats[i++];
				date = formatter.parse(s);
			} catch (Exception e) {				
				continue;
			}
			success = true;
		}
		
		return date;
	}
		
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -