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


package cmpp.v2_0; 
 
import java.io.UnsupportedEncodingException; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 
 
public class Submit extends CMPP 
{ 
    private  final int commandId 		= CMPP.ID_CMPP_SUBMIT; 
    private  final int commandLength 	= 126; 
    private	 final int maxMsgLength0	= 160; 
    private	 final int maxMsgLength1	= 140; 
     
    private	long	MsgId				= 0;				 
    private	int		PkTotal				= 1;				 
    private	int		PkNumber			= 1;			 
    private	int		RegisteredDelivery	= 0;	 
    private int		MsgLevel			= 0;			 
    private	String	ServiceId			= null;			 
    private int		FeeUserType			= 0;			 
    private String	FeeTerminalId		= null;		 
    private	int		TPPid				= 0; 
    private	int		TPUdhi				= 0; 
    private	int		MsgFmt				= 0; 
    private	String	MsgSrc				= null; 
    private	String	FeeType				= null; 
    private	String	FeeCode				= null; 
    private	String	ValidTime			= null; 
    private	String	AtTime				= null; 
    private	String	SrcId				= null; 
    private	int		DestUsrTl			= 0; 
    private	List 	DestTerminalId		= new ArrayList(); 
    private	int		MsgLength			= 0; 
    private byte[]  MsgContent			= null; 
    private	String	Reserve				= null; 
	 
	public Submit() { 
    	super(CMPP.ID_CMPP_SUBMIT); 
    } 
    public Submit(CMPP cmpp)  { 
        super(cmpp); 
    } 
 
	public int getCommandId() { 
		return commandId; 
	} 
	public int getCommandLength() { 
		return commandLength; 
	} 
 
	// AtTime 
	public String getAtTime() { 
		return AtTime; 
	} 
	public void setAtTime(String atTime) { 
		AtTime = atTime; 
	} 
	 
	//DestTerminalId 
	public List getDestTerminalId() { 
		return DestTerminalId; 
	} 
	public void addDestTerminalId(String destTerminalId) { 
		if( destTerminalId == null )  
			return; 
 
		DestTerminalId.add(destTerminalId); 
		DestUsrTl = DestTerminalId.size(); 
	} 
	public void removeDestTerminalId(String destTerminalId) { 
		if( destTerminalId == null )  
			return; 
 
		DestTerminalId.remove(destTerminalId); 
		DestUsrTl = DestTerminalId.size(); 
	} 
 
	//DestUsrTl 
	public int getDestUsrTl() { 
		return DestUsrTl; 
	} 
	 
	//FeeCode 
	public String getFeeCode() { 
		return FeeCode; 
	} 
	public void setFeeCode(String feeCode) { 
		FeeCode = feeCode; 
	} 
	 
	//FeeTerminalId 
	public String getFeeTerminalId() { 
		return FeeTerminalId; 
	} 
	public void setFeeTerminalId(String feeTerminalId) { 
		FeeTerminalId = feeTerminalId; 
	} 
 
	//FeeUserType 
	public int getFeeUserType() { 
		return FeeUserType; 
	} 
	public void setFeeUserType(int feeUserType) { 
		FeeUserType = feeUserType; 
	} 
	 
	//Reserve 
	public String getReserve() { 
		return Reserve; 
	} 
	public void setReserve(String reserve) { 
		Reserve = reserve; 
	} 
	 
	//MsgContent 
	/** 
	 * 根据当前的二进制内容 
	 */ 
	public byte[] getMsgContent() { 
		return MsgContent; 
	} 
	 
