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

📄 cb8583pack.java

📁 8583 pack and unpack 8583 pro
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package com.szzt.trade.zsbank;

import com.szzt.common.SysFunction;
import com.szzt.common.datapacket.Domain;
import com.szzt.common.datapacket.DataPacket;
import com.szzt.common.datapacket.DataField;
import com.szzt.common.datapacket.SignConst;
import com.szzt.trade.tradestruct.*;
import com.szzt.trade.util.TradeDataConvert;
import com.szzt.trade.util.TradeDataSelectConvert;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import com.szzt.common.datapacket.PacketException;


public class CB8583Pack {
  private static final String TRACENO_FILE="boc_traceno.txt";

  //private static byte[] pSrvCdCod = null;

  public CB8583Pack() {
  }

  public static byte[] packField(int fieldNo,byte[] fieldValue) throws PacketException{
    if (fieldValue==null)
      throw new PacketException("打包出错:字段为空");
    Integer fieldKey = new Integer(fieldNo);
    CB8583ConvertParam convertField=(CB8583ConvertParam)CB8583ConvertConst.convertMapTable.get(fieldKey);
    if (convertField==null)
      throw new PacketException("打包出错:字段没有配置转换");
    int packMethod=convertField.getPackMethod();
    int packMaxSize=convertField.getMaxSize();

    byte[] destByteArray=null;
    switch (packMethod) {
      case CB8583ConvertConst.PACK_STR_TO_ASC:
        destByteArray = CB8583ConvertMethod.pack_Str_To_Asc(fieldValue, packMaxSize);
        break;
      case CB8583ConvertConst.PACK_STR_TO_ASC0:
        destByteArray = CB8583ConvertMethod.pack_Str_To_AscZero(fieldValue, packMaxSize);
        break;
      case CB8583ConvertConst.PACK_BIT_TO_BIT:
        destByteArray = CB8583ConvertMethod.pack_Bit_To_Bit(fieldValue, packMaxSize);
        break;
      case CB8583ConvertConst.PACK_STR_TO_BCD:
        destByteArray = CB8583ConvertMethod.pack_Str_To_BCD(fieldValue, packMaxSize);
        break;
      case CB8583ConvertConst.PACK_STR_TO_AV2:
        destByteArray = CB8583ConvertMethod.pack_Str_To_AV2(fieldValue, packMaxSize);
        break;
      case CB8583ConvertConst.PACK_STR_TO_AV3:
        destByteArray = CB8583ConvertMethod.pack_Str_To_AV3(fieldValue, packMaxSize);
        break;
      case CB8583ConvertConst.PACK_STR_TO_BV2:
        destByteArray = CB8583ConvertMethod.pack_Str_To_BV2(fieldValue, packMaxSize);
        break;
      case CB8583ConvertConst.PACK_STR_TO_BV3:
        destByteArray = CB8583ConvertMethod.pack_Str_To_BV3(fieldValue, packMaxSize);
        break;
      case CB8583ConvertConst.PACK_STR_TO_XBCD:
        destByteArray = CB8583ConvertMethod.pack_Str_To_XBCD(fieldValue, packMaxSize);
        break;
      default:
        throw new PacketException("打包出错,没有打包方法");
    }
    if(destByteArray==null)
      throw new PacketException("打包出错");
    if (destByteArray.length<=0)
      throw new PacketException("打包出错");
    return destByteArray;
  }



