return this.dataPack; }catch(Exception e){ e.printStackTrace(); System.out.println("[SMGP] makeActiveMsgPack.pack() error : "+e.getMessage()); return null; } } /**取得5.3.1active_test的消息包 * 5.3.7active_test **/ private byte[] makeActiveMsgPack(){ ByteArrayOutputStream byteArrayOutStream = new ByteArrayOutputStream(); DataOutputStream dataOutStream = new DataOutputStream(byteArrayOutStream); try{ //MT消息长度 this.PacketLength = 12; //打包MT消息报头 packHead(dataOutStream); //返回消息的字节流 dataPack = byteArrayOutStream.toByteArray(); return this.dataPack; }catch(Exception e){ e.printStackTrace(); System.out.println("[SMGP] makeActiveMsgPack.pack() error : "+e.getMessage()); return null; } } /**取得5.3.1exit的消息包 * 5.3.11exit **/ private byte[] makeExitMsgPack(){ ByteArrayOutputStream byteArrayOutStream = new ByteArrayOutputStream(); DataOutputStream dataOutStream = new DataOutputStream(byteArrayOutStream); try{ //MT消息长度 this.PacketLength = 12; //打包MT消息报头 packHead(dataOutStream); //返回消息的字节流 dataPack = byteArrayOutStream.toByteArray(); return this.dataPack; }catch(Exception e){ e.printStackTrace(); System.out.println("[SMGP] makeExitMsgPack.pack() error : "+e.getMessage()); return null; } } /** * 解吸接受到的数据包,转换成SMGP MSG * @param recvPack * @return */ public void parsePack(byte[] recvPack) throws Exception{ ByteArrayInputStream byteStream = new ByteArrayInputStream(recvPack); DataInputStream dataInStream = new DataInputStream(byteStream); //读取消息头 try { this.RequestID = dataInStream.readInt(); //消息类型 this.SequenceID = dataInStream.readInt(); //消息流水号(可以用来完成消息的确认) } catch(IOException e) { System.out.println("[SMGP] SMGP.parseHead() error : "+e); throw e; } //读取消息体 try{ switch(RequestID){ case SMGP.SMGP_LOGIN: break; case SMGP.SMGP_LOGIN_RESP: this.parseLoginRespPack(dataInStream); break; case SMGP.SMGP_DELIVER: break; case SMGP.SMGP_DELIVER_RESP: break; case SMGP.SMGP_SUBMIT: break; case SMGP.SMGP_SUBMIT_RESP: break; case SMGP.SMGP_ACTIVE_TEST: break; case SMGP.SMGP_ACTIVE_TEST_RESP: break; case SMGP.SMGP_QUERY: break; case SMGP.SMGP_QUERY_RESP: break; } } catch(Exception e){ throw e; } } /** * 解析login_resp信息包 * 5.3.2login_resp * @param dataInputStream */ private void parseLoginRespPack( DataInputStream dataInputStream ) throws Exception { try{ this.Status = dataInputStream.readInt(); this.AuthenticatorServer = this.readString(dataInputStream,16); this.Version = dataInputStream.readByte(); }catch(Exception e){ System.out.println("[SMGP]SMGP.parseConnectRespPack error " + e); throw e; } } /** * 解析submit_resp信息包 * 5.3.4submit_resp * @param dataInputStream */ private void parseSubmitRespPack( DataInputStream dataInputStream ) throws Exception { try{ this.MsgID = this.readString(dataInputStream,10); this.Status = dataInputStream.readInt(); }catch(Exception e){ System.out.println("[SMGP]SMGP.parseSubmitRespPack error " + e); throw e; } } /** * 解析deliver信息包 * 5.3.5deliver * @param dataInputStream */ private void parseDeliverPack( DataInputStream dataInputStream ) throws Exception { try{ this.MsgID = this.readString(dataInputStream,10); this.IsReport = dataInputStream.readByte(); this.MsgFormat = dataInputStream.readByte(); this.RecvTime = this.readString(dataInputStream,14); this.SrcTermID = this.readString(dataInputStream,21); this.DestTermID = this.readString(dataInputStream,21); this.MsgLength = dataInputStream.readByte(); //当为状态报告时候 if(IsReport == 1){// this.Report_ID = this.readString(dataInputStream,10);// this.Report_Submit_date = this.readString(dataInputStream,14);// this.Report_Done_date = this.readString(dataInputStream,14);// this.Report_Stat = this.readString(dataInputStream,7);// this.Report_Error = this.readString(dataInputStream,3); this.MsgContent = readDeliverString(dataInputStream,this.MsgLength,this.MsgFormat); } else{ //非状态报告,当字节数大于128时候 this.MsgContent = readDeliverString(dataInputStream,this.MsgLength,this.MsgFormat); } }catch(Exception e){ System.out.println("[SMGP]SMGP.parseDeliverPack error " + e); throw e; } } /** 生成序列号 **/ private static int fmo_number = 1; private synchronized static int getSequenceID() { int temp_SeqNo = fmo_number++; if (fmo_number >= 100000000) fmo_number = 1; return temp_SeqNo; } /** * 向数据输出流写入指定长度字符串,不足右补“\0” * @param dataOutStream 由调用者传送来的数据输出流 * @param str 写入的字符 * @param StrLen 指定长度 * @throws Exception */ private void writeString_old(DataOutputStream dataOutStream,String str,int StrLen) throws Exception { int i; try{ if(str!=null) { dataOutStream.writeBytes(str) ; System.out.println("str len = "+str.length()); for (i=1;i { dataOutStream.writeByte('\0'); } }else{ for (i=1;i { dataOutStream.writeByte('\0'); } } }catch(IOException e){ throw e; } } /** * 向数据输出流写入指定长度字符串,不足右补“\0” * @param dataOutStream 由调用者传送来的数据输出流 * @param str 写入的字符 * @param StrLen 指定长度 * @throws Exce **/ private void writeString(DataOutputStream dataOutStream,String str,int StrLen) throws Exception { if(str==null || str.length()==0){ for(int i=0;i dataOutStream.writeByte('\0'); } return; }// byte[] bytes = str.getBytes();// int theLength = Math.min(bytes.length,StrLen);// dataOutStream.write(bytes,0,theLength); int theLength = str.length() < StrLen ? str.length() : StrLen; dataOutStream.writeBytes(str.substring(0,theLength)); //不足部分补'\0' for(int i=0;i dataOutStream.writeByte('\0'); } } /** * 按长度,编码格式从字节流中读取指定长度的字符 * @param inStream :字节流 * @param StrLen :长度 * @param fmt :编码格式 * @return */ private static String readDeliverString(DataInputStream inStream, int StrLen, int fmt) { String result = ""; byte[] readByte = new byte[1000]; int i = 0; //判断字符串是否长于128位 if (StrLen < 0) { StrLen = 256 + StrLen; } try { while (true) { readByte[i] = inStream.readByte(); i++; if (i >= 1000) return ""; if (i > StrLen - 1) break; } } catch (IOException e) { return "readStringError->IOException"; } //按编码格式进行转码 try { if (fmt == 8) { result = new String(readByte, "UnicodeBigUnmarked"); } if (fmt == 0) { result = new String(readByte, 0, i); } if (fmt == 15) { result = new String(readByte, "gb2312"); } } catch (Exception e) { return "readStringError->FormatException"; } return result.trim(); } /** * 从流中读取一个字符串,返回的String 不包含'\0' */ private static String readString(DataInputStream inStream,int StrLen) { byte[] readByte = new byte[1000]; int i = 0; //当消息长度为大于(128时候) if(StrLen StrLen = 256 + StrLen; try{ while(true){ readByte[i] = inStream.readByte() ; i++; if (i>=1000) return ""; if(i > StrLen-1) break; } } catch(IOException e){ return "erro"; } String result = new String(readByte,0,i); return result.trim (); }}
中国电信 smgp java_smgp.java~1~ 源代码在线查看 - 中国电信小灵通短信平台SP端程序 资源下载 虫虫电子下载站...
最新推荐文章于 2024-07-26 03:16:02 发布