Esempio n. 1
0
  public static boolean unpack(MsgItem item, UnpackContext context) {
    int pos = context.pos, pos2;
    byte[] SOH = {1};

    // colCount
    pos2 = ByteArrayUtils.IndexOf(context.bytes, SOH, pos);
    String colCountStr =
        new String(ByteArrayUtils.SubArray(context.bytes, pos, pos2 - pos), context.spec.encoding);
    pos = pos2 + 1;
    int colCount = Integer.parseInt(colCountStr);

    // rowCount
    pos2 = ByteArrayUtils.IndexOf(context.bytes, SOH, pos);
    String rowCountStr =
        new String(ByteArrayUtils.SubArray(context.bytes, pos, pos2 - pos), context.spec.encoding);
    pos = pos2 + 1;
    int rowCount = Integer.parseInt(rowCountStr);

    // title
    String[] title = new String[colCount];
    for (int i = 0; i < colCount; i++) {
      pos2 = ByteArrayUtils.IndexOf(context.bytes, SOH, pos);
      title[i] =
          new String(
              ByteArrayUtils.SubArray(context.bytes, pos, pos2 - pos), context.spec.encoding);
      pos = pos2 + 1;
    }

    // body
    MsgDocument doc = (MsgDocument) item;
    MsgArray root = new MsgArray();
    doc.put("root", root);

    for (int i = 0; i < rowCount; i++) {
      MsgStruct stru = new MsgStruct();
      stru.setAttribute("name", "root");
      stru.setAttribute("isarray", true);

      for (int j = 0; j < colCount; j++) {
        pos2 = ByteArrayUtils.IndexOf(context.bytes, SOH, pos);
        String data =
            new String(
                ByteArrayUtils.SubArray(context.bytes, pos, pos2 - pos), context.spec.encoding);
        pos = pos2 + 1;

        MsgField field = new MsgField();
        field.setAttribute("name", title[j]);
        field.set(data);
        stru.put(title[j], field);
      }
      root.add(stru);
    }

    context.length = context.bytes.length - context.pos;
    return true;
  }
Esempio n. 2
0
  public static boolean bitmap(MsgItem item, UnpackContext context) {
    // 解析位图

    int bmLen = context.bytes[2] < 0 ? 16 : 8; // 用符号位判断是否存在扩展位图

    byte[] bitmap = new byte[bmLen];
    System.arraycopy(context.bytes, 2, bitmap, 0, bmLen);

    // 根据位图中的内容 砍掉模板中不应该存在的域 以使模板与报文相匹配
    for (int bmIndex = 0; bmIndex < bitmap.length; bmIndex++)
      for (int i = 0; i < 8; i++)
        if ((1 << (7 - i) & bitmap[bmIndex]) == 0)
          ((MsgStruct) context.template).remove("b" + String.valueOf(bmIndex * 8 + i + 1));

    // 设置位图长度
    context.length = bmLen;
    return true;
  }