  public static CB8583UnpackField unpackField(byte[] srcByteArray,int startPos,int fieldKey)
    throws PacketException
  {
    if (srcByteArray==null || startPos<0)
      throw new PacketException("解包出错:参数错");
    if (srcByteArray.length<=0)
      throw new PacketException("解包出错:参数错");
    CB8583UnpackField unpackResult=null;
    CB8583ConvertParam convertField=(CB8583ConvertParam)CB8583ConvertConst.convertMapTable.get(new Integer(fieldKey));
    if (convertField==null)
      throw new PacketException("解包出错:字段没有配置转换");
    int unPackMethod=convertField.getUnPackMethod();
    int unPackMaxSize=convertField.getMaxSize();

    switch(unPackMethod) {
      case CB8583ConvertConst.UNPACK_ASC_TO_STR:
        unpackResult=CB8583ConvertMethod.unpack_Asc_To_Str(srcByteArray,startPos,unPackMaxSize);
        break;
      case CB8583ConvertConst.UNPACK_ASC0_TO_STR:
        unpackResult=CB8583ConvertMethod.unpack_Asc_To_Str(srcByteArray,startPos,unPackMaxSize);
        break;
      case CB8583ConvertConst.UNPACK_BIT_TO_BIT:
        unpackResult=CB8583ConvertMethod.unpack_Bit_To_Bit(srcByteArray,startPos,unPackMaxSize);
        break;
      case CB8583ConvertConst.UNPACK_BCD_TO_STR:
        unpackResult=CB8583ConvertMethod.unpack_BCD_To_Str(srcByteArray,startPos,unPackMaxSize);
        break;
      case CB8583ConvertConst.UNPACK_AV2_TO_STR:
        unpackResult=CB8583ConvertMethod.unpack_AV2_To_Str(srcByteArray,startPos,unPackMaxSize);
        break;
      case CB8583ConvertConst.UNPACK_AV3_TO_STR:
        unpackResult=CB8583ConvertMethod.unpack_AV3_To_Str(srcByteArray,startPos,unPackMaxSize);
        break;
      case CB8583ConvertConst.UNPACK_BV2_TO_STR:
        unpackResult=CB8583ConvertMethod.unpack_BV2_To_Str(srcByteArray,startPos,unPackMaxSize);
        break;
      case CB8583ConvertConst.UNPACK_BV3_TO_STR:
        unpackResult=CB8583ConvertMethod.unpack_BV3_To_Str(srcByteArray,startPos,unPackMaxSize);
        break;
      case CB8583ConvertConst.UNPACK_XBCD_TO_STR:
        unpackResult=CB8583ConvertMethod.unpack_XBCD_To_Str(srcByteArray,startPos,unPackMaxSize);
        break;
      default:
        throw new PacketException("解包出错:没有转换方法");
    }
    if (unpackResult==null)
      throw new PacketException("解包出错");
    if (unpackResult.getRetByteArray()==null)
      throw new PacketException("解包出错");
    if (unpackResult.getRetByteArray().length<=0)
      throw new PacketException("解包出错");
    if (unpackResult.getPosIncValue()<=0)
      throw new PacketException("解包出错");
    return unpackResult;
  }

  public static TradeParam getTradeConfig(String tradeCode)throws PacketException{
      CB8583Config tradeConfig = CB8583Config.getInstance();
      TradeParam tradeParam = (TradeParam)tradeConfig.tradeList.get(tradeCode);
      return tradeParam;
  }

  public static ArrayList packHeader(String tradeCode,String cardNo,String traceNo)throws PacketException{
      CB8583Config tradeConfig = CB8583Config.getInstance();
      TradeParam tradeParam = getTradeConfig(tradeCode);
      if(tradeParam == null)
         return null;
      if(tradeParam.paramList == null || tradeParam.paramList.get("header") == null)
         return null;
      String[] headerList = SysFunction.splitString((String)tradeParam.paramList.get("header"),"|");
      if(headerList == null || headerList.length < 11)
         return null;
      CB8583PacketHeader header = new CB8583PacketHeader(headerList[0],headerList[1],tradeParam.tradeToCode,tradeConfig.acqInstID,
                                       tradeConfig.termID,traceNo,
                                       headerList[2],cardNo,headerList[3],headerList[4],headerList[5],
                                       headerList[6],headerList[7]);
      ArrayList retHeader = new ArrayList();
      retHeader.add(header.getPacketHeader());
      retHeader.add(headerList);
      System.out.println("Packet Header:"+new String(header.getPacketHeader())+" Header len:"+header.getPacketHeader().length);
      return retHeader;
  }

  public static byte[] pack8583(String tradeCode,TradePacket requestPack)throws PacketException{
      String[] s =null;
      byte[] srcvalue = null;
      byte[] desvalue = null;
      String cardNo = null;
      byte[] headerBuf = null;
      byte[] bodyBuf = new byte[CB8583Const.MAX_PACKET_LEN];
      byte[] fieldByteArray = null;
      byte[] destArrayBuf = null;
      DataField tmpDataField = null;
      CB8583Bitmap packetBitmap = new CB8583Bitmap();
      Map fieldDataList = new HashMap();
      ArrayList retHeader = null;
      int fieldindex = 0;
      int start = 0;
      int domainNo = 0;
      int startPos = 0;
      int fieldLen = 0;
      int convertMethod = 0;
      int bufferCurPos = 0;
      String[] ruleList = null;
      String[] fieldList = null;
      try{
        System.out.println("doPack..............");
        TradeParam tradeParam = getTradeConfig(tradeCode);
        if(tradeParam == null || tradeParam.paramList == null)
           throw new PacketException("没有配置交易");
        Map paramList = tradeParam.paramList;
        String inParam = new StringBuffer("|").append((String)paramList.get("in")).toString();
        byte[] tmpDataFieldValue = null;
        byte[] tmpArrayByte = null;
        for(int i = 0; i < requestPack.getSize(); i++){
          tmpDataField = (DataField) requestPack.getDomain(i);
          tmpArrayByte = null;
          //System.out.println("Pack field data:"+new String(tmpDataField.value)+" Rule:"+tmpDataField.fieldRule);
          if (tmpDataField == null)
            continue;
          if (tmpDataField.getValue() == null)
            throw new PacketException("打包出错,空字段");
          if (tmpDataField.getValue().length <= 0)
            throw new PacketException("打包出错,空字段");
          fieldList = SysFunction.splitString(new String(tmpDataField.value).trim(), "|");
          int paramStartIndex = inParam.indexOf("|"+fieldList[0]);
          if(paramStartIndex < 0){
            throw new PacketException("输入参数没有配置");
          }
          int paramEndIndex = inParam.indexOf("|",paramStartIndex+1);//取第一个|前面的数据
          if(paramEndIndex >= 0){
           // System.out.println("Config field param:"+inParam.substring(paramStartIndex+1,paramEndIndex));
            ruleList = SysFunction.splitString(inParam.substring(paramStartIndex+1,paramEndIndex), ":");
          }else{
           // System.out.println("Config field param:"+inParam.substring(paramStartIndex+1));
            ruleList = SysFunction.splitString(inParam.substring(paramStartIndex+1),":");
          }
          domainNo = Integer.parseInt(ruleList[0]);
          startPos = Integer.parseInt(ruleList[1]);
          fieldLen = Integer.parseInt(ruleList[2]);
          convertMethod = Integer.parseInt(ruleList[3]);
          tmpDataFieldValue = fieldList[1].getBytes();
          tmpDataField.fieldRule = new StringBuffer(ruleList[0]).append(":").append(ruleList[1]).toString();
          if (domainNo == 2)
            cardNo = new String(tmpDataFieldValue);
          if (convertMethod != 0) {
            //System.out.println("Convert field Value:"+tmpDataFieldValue);
            if(convertMethod != 888)
              tmpDataFieldValue = TradeDataConvert.convertData(tmpDataFieldValue,convertMethod,fieldLen);
            else if(convertMethod == 889){
              if(ruleList[4].equals(new String(tmpDataFieldValue)))
                  tmpDataFieldValue = ruleList[4].getBytes();
            }
            else
              tmpDataFieldValue = ruleList[4].getBytes();
           // System.out.println("Convert field Value1:"+tmpDataFieldValue);
          }
          if (fieldDataList.containsKey(ruleList[0])) {
            tmpArrayByte = (byte[]) fieldDataList.get(ruleList[0]);
            int tmpFieldLen = startPos + tmpDataFieldValue.length;
            if (tmpArrayByte.length < tmpFieldLen) {
              tmpArrayByte = SysFunction.changeMemLen(tmpArrayByte, tmpFieldLen);
            }
            System.arraycopy(tmpDataFieldValue, 0, tmpArrayByte, startPos,
                             tmpDataFieldValue.length);
            fieldDataList.put(ruleList[0], tmpArrayByte);
          }
          else {
            //System.out.println("====Rule:"+ruleList[0]+" Value:"+new String(tmpDataFieldValue));
            if (startPos == 0) {
              if (domainNo != 48)
                fieldDataList.put(ruleList[0], tmpDataFieldValue);
              else {
                if (domainNo == 48) {
                  tmpArrayByte = new byte[86];
                  CB8583ConvertMethod.clearByteArray(tmpArrayByte, (byte) 0x20);
                }
                System.arraycopy(tmpDataFieldValue, 0, tmpArrayByte, 0,
                                 tmpDataFieldValue.length);
                fieldDataList.put(ruleList[0], tmpArrayByte);
              }
            }else {
              if (domainNo == 48) {
                tmpArrayByte = new byte[86];
                CB8583ConvertMethod.clearByteArray(tmpArrayByte, (byte) 0x20);
              }
              else
                tmpArrayByte = new byte[startPos + tmpDataFieldValue.length];
              System.arraycopy(tmpDataFieldValue, 0, tmpArrayByte, startPos,
                               tmpDataFieldValue.length);
              fieldDataList.put(ruleList[0], tmpArrayByte);
            }
          }
          packetBitmap.openBit(domainNo);
          ruleList = null;
          fieldList = null;
        }

⌨️ 快捷键说明

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