	/** 
	 * 根据当前的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; 
	} 
 
	/** 
	 * 得到消息的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; 
	} 
 
	/** 
	 * 设置二进制内容 
	 */ 
	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); 
	} 
 
	//MsgFmt 
	public int getMsgFmt() { 
		return MsgFmt; 
	} 
	 
	//MsgId 
	public long getMsgId() { 
		return MsgId; 
	} 
	public void setMsgId(long msgId) { 
		MsgId = msgId; 
	} 
 
	//MsgLength 
	public int getMsgLength() { 
		return MsgLength; 
	} 
 
	//MsgLevel  
	public int getMsgLevel() { 
		return MsgLevel; 
	} 
	public void setMsgLevel(int msgLevel) { 
		MsgLevel = msgLevel; 
	} 
 
	//MsgSrc 
	public String getMsgSrc() { 
		return MsgSrc; 
	} 
	public void setMsgSrc(String msgSrc) { 
		MsgSrc = msgSrc; 
	} 
	 
	// PkNumber 
	public int getPkNumber() { 
		return PkNumber; 
	} 
	public void setPkNumber(int pkNumber) { 
		PkNumber = pkNumber; 
	} 
	 
	//PkTotal 
	public int getPkTotal() { 
		return PkTotal; 
	} 
	public void setPkTotal(int pkTotal) { 
		PkTotal = pkTotal; 
	} 
	 
	//RegisteredDelivery 
	public int getRegisteredDelivery() { 
		return RegisteredDelivery; 
	} 
	public void setRegisteredDelivery(int registeredDelivery) { 
		RegisteredDelivery = registeredDelivery; 
	} 
	 
	//ServiceId 
	public String getServiceId() { 
		return ServiceId; 
	} 
	public void setServiceId(String serviceId) { 
		ServiceId = serviceId; 
	} 
	 
	//SrcId 
	public String getSrcId() { 
		return SrcId; 
	} 
	public void setSrcId(String srcId) { 
		SrcId = srcId; 
	} 
	 
	//TPPid 
	public int getTPPid() { 
		return TPPid; 
	} 
	public void setTPPid(int pid) { 
		TPPid = pid; 
	} 
 
	//TPUdhi 
	public int getTPUdhi() { 
		return TPUdhi; 
	} 
	public void setTPUdhi(int udhi) { 
		TPUdhi = udhi; 
	} 
	 
	//ValidTime 
	public String getValidTime() { 
		return ValidTime; 
	} 
	public void setValidTime(String validTime) { 
		ValidTime = validTime; 
	} 
	 
	//FeeType 
	public String getFeeType() { 
		return FeeType; 
	} 
	public void setFeeType(String feeType) { 
		FeeType = feeType; 
	} 
	 
    /** 
     * 字节数组转换成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(); 
	} 
 
	/** 
	 * parseBody  
	 */ 
	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, 0); 
    	PkTotal = CMPP.ByteToInt(abyte0[0]); 
    	off += 1; 
    	 
    	Arrays.fill(abyte0,(byte)0); 
    	CMPP.BytesCopy(super.bodybytes, abyte0, off, off, 0); 
    	PkNumber = CMPP.ByteToInt(abyte0[0]); 
    	off += 1; 
 
    	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); 
    	MsgLevel = CMPP.ByteToInt(abyte0[0]); 
    	off += 1; 
 
    	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); 
    	FeeUserType = CMPP.ByteToInt(abyte0[0]); 
    	off += 1; 
 
    	Arrays.fill(abyte0,(byte)0); 
    	CMPP.BytesCopy(super.bodybytes, abyte0, off, off+20, 0); 
    	FeeTerminalId = new String(abyte0,0,21); 
    	off += 21; 
 
    	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+5, 0); 
    	MsgSrc = new String(abyte0,0,6); 
    	off += 6; 
 
    	Arrays.fill(abyte0,(byte)0); 
    	CMPP.BytesCopy(super.bodybytes, abyte0, off, off+1, 0); 
    	FeeType = new String(abyte0,0,2); 
    	off += 2; 
 
    	Arrays.fill(abyte0,(byte)0); 
    	CMPP.BytesCopy(super.bodybytes, abyte0, off, off+5, 0); 
    	FeeCode = new String(abyte0,0,6); 
    	off += 6; 
 
    	Arrays.fill(abyte0,(byte)0); 
    	CMPP.BytesCopy(super.bodybytes, abyte0, off, off+16, 0); 
    	ValidTime = new String(abyte0,0,17); 
    	off += 17; 
 
    	Arrays.fill(abyte0,(byte)0); 
    	CMPP.BytesCopy(super.bodybytes, abyte0, off, off+16, 0); 
    	AtTime = new String(abyte0,0,17); 
    	off += 17; 
 
    	Arrays.fill(abyte0,(byte)0); 
    	CMPP.BytesCopy(super.bodybytes, abyte0, off, off+20, 0); 
    	SrcId = new String(abyte0,0,21); 
    	off += 21; 
 
    	Arrays.fill(abyte0,(byte)0); 
    	CMPP.BytesCopy(super.bodybytes, abyte0, off, off, 0); 
    	DestUsrTl = CMPP.ByteToInt(abyte0[0]); 
    	off += 1; 
 
    	for( int i=0; i