www.pudn.com > cmppNeu.rar > Deliver.java


package cmpp.v2_0; 
 
import java.io.UnsupportedEncodingException; 
import java.util.Arrays; 
 
public class Deliver extends CMPP 
{ 
 
	private  final int commandId 		= CMPP.ID_CMPP_DELIVER; 
    private  final int commandLength 	= 73; 
    private	 final int maxMsgLength0	= 160; 
    private	 final int maxMsgLength1	= 140; 
 
    private	long	MsgId		= 0;	 
    private	String	DestId		= null; 
    private	String	ServiceId	= null; 
    private	int		TPPid		= 0; 
    private	int		TPUdhi		= 0; 
    private	int		MsgFmt		= 0; 
    private	String	SrcTerminalId	= null; 
    private	int		RegisteredDelivery = 0; 
    private	int		MsgLength	= 0; 
    private byte[]	MsgContent	= null; 
    private String	Reserved	= null; 
     
    private	long	ReportMsgId	= 0; 
    private	String	ReportStat  = null; 
    private	String	ReportSubmitTime=null; 
    private	String	ReportDoneTime=null; 
    private	String	ReportDestTerminalId=null; 
    private	int		ReportSMSCSequence=0; 
     
    public String getReportDestTerminalId() { 
		return ReportDestTerminalId; 
	} 
 
	public void setReportDestTerminalId(String reportDestTerminalId) { 
		ReportDestTerminalId = reportDestTerminalId; 
	} 
 
	public String getReportDoneTime() { 
		return ReportDoneTime; 
	} 
 
	public void setReportDoneTime(String reportDoneTime) { 
		ReportDoneTime = reportDoneTime; 
	} 
 
	public long getReportMsgId() { 
		return ReportMsgId; 
	} 
 
	public void setReportMsgId(long reportMsgId) { 
		ReportMsgId = reportMsgId; 
	} 
 
	public int getReportSMSCSequence() { 
		return ReportSMSCSequence; 
	} 
 
	public void setReportSMSCSequence(int reportSMSCSequence) { 
		ReportSMSCSequence = reportSMSCSequence; 
	} 
 
	public String getReportStat() { 
		return ReportStat; 
	} 
 
	public void setReportStat(String reportStat) { 
		ReportStat = reportStat; 
	} 
 
	public String getReportSubmitTime() { 
		return ReportSubmitTime; 
	} 
 
	public void setReportSubmitTime(String reportSubmitTime) { 
		ReportSubmitTime = reportSubmitTime; 
	} 
 
	public Deliver()  { 
    	super(CMPP.ID_CMPP_DELIVER); 
    } 
 
    public Deliver(CMPP cmpp) 
    { 
        super(cmpp); 
    } 
 
	public int getCommandId() { 
		return commandId; 
	} 
 
	public int getCommandLength() { 
		return commandLength; 
	} 
     
	public String getDestId() { 
		return DestId; 
	} 
 
	public void setDestId(String destId) { 
		DestId = destId; 
	} 
 
	public byte[] getMsgContent() { 
		return MsgContent; 
	} 
 
	/** 
	 * 得到消息的16进制文本。 
	 */ 
	public String getMsgHexText() { 
    	String msgText = null; 
		 
    	if( MsgContent == null )  
			return null; 
 
		//0:纯ASCII字符串 
        //3:写卡操作 
        //4:二进制编码 
        //8:UCS2编码 
        //15: GBK编码 
        switch( MsgFmt ) { 
	        case 0: 
	        	msgText = byteToHexString(MsgContent,"US-ASCII"); 
	        	break; 
	        case 3: 
	        	msgText = byteToHexString(MsgContent,"US-ASCII"); 
	        	break; 
	        case 4: 
	        	msgText = byteToHexString(MsgContent,"BIN"); 
	        	break; 
	        case 8: 
	        	msgText = byteToHexString(MsgContent,"ISO-10646-UCS-2"); 
	        	break; 
	        case 15: 
	        	msgText = byteToHexString(MsgContent,"GBK"); 
	        	break; 
        	default: 
        		msgText = byteToHexString(MsgContent, null); 
        } 
        return msgText; 
	} 
 
