📄 smscshortmsg.java
字号:
msgSmDefaultMsgID = dataInStream.readByte() ;
msgSmLength = dataInStream.readByte();
int msgLen;
if( msgSmLength < 0) {
msgLen = 256 + msgSmLength ;
} else {
msgLen = msgSmLength ;
}
byte[] b = new byte[msgLen];
dataInStream.readFully(b);
if( msgDataCoding == 0)//Ascii
msgShortMsgText = new String(b);
else if( msgDataCoding == 8)//unicode
msgShortMsgText = new String(b,"UTF-16BE");
else if( msgDataCoding == 4)
msgShortMsgText = convertBytesToString(b);//!!!!!
else if(msgDataCoding == 15)
msgShortMsgText = new String(b,"gb2312");//!!!!!
else
msgShortMsgText = new String(b);
} catch(Exception e) {
//Log.error(" delivery IOException="+e);
headCmdID = 0;
}
return;
}
private boolean getBindRespCommand(DataOutputStream dataOutStream) {
int totalSize;
boolean bResp;
totalSize = 16 + bindSysID.length() + 1;
if(!getHead(dataOutStream, totalSize))
return false;
try {
bResp = writeString(dataOutStream, bindSysID);
if(bResp == false)
return false;
} catch(Exception e) {
//Log.error("Generate SMPP_BIND_RESP package exception : " + e.getMessage());
return false;
}
return true;
}
private boolean getHead(DataOutputStream dataOutStream, int totalSize) {
try {
dataOutStream.writeInt(totalSize);
dataOutStream.writeInt(headCmdID);
dataOutStream.writeInt(headCmdStatus);
dataOutStream.writeInt(headSeqcNo);
} catch(IOException e) {
//Log.error(" getHead has error="+e);
return false;
}
return true;
}
private int parseHead(DataInputStream dataInStream) {
try {
headCmdID = dataInStream.readInt();
headCmdStatus = dataInStream.readInt();
headSeqcNo = dataInStream.readInt();
return 0;
} catch(IOException e) {
//Log.error(" parse head has error:"+e) ;
nParseResult = 0;
return -1;
}
//return;
}
private boolean getSubmitAckCommand(DataOutputStream outStream) {
int totalSize;
totalSize = 16 + ackMsgID.length() + 1;
if(!getHead(outStream,totalSize))
return false;
return writeString(outStream, ackMsgID );
}
private boolean getDeliverCommand(DataOutputStream outStream) {
byte[] msgText = null;
int totalSize=0;
try {
if(msgShortMsgText == null)
msgShortMsgText = "";
if(msgDataCoding == 8 || msgDataCoding==15) //Unicode
{
if(msgShortMsgText.length() >80)
msgShortMsgText = msgShortMsgText.substring(0,79);
msgText = msgShortMsgText.getBytes("UTF-16BE");
msgDataCoding = 8;
} else if(msgDataCoding == 4 )
msgText = convertStringToBytes(msgShortMsgText);
else
msgText = msgShortMsgText.getBytes();
Integer msgTextLen = new Integer(msgText.length );
msgSmLength = msgTextLen.byteValue() ;
if(msgServiceType == null)
msgServiceType="";
if(msgSourceAddress == null)
msgSourceAddress="";
if(msgScheduleDeliveryTime == null)
msgScheduleDeliveryTime="";
if(msgValidityPeroid == null)
msgValidityPeroid="";
totalSize = 16 + 12 + msgServiceType.length() + 1
+ msgSourceAddress.length() + 1
+ msgDestAddress.length() + 1
+ msgScheduleDeliveryTime.length() + 1
+ msgValidityPeroid.length()+1
+ msgText.length;
} catch(Exception e) {
//Log.error (" getSubmitCommand has error="+e);
nParseResult = -1;
return false;
}
if(!getHead(outStream,totalSize))
return false;
try {
if(!writeString(outStream, msgServiceType ))
return false;
outStream.writeByte(msgSourceAddressTon);
outStream.writeByte(msgSourceAddressNpi);
if(!writeString(outStream,msgSourceAddress ))
return false;
outStream.writeByte(msgDestAddressTon);
outStream.writeByte(msgDestAddressNpi);
if(!writeString(outStream,msgDestAddress))
return false;
outStream.writeByte(msgEsmClass );
outStream.writeByte(msgProtocolID );
outStream.writeByte(msgPriorityFlag );
//msgScheduleDeliveryTime = "";
if(!writeString(outStream,msgScheduleDeliveryTime ) )
return false;
//msgValidityPeroid = "";
if(!writeString(outStream,msgValidityPeroid ))
return false;
outStream.writeByte(msgRegisterdDeliveryFlag );
msgReplaceIfPresentFlag=0;
outStream.writeByte(msgReplaceIfPresentFlag );
outStream.writeByte(msgDataCoding );
msgSmDefaultMsgID=0;
outStream.writeByte(msgSmDefaultMsgID );
outStream.writeByte(msgSmLength );
outStream.write(msgText);
} catch(Exception e) {
//Log.error(" yilu modify delive error e="+e);
return false;
}
return true;
}
private byte [] convertStringToBytes(String srcBuff) {
byte ab;
String subBuff, SrcBuff;
SrcBuff = srcBuff;
if(SrcBuff == null || SrcBuff.length() <= 0)
return null;
try {
byte[] des = new byte[SrcBuff.length()/2];
int j=0;
for(int i=0; i<SrcBuff.length(); i+=2,j++) {
subBuff = srcBuff.substring(i, i+2);
ab = (byte)Integer.parseInt(subBuff, 16);
des[j] = ab;
}
return des;
} catch(Exception e) {
System.out.println("Convert string to bytes exception, " + e.getMessage());
return null;
}
}
private static String readString(DataInputStream inStream) {
int i = 0;
byte[] readByte = new byte[180];
try {
do {
readByte[i] = inStream.readByte();
i++;
} while(i<180 && readByte[i-1] != 0);
} catch(IOException e) {
SMSCFrame.RecvArea.append(" Read string exception from input stream .\n");
return READSTRINGERROR;
}
//if(i >= 180) return false;
String result = new String(readByte, 0, i-1);
if(result == null)
SMSCFrame.RecvArea.append(" Read string return null .\n");
return result;
}
private static String convertBytesToString(byte bstr[]) {
String desBuff = "";
String oa;
try {
for (int i=0; i<bstr.length; i++) {
oa = Integer.toHexString((int) bstr[i]);
if(oa.length()<=1)
oa = "0" + oa;
if(bstr[i]<0)
oa = oa.substring(oa.length()-2);
desBuff += oa;
}
return desBuff;
} catch(Exception e) {
return null;
}
}
private static boolean writeString(DataOutputStream outStream, String str) {
try {
outStream.writeBytes(str) ;
outStream.writeByte('\0');
} catch(Exception e) {
System.out.println("write string="+e.getMessage() ) ;
return false;
}
return true;
}
private static final String READSTRINGERROR = "readStringError";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -