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

📄 cb8583pack.java

📁 8583 pack and unpack 8583 pro
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        ruleList = SysFunction.splitString(inParam,"|");
       // System.out.println("InParam:"+inParam);
        for(int i=0 ; i<ruleList.length ; i++){
           // System.out.println("ruleList:"+i+"="+ruleList[i]);
            if(ruleList[i] == null || ruleList[i].trim().equals(""))
              continue;
            fieldList = SysFunction.splitString(ruleList[i],":");
            tmpArrayByte = null;
            if(fieldList != null && fieldList.length >= 5 && (fieldList[3].trim().equals("888") || fieldList[3].trim().equals("889"))){
              boolean  hasField = false;
              for(int j=0; j<requestPack.getSize(); j++){
                 DataField tempChkField = (DataField)requestPack.getDomain(j);
                 if(tempChkField.fieldRule.startsWith(new StringBuffer(fieldList[0].trim()).append(":").append(fieldList[1].trim()).toString())){
                   hasField = true;
                   break;
                 }
              }
              if(hasField)
                continue;
             DataField tempAddField = new TradeField();
              tempAddField.fieldRule = new StringBuffer(fieldList[0].trim()).append(":").append(fieldList[1].trim()).toString();
              tempAddField.value = new StringBuffer(tempAddField.fieldRule).append("|").append(fieldList[4].trim()).toString().getBytes();
              requestPack.add(tempAddField);
              System.out.println("Adding field-------------"+tempAddField.fieldRule);

              if(!fieldDataList.containsKey(fieldList[0].trim())){
                 if(fieldList[2].trim().equals("0")){
                   fieldDataList.put(fieldList[0].trim(),fieldList[4].trim().getBytes());
                 }
                 else{
                   int tmpFieldStartPos = Integer.parseInt(fieldList[1].trim());
                   int tmpFieldLen = Integer.parseInt(fieldList[2].trim());
                   tmpArrayByte = new byte[tmpFieldStartPos + tmpFieldLen];
                   SysFunction.clearByteArray(tmpArrayByte,(byte)0x20);
                   System.arraycopy(fieldList[4].trim().getBytes(), 0, tmpArrayByte,tmpFieldStartPos, tmpFieldLen);
                   fieldDataList.put(fieldList[0].trim(),tmpArrayByte);
                 }
                 packetBitmap.openBit(Integer.parseInt(fieldList[0].trim()));
               }
               else{
                 if(!fieldList[2].trim().equals("0")){
                   tmpArrayByte = (byte[]) fieldDataList.get(fieldList[0].trim());
                   int tmpFieldStartPos = Integer.parseInt(fieldList[1].trim());
                   int tmpFieldLen = Integer.parseInt(fieldList[2].trim());
                   if (tmpFieldStartPos + tmpFieldLen > tmpArrayByte.length)
                     tmpArrayByte = SysFunction.changeMemLen(tmpArrayByte,
                         tmpFieldStartPos + tmpFieldLen);
                   System.arraycopy(fieldList[4].trim().getBytes(), 0, tmpArrayByte,
                                    tmpFieldStartPos, tmpFieldLen);
                   fieldDataList.put(fieldList[0].trim(),
                                     tmpArrayByte);
                 }
               }
            }
        }

        tmpArrayByte = null;
        String traceNo = SysFunction.getAddTraceNo(TRACENO_FILE);
        requestPack.traceNo = traceNo;
        retHeader = packHeader(tradeCode, cardNo, traceNo);
        headerBuf = (byte[])retHeader.get(0);
        fieldDataList.put("3", tradeParam.tradeToCode.getBytes());
        packetBitmap.openBit(3);
        fieldDataList.put("7", SysFunction.getMMDDHHMISS().getBytes());
        packetBitmap.openBit(7);
        fieldDataList.put("11", traceNo.getBytes());
        packetBitmap.openBit(11);
        fieldDataList.put("12", SysFunction.getHHMISS().getBytes());
        packetBitmap.openBit(12);
        fieldDataList.put("13", SysFunction.getMMDD().getBytes());
        packetBitmap.openBit(13);
        CB8583Config tradeConfig = CB8583Config.getInstance();
        fieldDataList.put("25", ((String[])retHeader.get(1))[8].getBytes());
        packetBitmap.openBit(25);
        fieldDataList.put("32", tradeConfig.acqInstID.getBytes());
        packetBitmap.openBit(32);
        fieldDataList.put("41", tradeConfig.termID.getBytes());
        packetBitmap.openBit(41);
        fieldDataList.put("42", tradeConfig.identifyCode.getBytes());
        packetBitmap.openBit(42);
        fieldDataList.put("1", packetBitmap.getBitmap());
        packetBitmap.openBit(1);
      /*
        System.out.println("Get bitmap:"+SysFunction.convertByteArrayToHexStr(packetBitmap.getBitmap()));
        System.out.println("102Field value:"+new String((byte[])fieldDataList.get(String.valueOf(102))));
        if(packetBitmap.isBitSet(102))
          System.out.println("has 102Field------------");
        else
          System.out.println("has not 102Field------------");
        debug 域值

       */
      for (int i = 1; i <= 128; i++) {
          if (packetBitmap.isBitSet(i)) {
            System.out.println("Pack buf no:"+i);
            System.out.println(" data:"+new String((byte[])fieldDataList.get(String.valueOf(i))));
            if (i == 2)
              requestPack.accountNo = new String((byte[])fieldDataList.get(String.valueOf(i)));
            else if (i == 4)
              requestPack.moneyNum = new String((byte[])fieldDataList.get(String.valueOf(i)));

            // System.out.println("process code three ="+ new String((byte[])fieldDataList.get(String.valueOf(3))));
            if(i==3 && (new String((byte[])fieldDataList.get(String.valueOf(3)))).trim().equals("936011")) {
                 fieldByteArray =packField(3,"936000".getBytes());
               }else{
                 fieldByteArray = packField(i,(byte[]) fieldDataList.get(String.valueOf(i)));
               }
            System.arraycopy(fieldByteArray, 0, bodyBuf, bufferCurPos,
                             fieldByteArray.length);
            bufferCurPos += fieldByteArray.length;
          }
        }
        byte[] packetLen = SysFunction.convertIntToStr(80 + 4 + 4 + bufferCurPos, 4).
            getBytes();
        byte[] bodyLen = SysFunction.convertIntToStr(bufferCurPos+4, 4).getBytes();
        destArrayBuf = new byte[4 + 80 + 4 + 4 + bufferCurPos];
        System.arraycopy(packetLen, 0, destArrayBuf, 0, 4);
        System.arraycopy(headerBuf, 0, destArrayBuf, 4, 80);
        System.arraycopy(bodyLen, 0, destArrayBuf, 84, 4);
        System.arraycopy(((String[])retHeader.get(1))[1].getBytes(), 0, destArrayBuf, 88, 4);
        System.arraycopy(bodyBuf, 0, destArrayBuf, 92, bufferCurPos);
        packetLen = null;
        bodyLen   = null;
      }catch(Exception ex){
        ex.printStackTrace();
      }
      finally{
        if(retHeader != null){
           retHeader.clear();
           retHeader = null;
        }
        if(fieldDataList != null){
           fieldDataList.clear();
           fieldDataList = null;
        }
        s =null;
        srcvalue = null;
        desvalue = null;
        fieldByteArray = null;
        ruleList = null;
        fieldList = null;
        headerBuf = null;
      }
      return destArrayBuf;
  }


  public static TradePacket unPack8583(byte[] recvBuf)throws PacketException{
      if(recvBuf == null || recvBuf.length < 84)
          throw new PacketException("解包出错,返回包为空或长度不够");
      byte[] packetHeader = new byte[80];
      byte[] bodyLen = new byte[4];
      byte[] packetBody = null;
      byte[] messageId = new byte[4];
      byte[] bitmapField = new byte[16];
      CB8583PacketHeader header = new CB8583PacketHeader();
      CB8583Bitmap bitmap = new CB8583Bitmap();
      TradePacket retDataPacket = null;//放返回的8583数据
      String[] outParamList = null;//放返回8583包中各域的解包规则
      String[] outRecordParam = null;//放8583包明细的解包规则
      String[] retCodeParam = null;//放返回码8583包明细的解规则
      byte[] field48Value = null;
      byte[] field47Value = null;
      try{
        int cusPos = 0;
        System.arraycopy(recvBuf, cusPos, packetHeader, 0, 80);
        cusPos += 80;
        System.arraycopy(recvBuf, cusPos, bodyLen, 0, 4);
        cusPos += 4;
        int iBodyLen = Integer.parseInt(new String(bodyLen));
        System.out.println("Unpack body len:"+iBodyLen);
        if (iBodyLen + 4 + 80 > recvBuf.length)
          throw new PacketException("解包出错,返回包为空或长度不够");
        System.arraycopy(recvBuf, cusPos, messageId, 0, 4);
        cusPos += 4;
        byte firstBitmap = recvBuf[cusPos];
        int bitmapSize = 0;
        if ( (firstBitmap & 0x80) == 0x80)
          bitmapSize = 16;
        else
          bitmapSize = 8;
        System.arraycopy(recvBuf, cusPos, bitmapField, 0, bitmapSize);
        cusPos += bitmapSize;
        bitmap.setBitmap(bitmapField);
        header.setPacketHeader(packetHeader);
        System.out.println("Unpack bitmap:"+SysFunction.convertByteArrayToHexStr(bitmapField));

        retDataPacket = new TradePacket();
        DataField retDataField = null;
        //取其中某一交易的规则
        TradeParam tradeParam = getTradeConfig(new String(header.getPrcCode()));
        //取其中输出项目的规则
        outParamList = SysFunction.splitString((String) tradeParam.paramList.get("out"), "#");
        for(int m=0;m<outParamList.length;m++){
            System.out.println("outParamList value["+m+"]="+String.valueOf(outParamList[m]));
        }
        if (outParamList == null || outParamList.length < 1)
          throw new PacketException("解包出错,交易没有配置");
        StringBuffer outTitleParam = new StringBuffer("|");
        outTitleParam.append(outParamList[0]);
        if (outParamList.length >= 2)
          outRecordParam = SysFunction.splitString(outParamList[1], "|");
        retCodeParam = SysFunction.splitString((String) tradeParam.paramList.get("retcode"), "|");
        if(retCodeParam == null || retCodeParam.length < 3)
          throw new PacketException("解包出错,返回码没有配置");

        String[] tmpRuleValue = null;
        String   tmpDomainNo  = null;
        byte[]   tmpValue     = null;
        for (int i = 2; i <= bitmapSize * 8; i++) {
          if (bitmap.isBitSet(i)) {
            System.out.println("Unpack no:"+i);
            CB8583UnpackField unPacketField = unpackField(recvBuf, cusPos, i);
            System.out.println("Unpack value:"+new String(unPacketField.getRetByteArray()));
            cusPos += unPacketField.getPosIncValue();
            tmpRuleValue = null;
            tmpDomainNo = new StringBuffer("|").append(i).append(":").toString();
        //  System.out.println("tmpDomainNo="+tmpDomainNo);
            if (outTitleParam.indexOf(tmpDomainNo) >= 0){
            //  System.out.println("outTitleParam value="+outTitleParam.toString());
              int startPos = outTitleParam.indexOf(tmpDomainNo)+1;
              int endPos   = outTitleParam.indexOf("|",startPos);
              if(endPos >= startPos){
             //   System.out.println("Title Param"+outTitleParam.substring(startPos,endPos)+" tmpNo:"+tmpDomainNo);
                tmpRuleValue = SysFunction.splitString(outTitleParam.substring(startPos,endPos),":");
              }else{
             //   System.out.println("Title Param"+outTitleParam.substring(startPos)+" tmpNo:"+tmpDomainNo);
                tmpRuleValue = SysFunction.splitString(outTitleParam.substring(startPos),":");
              }
              retDataField = new TradeField();//域值
          //  retDataField.fieldRule = new StringBuffer(i).append(":").append(tmpRuleValue[1]).toString();
              retDataField.fieldRule = new StringBuffer("").append(i).append(":").append(tmpRuleValue[1]).append("|").toString();
              retDataField.fieldSign = SignConst.STRING_DOMAIN;
              tmpValue = null;
              System.out.println("data.fieldRule="+retDataField.fieldRule);
              System.out.println("  Unpack src:"+new String(unPacketField.getRetByteArray())+" Start Pos:"+tmpRuleValue[1]+" Len:"+tmpRuleValue[2]);
              if(tmpRuleValue[2].trim().equals("0")){
                 tmpValue = unPacketField.getRetByteArray();
                 System.out.println("tmpvalue:="+new String(tmpValue));
              }else{
                 tmpValue = new byte[Integer.parseInt(tmpRuleValue[2].trim())];
                 System.arraycopy(unPacketField.getRetByteArray(),Integer.parseInt(tmpRuleValue[1].trim()),tmpValue,0,Integer.parseInt(tmpRuleValue[2].trim()));
              }
              if(tmpRuleValue[3].trim().equals("0")){
                  retDataField.value = tmpValue;
              }
              else{
                if(tmpRuleValue.length < 5)
                  retDataField.value = convertField(tmpValue,Integer.parseInt(tmpRuleValue[3].trim()),Integer.parseInt(tmpRuleValue[2].trim()),null,null);
                else
                  retDataField.value = convertField(tmpValue,Integer.parseInt(tmpRuleValue[3].trim()),Integer.parseInt(tmpRuleValue[2].trim()),tmpRuleValue[4].trim().getBytes(),tmpRuleValue[4].trim());
              }
             System.out.println("retDataField value="+new String(retDataField.value));
              retDataPacket.add(retDataField);
            }
            if(i == 48){
              field48Value = unPacketField.getRetByteArray();
            }
            else if(i == 47) {
              field47Value = unPacketField.getRetByteArray();
            }
            else if(i == 11){
              retDataPacket.traceNo = new String(unPacketField.getRetByteArray());
            }
            if(retCodeParam[0].trim().toUpperCase().startsWith(String.valueOf(i)+":")){
              retDataPacket.errCode = new String(unPacketField.getRetByteArray());
              if (retDataPacket.errCode==null){
               retDataPacket.errCode="0B";
              }
              if(retCodeParam[1].indexOf(retDataPacket.errCode) >= 0){
                retDataPacket.errCode = "00";
                retDataPacket.errMsg = "交易成功";
              }
              else{

⌨️ 快捷键说明

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