	/** 
	 * 根据当前的MsgFmt得到MsgContent文本 
	 */ 
	public String getMsgText() { 
		String msgText = null; 
		 
		if( MsgContent == null )  
			return null; 
		 
        //0:纯ASCII字符串 
        //3:写卡操作 
        //4:二进制编码 
        //8:UCS2编码 
        //15: GBK编码 
    	try { 
	        switch( MsgFmt ) { 
		        case 0: 
					msgText = new String(MsgContent, "US-ASCII"); 
		        	break; 
		        case 3: 
		        	msgText = new String(MsgContent, "US-ASCII"); 
		        	break; 
		        case 4: 
		        	msgText = toPrintableString(MsgContent); 
		        	break; 
		        case 8: 
		        	msgText = new String(MsgContent, "ISO-10646-UCS-2"); 
		        	break; 
		        case 15: 
		        	msgText = new String(MsgContent, "GBK"); 
		        	break; 
	        	default: 
	        		msgText = toPrintableString(MsgContent); 
	        } 
		} catch (UnsupportedEncodingException e) { 
			msgText = toPrintableString(MsgContent); 
		} 
        return msgText; 
	} 
 
	 
	/** 
	 * 设置二进制内容 
	 */ 
	public void setMsgContent(byte[] msgContent, int msgFmt) { 
		// set 
		MsgFmt	   = msgFmt; 
		MsgContent = msgContent; 
		if( MsgContent == null ) { 
			MsgLength = 0; 
		} else { 
			if( msgFmt == 0 ) { 
				if( msgContent.length > maxMsgLength0 ) { 
					MsgContent = new byte[maxMsgLength0]; 
					CMPP.BytesCopy(msgContent,MsgContent,0,maxMsgLength0-1,0); 
				} 
			} else { 
				if( msgContent.length > maxMsgLength1 ) { 
					MsgContent = new byte[maxMsgLength1]; 
					CMPP.BytesCopy(msgContent,MsgContent,0,maxMsgLength1-1,0); 
				} 
			} 
			MsgLength  = MsgContent.length; 
		} 
	} 
	/** 
	 * 设置文本内容 
	 * 信息格式: 0:ASCII串 3:短信写卡操作 4:二进制信息 8:UCS2编码 15:含GB汉字 
	 */ 
	public void setMsgText(String msgText, int msgFmt) { 
 
		byte[]	binCnt = null; 
        //0:纯ASCII字符串 
        //3:写卡操作 
        //4:二进制编码 
        //8:UCS2编码 
        //15: GBK编码 
    	try { 
	        switch( msgFmt ) { 
		        case 0: 
		        	binCnt = msgText.getBytes("US-ASCII"); 
		        	break; 
		        case 3: 
		        	binCnt = msgText.getBytes("US-ASCII"); 
		        case 4: 
		        	binCnt = msgText.getBytes("US-ASCII"); 
		        case 8: 
		        	binCnt = msgText.getBytes("ISO-10646-UCS-2"); 
		        	break; 
		        case 15: 
		        	binCnt = msgText.getBytes("GBK"); 
		        	break; 
	        	default: 
	        		binCnt = msgText.getBytes(); 
	        } 
		} catch (UnsupportedEncodingException e) { 
			binCnt = msgText.getBytes(); 
		} 
		// set 
		setMsgContent(binCnt,msgFmt); 
	} 
 
	public int getMsgFmt() { 
		return MsgFmt; 
	} 
 
	public long getMsgId() { 
		return MsgId; 
	} 
 
	public void setMsgId(long msgId) { 
		MsgId = msgId; 
	} 
 
	public int getMsgLength() { 
		return MsgLength; 
	} 
 
	public int getRegisteredDelivery() { 
		return RegisteredDelivery; 
	} 
 
