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

📄 smgp.java~3~

📁 中国电信小灵通短信平台SP端程序
💻 JAVA~3~
📖 第 1 页 / 共 2 页
字号:



  /**取得5.3.1	exit的消息包
   * 5.3.11	exit
   **/
  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:
          this.parseDeliverPack(dataInStream);
          break;
        case SMGP.SMGP_DELIVER_RESP:
          break;

        case SMGP.SMGP_SUBMIT:
          break;
        case SMGP.SMGP_SUBMIT_RESP:
          this.parseSubmitRespPack(dataInStream);
          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.2	login_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.4	submit_resp
    * @param dataInputStream
    */
    private void parseSubmitRespPack( DataInputStream dataInputStream ) throws Exception
    {
      try{
        this.MsgID               = this.readMsgIdString(dataInputStream,10);
        this.Status              = dataInputStream.readInt();
      }catch(Exception e){
        System.out.println("[SMGP]SMGP.parseSubmitRespPack error " + e);
        throw e;
      }
    }



    /**
     * 解析deliver信息包
     * 5.3.5	deliver
     * @param dataInputStream
     */
     private void parseDeliverPack( DataInputStream dataInputStream ) throws Exception
     {
       try{

         this.MsgID        = this.readMsgIdString(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.readString(dataInputStream,3);
           //this.Report_ID  = this.readString(dataInputStream,10);
           String a = this.readString(dataInputStream,3);
           String b = this.readString(dataInputStream,4);
           BCDMsgID = this.readBCD3(dataInputStream);

//           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-13,this.MsgFormat);

           Debug.outInfo("\n[SMGPMaster]"+PublicFunction.getFormatTime()+
                         " 状态报告:("+this.SrcTermID+")"+this.MsgContent+" TO:"+DestTermID
                         +" MsgID:"+BCDMsgID +" SequenceID:"+this.SequenceID);
         }
         else{
           //非状态报告,当字节数大于128时候
           this.MsgContent = readDeliverString(dataInputStream,this.MsgLength,this.MsgFormat);
           Debug.outInfo("\n[SMGPMaster]"+PublicFunction.getFormatTime()+
                         " 接收数据\nSrc_Numer:"+this.SrcTermID+"\nMsg_Content:"+this.MsgContent+"\nDest_Number:"+DestTermID
                         +"\nMsgID:"+BCDMsgID +"\nSequenceID:"+this.SequenceID);
         }

       }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) ;
        for (i=1;i<=StrLen-str.length ();i++)
        {
          dataOutStream.writeByte('\0');
        }
      }else{
        for (i=1;i<=StrLen;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<StrLen;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<StrLen-theLength;i++){
     dataOutStream.writeByte('\0');
   }

 }


 /**
  * 按长度,编码格式从字节流中读取指定长度的字符
  * @param inStream :字节流
  * @param StrLen   :长度
  * @param fmt      :编码格式
  * @return
  */
 private 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 <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 "erro";
      }
        String result = new String(readByte,0,i);
        return result.trim ();
   }



   private String readMsgIdString(DataInputStream inStream,int StrLen){
       byte[] readByte = new byte[1000];
       int i = 0;
       try{
           while(true){
              readByte[i] = inStream.readByte() ;
              i++;
             if (i>=1000)
                 return "";
             if(i > StrLen-1)
                 break;
           }
       }    catch(IOException e){
           return "Error";
      }

      int i1 = (int)readByte[7];
      int i2 = (int)readByte[8];
      int i3 = (int)readByte[9];
      BCDMsgID = (0 << 24) + (i1 << 16) + (i2 << 8) + (i3 << 0);
      String result = new String(readByte,0,i);
        return result.trim ();
   }

   /** 将Unicode转换 **/
   protected static String HibyteString(byte ascii[], int hibyte) {

       int offset = 0;
       int count  = ascii.length;
       char value[] = new char[count];

       if (hibyte == 0) {
           for (int i = count ; i-- > 0 ;) {
               value[i] = (char) (ascii[i + offset] & 0xff);
           }
       } else {
           hibyte <<= 8;
           for (int i = count ; i-- > 0 ;) {
               value[i] = (char) (hibyte | (ascii[i + offset] & 0xff));
           }
       }

       try{
         //return new String(new String(value).getBytes());
         return new String(new String(value).getBytes("iso-8859-1"));
       }
       catch(Exception ex){
         return "";
       }
   }



   protected static String HibyteString(String str, int hibyte) {

       byte ascii[] = str.getBytes();

       int offset = 0;
       int count  = ascii.length;
       char value[] = new char[count];

       if (hibyte == 0) {
           for (int i = count ; i-- > 0 ;) {
               value[i] = (char) (ascii[i + offset] & 0xff);
           }
       } else {
           hibyte <<= 8;
           for (int i = count ; i-- > 0 ;) {
               value[i] = (char) (hibyte | (ascii[i + offset] & 0xff));
           }
       }

       try{
         return new String(new String(value).getBytes("ISO-8859-1"));
       }
       catch(Exception ex){
         return "";
       }

   }


   public final int readBCD3(DataInputStream in) throws IOException {
       int ch1 = in.read();
       int ch2 = in.read();
       int ch3 = in.read();
       if ((ch1 | ch2 | ch3 ) < 0)
            throw new EOFException();
       return ((0 << 24) + (ch1 << 16) + (ch2 << 8) + (ch3 << 0));
   }

}

⌨️ 快捷键说明

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