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

📄 mmsencoder.java.svn-base

📁 编解码MMS彩信的Java类库
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) 2008 Andrea Zito *  * This file is part of jMmsLib. * * jMmsLib is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as  * published by the Free Software Foundation, either version 3 of  * the License, or  (at your option) any later version. * * jMmsLib is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public  * License along with jMmsLib.  If not, see <http://www.gnu.org/licenses/>. */ package net.sourceforge.jmmslib;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.util.Date;import java.util.Iterator;/** * Encodes an MmsMessage class in a binary MMS object ready to be sent to an MMSC. * <p>The encoding follows the specification provided by  * <a href="http://www.openmobilealliance.org/tech/affiliates/wap/wapindex.html">Open Mobile Alliance</a>. Relevant documents are * <i>WAP-209-MMSEncapsulation-20020105-a</i> and <i>WAP-230-WSP-20010705-a</i>.</p> * <p> * 	A simple usage scenario of MmsEncoder is as follow: * 	<pre> * 	  MmsMessage mms = new MmsMessage(); * 	   * 	  //fill out mms fields... * 	  MmsEncoder mmsEncoder = new MmsEncoder(mms); * 	  byte[] encodedMMS = mmmsEncoder.encodeMessage(); * 	</pre * </p> * <p> * 	To see what are the supported fields and values of an mms refer to the documentation of MmsMessage * </p> *  * @author Andrea Zito * @see MmsMessage * @version 0.8 * */public class MmsEncoder {	/*=========================================================================	 * Header Name Constants	 *=========================================================================*/	protected static final byte H_BASE = (byte)0x80;		protected static final byte H_MMS_MESSAGE_TYPE = (byte)0x0C;	protected static final byte H_MMS_TRANSACTION_ID = (byte)0x18;	protected static final byte H_MMS_VERSION = (byte)0x0D;	protected static final byte H_MMS_DATE = (byte)0x05;		protected static final byte H_MMS_FROM = (byte)0x09;		protected static final byte H_MMS_TO = (byte)0x17;		protected static final byte H_MMS_CC = (byte)0x02;		protected static final byte H_MMS_BCC = (byte)0x01;			protected static final byte H_MMS_SUBJECT = (byte)0x16;			protected static final byte H_MMS_CLASS = (byte)0x0A;				protected static final byte H_MMS_EXPIRY = (byte)0x08;			protected static final byte H_MMS_DELIVERY_TIME = (byte)0x07;				protected static final byte H_MMS_PRIORITY = (byte)0x0F;			protected static final byte H_MMS_SENDER_VISIBILITY = (byte)0x14;				protected static final byte H_MMS_DELIVERY_REPORT = (byte)0x06;				protected static final byte H_MMS_READ_REPLY = (byte)0x10;				protected static final byte H_MMS_CONTENT_TYPE = (byte)0x04;				protected static final byte H_MMS_RESPONSE_STATUS = (byte)0x12;				protected static final byte H_MMS_RESPONSE_TEXT = (byte)0x13;			protected static final byte H_MMS_MESSAGE_ID = (byte)0x0B;		protected static final byte P_CHARSET = (byte)0x81;		/*=========================================================================	 * Header Value Constants	 *=========================================================================*/	public static final byte NULL_CHAR = (byte)0x00;		public static final byte CHARSET_US_ASCII = 0x03;	public static final byte CHARSET_UTF8 = 0x6A;		public static final byte MMS_MESSAGE_TYPE_SEND_REQUEST = (byte)0x80;	public static final byte MMS_MESSAGE_TYPE_SEND_CONF = (byte)0x81;	public static final byte MMS_VERSION_1 = (byte)0x90;	private static final byte MMS_SENDER_PRESENT = (byte)0x80;	private static final byte MMS_SENDER_INSERT = (byte)0x81;		public static final byte MMS_DELIVERY_REPORT_YES = (byte)0x80;	public static final byte MMS_DELIVERY_REPORT_NO = (byte)0x81;		public static final byte MMS_CLASS_PERSONAL = (byte)0x80;	public static final byte MMS_CLASS_ADVERISEMENT = (byte)0x81;	public static final byte MMS_CLASS_INFORMATIONAL = (byte)0x82;	public static final byte MMS_CLASS_AUTO = (byte)0x83;		private static final byte MMS_EXPIRY_TIME_ABSOLUTE = (byte)0x80;	private static final byte MMS_EXPIRY_TIME_RELATIVE = (byte)0x81;		private static final byte MMS_DELIVERY_TIME_ABSOLUTE = (byte)0x80;	private static final byte MMS_DELIVERY_TIME_RELATIVE = (byte)0x81;			public static final byte MMS_PRIORITY_LOW = (byte)128;	public static final byte MMS_PRIORITY_NORMAL = (byte)129;	public static final byte MMS_PRIORITY_HIGH = (byte)130;		public static final byte MMS_READ_REPLY_YES = (byte)128;	public static final byte MMS_READ_REPLY_NO = (byte)129;		public static final byte MMS_RESPONSE_STATUS_OK = (byte)0x80; 	public static final byte MMS_RESPONSE_STATUS_ERROR_UNSPECIFIED = (byte)0x81;	public static final byte MMS_RESPONSE_STATUS_ERROR_SERVICE_DENIED = (byte)0x82;	public static final byte MMS_RESPONSE_STATUS_ERROR_MESSAGE_FORMAT_CORRUPT = (byte)0x83;	public static final byte MMS_RESPONSE_STATUS_ERROR_SENDING_ADDRESS_UNRESOLVED = (byte)0x84;	public static final byte MMS_RESPONSE_STATUS_ERROR_MESSAGE_NOT_FOUND = (byte)0x85;	public static final byte MMS_RESPONSE_STATUS_ERROR_NETWORK_PROBLEM = (byte)0x86;	public static final byte MMS_RESPONSE_STATUS_ERROR_CONTENT_NOT_ACCEPTED = (byte)0x87;	public static final byte MMS_RESPONSE_STATUS_ERROR_UNSUPPORTED_MESSAGE = (byte)0x88; 		public static final byte MMS_SENDER_VISIBILITY_HIDE = (byte)128;	public static final byte MMS_SENDER_VISIBILITY_SHOW = (byte)129;		public static final String MMS_ADDRESS_TYPE_MOBILE_NUMBER = "/TYPE=PLMN";	public static final String MMS_ADDRESS_TYPE_MAIL = "";	public static final String MMS_ADDRESS_TYPE_IPV4 = "/TYPE=IPV4";	public static final String MMS_ADDRESS_TYPE_IPV6 = "/TYPE=IPV6";	/*=========================================================================	 * CONTENT TYPE CONSTANTS	 *=========================================================================*/		public static final byte CTYPE_UNKNOWN = 0x00;	public static final byte CTYPE_TEXT = 0x01;	public static final byte CTYPE_TEXT_HTML =  0x02;	public static final byte CTYPE_TEXT_PLAIN = 0x03;	public static final byte CTYPE_TEXT_WML =  0x08;	public static final byte CTYPE_IMAGE = 0x1C;	public static final byte CTYPE_IMAGE_JPEG = 0x1E;	public static final byte CTYPE_IMAGE_GIF  = 0x1D;	public static final byte CTYPE_IMAGE_TIFF = 0x1F;	public static final byte CTYPE_IMAGE_PNG  = 0x20;	public static final byte CTYPE_IMAGE_VND_WAP_WBMP = 0x21;	public static final byte CTYPE_MULTIPART =  0x0B;	public static final byte CTYPE_MULTIPART_MIXED = 0x0C;	public static final byte CTYPE_APPLICATION_MULTIPART_MIXED = 0x23;	public static final byte CTYPE_APPLICATION_MULTIPART_RELATED = 0x33;				/*=========================================================================	 * CLASS VARIABLES	 *=========================================================================*/	private MmsMessage mmsMessage;		/*=========================================================================	 * CONSTRUCTORS	 *=========================================================================*/	/**	 * Creates an MmsEncoder object for the specified MMS message	 */	public MmsEncoder(MmsMessage mms){		this.mmsMessage = mms;	}		/*=========================================================================	 * METHODS	 *=========================================================================*/	/**	 * Encodes the message type.	 * @return encoded byte	 * @see MmsMessage#setMessageType(String)	 */	private byte encodeMessageType(String messageType) throws MmsEncodingException{		if (messageType.equals(MmsMessage.MMS_MESSAGE_TYPE_SEND_REQUEST)) return MMS_MESSAGE_TYPE_SEND_REQUEST;		else if (messageType.equals(MmsMessage.MMS_MESSAGE_TYPE_SEND_CONF)) return MMS_MESSAGE_TYPE_SEND_CONF;		else throw new MmsEncodingException("Cannot encode message type: " + messageType);	}	/**	 * Encodes a long value in the LongInteger format as	 * specified by the WAP-203-WSP document.<br>	 * 	 * The fist field of the returned array contains the number of	 * 8-bit portions that forms the value	 * @param value long to be encoded	 * @return encoded byte array	 */	private static byte[] encodeLongInteger(long value){		byte[] buf = encodeMultiOctetInteger(value);		byte[] result = new byte[buf.length+1];		result[0] = (byte)buf.length;		System.arraycopy(buf, 0, result, 1, buf.length);		return result;	}		/**	 * Encodes a long value in the MultiOctetInteger format as	 * specified by the WAP-203-WSP document.<br>	 * 	 * The binary representation is splitted in 8-bit portions.	 * @param value long integer to be encoded	 * @return encoded byte array	 */	private static byte[] encodeMultiOctetInteger(long value){		byte[] buf = new byte[8]; 		long temp=0;		int count=0;		long mask=0;				if (value == 0) return new byte[]{0x0};				for (int i=1; i<=8; i++){			mask = ((0XFFFFFFFFFFFFFFFFL) >>> 64-(8*i));			temp = (value & mask) >>> (8*(i-1));			buf[i-1] = (byte)(temp);			if (temp==0) count++;			else count=0;		}				byte[] result = new byte[8-count];		int d = 0;		for (int i=7-count; i>=0; i--) result[d++] = buf[i];		return result;			}		/**	 * Encodes an unsigned integer in the "Uintvar" format as	 * specified by WAP-203-WDP document.<br>	 * 	 * The binary representation of the value is splitted in 	 * 7-bit portions. The most significant bit is setted to 1	 * in all the portions but the last.	 * 	 * Example: 0x87A5 (1000 0111 1010 0101)	 * 	 * <pre>	 * +---+---------+  +---+---------+  +---+---------+   	 * | 1 | 0000010 |  | 1 | 0001111 |  | 0 | 0100101 |	 * +-------------+  +---+---------+  +---+---------+	 * </pre>	 * @param value integer to encode	 * @return encoded byte array	 */	private static byte[] encodeUintvar(int value){		byte[] buf = new byte[5];		int temp=0;		int count=0;		int mask=0;		if (value == 0) return new byte[]{0x0};				for (int i=1; i<=5; i++){			mask = ((0XFFFFFFFF) >>> 32-(7*i));			temp = (value & mask) >>> (7*(i-1));			buf[i-1] = (byte)(0x80 | temp);			if (temp==0) count++;			else count=0;		}				buf[0] &= 0x7F;		byte[] result = new byte[5-count];		int d = 0;		for (int i=4-count; i>=0; i--) result[d++] = buf[i];		return result;	}	/**	 * Encode the MMS version.<br>	 *	 * Actually only version 1 is supported.	 * @param version MMS version of the message	 * @return encoded version byte	 * @throws MmsEncodingException MMS version not supported	 * @see MmsMessage#setVersion(String)	 */	private byte encodeVersion(String version) throws MmsEncodingException{		if (version.equals(MmsMessage.MMS_VERSION_1)) return MMS_VERSION_1;		else throw new MmsEncodingException("MMS version not supported: " + version);	}		/**	 * Encodes a date.<br> 	 * 	 * Dates are expressed in seconds as long integer values starting from 1970/01/01 00:00:00 GMT. 	 * 	 * @param d date to encode	 * @return encoded byte array	 */	private byte[] encodeDate(Date d){		long seconds = d.getTime()/1000;		byte[] encodedDate = encodeLongInteger(seconds);		return encodedDate;	}		/**	 * Encodes the sender visibility.	 *	 * @param visibility sender visibility	 * @return encoded byte	 * @throws MmsEncodingException Sender visibility not supported	 * @see MmsMessage#setSenderVisibility(String)	 */	private byte encodeSenderVisibility(String visibility) throws MmsEncodingException{		if (visibility.equals(MmsMessage.MMS_SENDER_VISIBILITY_SHOW))			return MMS_SENDER_VISIBILITY_SHOW;		else if (visibility.equals(MmsMessage.MMS_SENDER_VISIBILITY_HIDE))			return MMS_SENDER_VISIBILITY_HIDE;		else throw new MmsEncodingException("Sender visibility not supported: " + visibility);				}		/**	 * Encodes the message class.	 * 	 * @param msgClass message class	 * @return encoded byte	 * @throws MmsEncodingException Message class not supported	 * @see MmsMessage#setMessageClass(String)	 */	private byte encodeMessageClass(String msgClass) throws MmsEncodingException{		if (msgClass.equals(MmsMessage.MMS_CLASS_PERSONAL)) return MMS_CLASS_PERSONAL;

⌨️ 快捷键说明

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