Esempio n. 1
0
  public DPacketParser(
      int pktSingal,
      int pktDataRow,
      int pktDataColumn,
      int[] pktDataColumnType,
      int[] pktDataColumnLength,
      byte[] pktData) {
    this.pktSignal = pktSingal;
    this.pktDataRow = pktDataRow;
    this.pktDataColumn = pktDataColumn;
    this.pktDataColumnType = pktDataColumnType;
    this.pktDataColumnLength = pktDataColumnLength;
    //	System.out.println(ByteHexUtil.bytesToHexString(pktData));
    this.pktData = JzilbHelp.jzlib(pktData);

    this.pktLength = 20 + this.pktDataColumn * 4 * 2 + this.pktData.length + 6; // 报文长度

    // System.out.println(ByteHexUtil.bytesToHexString(this.pktData));
    // this.pktData = pktData;
    ByteArrayOutputStream bis = new ByteArrayOutputStream();

    try {
      bis.write(ByteHexUtil.intToByte(this.pktHead));
      bis.write(ByteHexUtil.intToByte(this.pktLength));
      bis.write(ByteHexUtil.intToByte(this.pktSignal));
      bis.write(ByteHexUtil.intToByte(this.pktDataRow));
      bis.write(ByteHexUtil.intToByte(this.pktDataColumn));
      for (int ii = 0; ii < this.pktDataColumn; ii++) {
        bis.write(ByteHexUtil.intToByte(pktDataColumnType[ii]));
      }
      for (int ii = 0; ii < this.pktDataColumn; ii++) {
        bis.write(ByteHexUtil.intToByte(pktDataColumnLength[ii]));
      }
      bis.write(this.pktData);
      this.pktCheck = packetCheck(bis.toByteArray());

      bis.write(this.pktCheck);
      bis.write(this.pktVersion);
      bis.write(ByteHexUtil.intToByte(this.pktEnd));

      this.pktBuffer = bis.toByteArray();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Esempio n. 2
0
  public DPacketParser(byte[] pktBuffer) {

    int head = 0; // 指向包头指针

    this.pktBuffer = pktBuffer;

    this.pktHead = ByteHexUtil.bytesToInt(Arrays.copyOfRange(pktBuffer, head, head += 4));
    this.pktLength = ByteHexUtil.bytesToInt(Arrays.copyOfRange(pktBuffer, head, head += 4));
    byte[] pkt = Arrays.copyOfRange(pktBuffer, 0, this.pktLength);

    this.pktSignal = ByteHexUtil.bytesToInt(Arrays.copyOfRange(pktBuffer, head, head += 4));
    this.pktDataRow = ByteHexUtil.bytesToInt(Arrays.copyOfRange(pktBuffer, head, head += 4));
    this.pktDataColumn = ByteHexUtil.bytesToInt(Arrays.copyOfRange(pktBuffer, head, head += 4));
    this.pktDataColumnType = new int[this.pktDataColumn];
    this.pktDataColumnLength = new int[this.pktDataColumn];
    for (int ii = 0; ii < this.pktDataColumn; ii++) {
      this.pktDataColumnType[ii] =
          ByteHexUtil.bytesToInt(Arrays.copyOfRange(pktBuffer, head, head += 4));
    }
    for (int ii = 0; ii < this.pktDataColumn; ii++) {
      this.pktDataColumnLength[ii] =
          ByteHexUtil.bytesToInt(Arrays.copyOfRange(pktBuffer, head, head += 4));
    }

    int datalen = this.pktLength - 4 * 5 - this.pktDataColumn * 4 * 2 - 6;
    byte[] dataorg = Arrays.copyOfRange(pkt, head, head += datalen);
    this.pktData = JzilbHelp.unjzlib(dataorg);
    this.pktCheck = pkt[head++];
    this.pktVersion = pkt[head++];

    this.pktEnd = ByteHexUtil.bytesToInt(Arrays.copyOfRange(pkt, head++, head += 4));

    this.dataTable =
        new DataTable(
            this.pktDataRow,
            this.pktDataColumn,
            this.pktDataColumnType,
            this.pktDataColumnLength,
            this.pktData);
  }