	public void setRegisteredDelivery(int registeredDelivery) { 
		RegisteredDelivery = registeredDelivery; 
		if( RegisteredDelivery == 1 ) { 
			MsgLength = 60; 
			MsgFmt = 4; 
		} 
	} 
 
	public String getReserved() { 
		return Reserved; 
	} 
 
	public void setReserved(String reserved) { 
		Reserved = reserved; 
	} 
 
	public String getServiceId() { 
		return ServiceId; 
	} 
 
	public void setServiceId(String serviceId) { 
		ServiceId = serviceId; 
	} 
 
	public String getSrcTerminalId() { 
		return SrcTerminalId; 
	} 
 
	public void setSrcTerminalId(String srcTerminalId) { 
		SrcTerminalId = srcTerminalId; 
	} 
 
	public int getTPPid() { 
		return TPPid; 
	} 
 
	public void setTPPid(int pid) { 
		TPPid = pid; 
	} 
 
	public int getTPUdhi() { 
		return TPUdhi; 
	} 
 
	public void setTPUdhi(int udhi) { 
		TPUdhi = udhi; 
	} 
    /** 
     * 字节数组转换成16进制可打印字符串 
     * @param b 
     * @param charset 
     * @return 
     */ 
    private String byteToHexString(byte[] b, String charset) { 
    	String  str = null; 
    	 
    	if( b == null ) { 
    		return null; 
    	} 
    	if( charset != null ) { 
	    	try { 
				str = new String(b, charset); 
			} catch (UnsupportedEncodingException e1) { 
	    		StringBuffer sb = new StringBuffer();  
	    		for( int i=0; i>4	,16); 
	    			char chLow = Character.forDigit((b[i]&0x0F)	,16); 
	    			sb.append(chHi); 
	    			sb.append(chLow); 
	    			sb.append(' '); 
	    		} 
	    		str = sb.toString().toUpperCase(); 
			} 
    	} else { 
			str = new String(b); 
    	} 
    	return str; 
    } 
 
	/** 
	 * to protableString 
	 * @param b 
	 * @return 
	 */ 
	private String toPrintableString(byte[] b) { 
 
		if( b == null ) return null; 
		 
		StringBuffer sb = new StringBuffer(); 
		byte[] t = new byte[1]; 
		for( int i=0; i=0x20 && b[i]<=0x7e ) {	// printable char 
				t[0] = b[i]; 
				sb.append(new String(t)); 
			} else { 							//non-pritable char 
				sb.append("."); 
			} 
		} 
		return sb.toString(); 
	} 
 
	/** 
	 *  
	 */ 
	protected int parseBody() { 
		 
    	if( super.bodyLength < commandLength ) {	//length too short. 
    		return -1; 
    	} 
    	 
    	byte[] abyte0 = new byte[21]; 
    	int off = 0; 
 
    	Arrays.fill(abyte0,(byte)0); 
    	CMPP.BytesCopy(super.bodybytes, abyte0, off, off+7, 0); 
    	MsgId = CMPP.Bytes8ToLong(abyte0);  
    	off += 8; 
    	 
    	Arrays.fill(abyte0,(byte)0); 
    	CMPP.BytesCopy(super.bodybytes, abyte0, off, off+20, 0); 
    	DestId = new String(abyte0,0,21); 
    	off += 21; 
    	 
    	Arrays.fill(abyte0,(byte)0); 
    	CMPP.BytesCopy(super.bodybytes, abyte0, off, off+9, 0); 
    	ServiceId = new String(abyte0, 0, 10); 
    	off += 10; 
 
    	Arrays.fill(abyte0,(byte)0); 
    	CMPP.BytesCopy(super.bodybytes, abyte0, off, off, 0); 
    	TPPid = CMPP.ByteToInt(abyte0[0]); 
    	off += 1; 
 
    	Arrays.fill(abyte0,(byte)0); 
    	CMPP.BytesCopy(super.bodybytes, abyte0, off, off, 0); 
    	TPUdhi = CMPP.ByteToInt(abyte0[0]); 
    	off += 1; 
 
    	Arrays.fill(abyte0,(byte)0); 
    	CMPP.BytesCopy(super.bodybytes, abyte0, off, off, 0); 
    	MsgFmt = CMPP.ByteToInt(abyte0[0]); 
    	off += 1; 
 
    	Arrays.fill(abyte0,(byte)0); 
    	CMPP.BytesCopy(super.bodybytes, abyte0, off, off+20, 0); 
    	SrcTerminalId = new String(abyte0,0,21); 
    	off += 21; 
    	 
    	Arrays.fill(abyte0,(byte)0); 
    	CMPP.BytesCopy(super.bodybytes, abyte0, off, off, 0); 
    	RegisteredDelivery = CMPP.ByteToInt(abyte0[0]); 
    	off += 1; 
 
    	Arrays.fill(abyte0,(byte)0); 
    	CMPP.BytesCopy(super.bodybytes, abyte0, off, off, 0); 
    	MsgLength = CMPP.ByteToInt(abyte0[0]); 
    	off += 1; 
 
    	MsgContent = new byte[MsgLength]; 
    	CMPP.BytesCopy(super.bodybytes, MsgContent, off, off+MsgLength-1, 0); 
    	off += MsgLength; 
 
    	Arrays.fill(abyte0,(byte)0); 
    	CMPP.BytesCopy(super.bodybytes, abyte0, off, off+7, 0); 
    	Reserved = new String(abyte0,0,8); 
    	off += 8; 
    	 
    	if( RegisteredDelivery == 1 ) { 
    		off = 0; 
 
    		Arrays.fill(abyte0,(byte)0); 
        	CMPP.BytesCopy(MsgContent, abyte0, off, off+7, 0); 
        	ReportMsgId = CMPP.Bytes8ToLong(abyte0);  
        	off += 8; 
 
        	Arrays.fill(abyte0,(byte)0); 
        	CMPP.BytesCopy(MsgContent, abyte0, off, off+6, 0); 
        	ReportStat = new String(abyte0,0,7); 
        	off += 7; 
        	 
        	Arrays.fill(abyte0,(byte)0); 
        	CMPP.BytesCopy(MsgContent, abyte0, off, off+9, 0); 
        	ReportSubmitTime = new String(abyte0,0,10); 
        	off += 10; 
        	 
        	Arrays.fill(abyte0,(byte)0); 
        	CMPP.BytesCopy(MsgContent, abyte0, off, off+9, 0); 
        	ReportDoneTime = new String(abyte0,0,10); 
        	off += 10; 
        	 
        	Arrays.fill(abyte0,(byte)0); 
        	CMPP.BytesCopy(MsgContent, abyte0, off, off+20, 0); 
        	ReportDestTerminalId = new String(abyte0,0,21); 
        	off += 21; 
        	 
        	Arrays.fill(abyte0,(byte)0); 
        	CMPP.BytesCopy(MsgContent, abyte0, off, off+3, 0); 
        	ReportSMSCSequence = CMPP.Bytes4ToInt(abyte0);  
        	off += 4; 
    	} 
    	 
    	return 0; 
    } 
 
	/** 
	 *  
	 */ 
	protected int makeBody() { 
 
    	// make bodybytes 
    	super.bodyLength = commandLength + MsgLength;  
    	super.bodybytes  = new byte[super.bodyLength]; 
    	Arrays.fill(super.bodybytes,(byte)0); 
 
    	// make parameter 
    	if( MsgId == 0 ) { 
    		MsgId = 123456; 
    	} 
    	if( DestId == null ) { 
    		DestId = ""; 
    	} 
    	if( ServiceId == null ) { 
    		ServiceId = ""; 
    	} 
    	if( SrcTerminalId == null ) { 
    		SrcTerminalId = ""; 
    	} 
    	if( Reserved == null ) { 
    		Reserved = ""; 
    	} 
    	if( ReportStat == null ) { 
    		ReportStat = ""; 
    	} 
    	if( ReportSubmitTime == null ) { 
    		ReportSubmitTime = ""; 
    	} 
    	if( ReportDoneTime == null ) { 
    		ReportDoneTime = ""; 
    	} 
    	if( ReportDestTerminalId == null ) { 
    		ReportDestTerminalId = ""; 
    	} 
 
    	// make body 
    	int off = 0; 
    	CMPP.BytesCopy(CMPP.LongToBytes8(MsgId), super.bodybytes, 0, 7, off ); 
    	off += 8; 
    	CMPP.BytesCopy(DestId.getBytes(), super.bodybytes, 0, 20, off ); 
    	off += 21; 
    	CMPP.BytesCopy(ServiceId.getBytes(), super.bodybytes, 0, 9, off ); 
    	off += 10; 
    	super.bodybytes[off] = CMPP.IntToByte(TPPid); 
    	off += 1; 
    	super.bodybytes[off] = CMPP.IntToByte(TPUdhi); 
    	off += 1; 
    	super.bodybytes[off] = CMPP.IntToByte(MsgFmt); 
    	off += 1; 
    	CMPP.BytesCopy(SrcTerminalId.getBytes(), super.bodybytes, 0, 20, off ); 
    	off += 21; 
    	super.bodybytes[off] = CMPP.IntToByte(RegisteredDelivery); 
    	off += 1; 
    	super.bodybytes[off] = CMPP.IntToByte(MsgLength); 
    	off += 1; 
 
    	if( RegisteredDelivery == 1 ) { 
    		int off1 = 0; 
    		MsgContent = new byte[MsgLength]; 
 
    		CMPP.BytesCopy(CMPP.LongToBytes8(ReportMsgId), MsgContent, 0, 7, off1 ); 
        	off1 += 8; 
    		CMPP.BytesCopy(ReportStat.getBytes(), MsgContent, 0, 6, off1 ); 
        	off1 += 7; 
    		CMPP.BytesCopy(ReportSubmitTime.getBytes(), MsgContent, 0, 9, off1 ); 
        	off1 += 10; 
    		CMPP.BytesCopy(ReportDoneTime.getBytes(), MsgContent, 0, 9, off1 ); 
        	off1 += 10; 
    		CMPP.BytesCopy(ReportDestTerminalId.getBytes(), MsgContent, 0, 20, off1 ); 
        	off1 += 21; 
    		CMPP.BytesCopy(CMPP.IntToBytes4(ReportSMSCSequence), MsgContent, 0, 3, off1 ); 
        	off1 += 4; 
    	} 
    	 
    	CMPP.BytesCopy(MsgContent,super.bodybytes, 0, MsgLength-1, off ); 
    	off += MsgLength; 
    	CMPP.BytesCopy(Reserved.getBytes(), super.bodybytes, 0, 7, off ); 
    	off += 8; 
 
    	return 0; 
	} 
	 
	public String toString() { 
		StringBuffer sb = new StringBuffer(); 
		 
		sb.append("MsgId=" + MsgId + "\n"); 
		sb.append("DestId=" + DestId + "\n"); 
		sb.append("ServiceId=" + ServiceId + "\n"); 
		sb.append("TPPid=" + TPPid + "\n"); 
		sb.append("TPUdhi=" + TPUdhi + "\n"); 
		sb.append("MsgFmt=" + MsgFmt + "\n"); 
		sb.append("SrcTerminalId=" + SrcTerminalId + "\n"); 
		sb.append("RegisteredDelivery=" + RegisteredDelivery + "\n"); 
		sb.append("MsgLength=" + MsgLength + "\n"); 
		sb.append("MsgContent=" + MsgContent + "\n"); 
		sb.append("Reserved=" + Reserved + "\n"); 
		 
		return sb.toString(); 
	} 
	 
}