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

📄 cmppmsg.java

📁 采用JAVA开发
💻 JAVA
字号:
package com.gctech.cmpp3.msg;

import java.io.*;
import com.gctech.util.Tools;

/**
 * <p>Title: CNGP API</p>
 * <p>Description: 固网短信SP API</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: GCTECH</p>
 * @author 王红宝
 * @version $Id: CmppMsg.java,v 1.2 2004/07/16 08:32:43 wanghb Exp $
 */

public abstract class CmppMsg{
  protected MsgHead head;
  /**
   * 设置消息ID为msg的.
   * */
  public void setSequence(CmppMsg msg){
    this.getHead().setSequenceId(msg.getHead().getSequenceId());
  }
  public CmppMsg() {
    this.head = new MsgHead();
  }
  public MsgHead getHead() {
    return head;
  }
  public byte[] toByteArray(){
  	System.out.println("head.getTotalLength():"+head.getTotalLength());
    byte[] data = new byte[head.getTotalLength()];
    Tools.int2byte(head.getTotalLength(), data, 0);
    Tools.int2byte(head.getCommandId(), data, 4);
    Tools.int2byte(head.getSequenceId(), data, 8);
    toByteArray(data);
    return data;
  }
  /**
   * offset 表示有效数据开始处。
   * */
  public void fromByteArray(byte[] data, int offset){
    //head.setSequenceId(Tools.byte2int(data, 4+offset));
    head.setSequenceId(Tools.byte2int(data, offset));
  }
  /**
   * 缺省,带包头的。
   * */
  public void fromByteArray(byte[] data){
    this.fromByteArray(data, 4);
  }

  //子类需要重构
  abstract protected void toByteArray(byte[] data);

  public void setHead(MsgHead head) {
    this.head = head;
  }
  public String toString() {
    StringBuffer sb = new StringBuffer();
    try {
      sb.append("Total_Length:").append(head.getTotalLength())
          .append(",Command_Id:").append(CommandID.valueToName(head.getCommandId()))
          .append(",Sequence_Id:").append(head.getSequenceId());
    }
    catch (Throwable ex) {
      ex.printStackTrace();
    }
    return sb.toString();
  }

  //add by liya begin
  protected byte bodybytes[];
  protected int TotalLength;

  public CmppMsg(CmppMsg cmppmsg) {
    TotalLength = 0;
    head = cmppmsg.head;
    bodybytes = cmppmsg.bodybytes;
    TotalLength = cmppmsg.TotalLength;
  }

  public CmppMsg(MsgHead msghead) {
    head = msghead;
  }

  //i除了头外的数据长度,j CommandID
  public CmppMsg(MsgHead msghead, int i, int j)
  {
    TotalLength = 12 + i;
    head = new MsgHead();
    head.setTotalLength(TotalLength);
    head.setCommandId(j);
    head.setSequenceId(msghead.getSequenceId());
    bodybytes = new byte[i];
  }

  protected static int Bytes4ToInt(byte abyte0[])
  {
    return (0xff & abyte0[0]) << 24 | (0xff & abyte0[1]) << 16 | (0xff & abyte0[2]) << 8 | 0xff & abyte0[3];
  }

  protected static byte[] IntToBytes4(int i)
  {
    byte abyte0[] = new byte[4];
    abyte0[3] = (byte)(0xff & i);
    abyte0[2] = (byte)((0xff00 & i) >> 8);
    abyte0[1] = (byte)((0xff0000 & i) >> 16);
    abyte0[0] = (byte)((0xff000000 & i) >> 24);
    return abyte0;
  }


  protected static void BytesCopy(byte abyte0[], byte abyte1[], int i, int j, int k)
  {
    int i1 = 0;
    for(int l = i; l <= j; l++)
    {
      abyte1[k + i1] = abyte0[l];
      i1++;
    }
  }

  public CmppMsg read(InputStream inputstream)
      throws IOException, Exception
  {
    head.read(inputstream);
    readdataintobody(inputstream);
    switch(head.getCommandId())
    {
      case CommandID.CMPP_SUBMIT:
        return new SubmitRequest(this);

      case CommandID.CMPP_ACTIVE_TEST:
        return new ActiveTestRequest(this);

      case CommandID.CMPP_ACTIVE_TEST_RESP:
        return new ActiveTestResponse(this);

      case CommandID.CMPP_DELIVER_RESP:
        return new DeliverResponse(this);
    }
    return null;
  }

  private void readdataintobody(InputStream inputstream)
      throws IOException, Exception
  {
    TotalLength = head.getTotalLength();
    bodybytes = new byte[TotalLength - 12];
    inputstream.read(bodybytes);
  }

  public int getCommandId()
  {
    return head.getCommandId();
  }

  public int write(OutputStream outputstream){
    try
    {
      byte abyte0[] = new byte[TotalLength];
      BytesCopy(head.Head, abyte0, 0, 11, 0);
      BytesCopy(bodybytes, abyte0, 0, TotalLength - 11 - 1, 12);
      outputstream.write(abyte0);
      return 0;
    }
    catch(Exception exception)
    {
      System.out.println(exception.toString());
    }
    return -1;
  }
  //add by liya end
  public void printHead(){
    System.out.println("head.getSequenceId()"+head.getSequenceId());
    System.out.println("head.getCommandId()"+head.getCommandId());
    System.out.println("head.getTotalLength()"+head.getTotalLength());
  }
}

⌨️ 快捷键说明

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