分享

java和C结构体通信

 mrjbydd 2013-07-22

java和C结构体通信

分类: Java 1228人阅读 评论(1) 收藏 举报
  1. package com.cvicse.naba.service.impl.user;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import com.cvicse.naba.common.utils.ByteConvert;  
  9. import com.cvicse.naba.common.utils.ObjectUtils;  
  10. import com.cvicse.naba.service.api.msg.IMessageManSrv;  
  11. import com.cvicse.naba.service.api.user.IUserManageSrv;  
  12. import com.cvicse.unicorn.framework.entity.CB_User;  
  13.   
  14. /** 
  15.  *  
  16.  *  
  17.  * 描述: 
  18.  * <p> 
  19.  *     用户操作数据接口实现类。 
  20.  * </p> 
  21.  * 创建日期:2012-7-9 下午3:39:46<br> 
  22.  *  
  23.  * @author:tianyj<br> 
  24.  * @update:$Date$<br> 
  25.  * @version:$Revision$<br> 
  26.  * @since 1.0.0 
  27.  */  
  28. public class UserManageSrvImpl implements IUserManageSrv {  
  29.   
  30.     private static final int STRUCTS_LENGTH = 316;  
  31.   
  32.     private IMessageManSrv messageManSrv;  
  33.   
  34.     /** 
  35.      * 保存用户信息到后台 
  36.      *  
  37.      * @param user 
  38.      *          当前登陆user对象 
  39.      * @param map 
  40.      *          添加的数据信息 
  41.      * @return boolean 
  42.      *          成功与否,true成功;false失败 
  43.      * @throws Exception 
  44.      */  
  45.     @Override  
  46.     public String saveUser(CB_User user, Map<String, String> map) throws Exception {  
  47.         // 方法坐标  
  48.         user.setC_x(20);  
  49.         user.setC_y(1);  
  50.         // 保存前台传递的用户数据信息  
  51.         // String userInfo = this.getSaveUserMessage(map);  
  52.         byte[] returnMsg = messageManSrv.exeCommand(user, getSaveUserMessage(map));  
  53.         String result = null;  
  54.         if (returnMsg != null) {  
  55.             if (handleResult(returnMsg)) {  
  56.                 result = "true";  
  57.             } else {  
  58.                 result = "false";  
  59.             }  
  60.         } else {  
  61.             result = "logout";  
  62.         }  
  63.         // 处理后台返回的操作结果  
  64.         return result;  
  65.     }  
  66.       
  67.     /** 
  68.      * 更新用户信息, 
  69.      * 只是调用后台的方法不同 通过设置user.setC_X和user.setC_Y方法坐标确定 
  70.      * @param user 
  71.      *            user对象,主要是方法坐标 
  72.      * @param map 
  73.      *            更新的信息 
  74.      * @return 
  75.      * @throws Exception 
  76.      */  
  77.     @Override  
  78.     public String updateUser(CB_User user, Map<String, String> map) throws Exception {  
  79.         user.setC_x(20);  
  80.         user.setC_y(4);  
  81.         // 更新前台需要更新的用户信息  
  82.         // String userInfo = this.getUpdateUserMessage(map);  
  83.         byte[] returnMsg = messageManSrv.exeCommand(user, getUpdateUserMessage(map));  
  84.         String result = null;  
  85.         if (returnMsg != null) {  
  86.             if (handleResult(returnMsg)) {  
  87.                 result = "true";  
  88.             } else {  
  89.                 result = "false";  
  90.             }  
  91.         } else {  
  92.             result = "logout";  
  93.         }  
  94.         // 处理后台操作返回的结果  
  95.         return result;  
  96.     }  
  97.       
  98.     /** 
  99.      * 修改用户登陆密码,没有用到暂时 
  100.      * @param user 
  101.      * @param map 
  102.      * @throws Exception 
  103.      * @return  
  104.      */  
  105.     @Override  
  106.     public boolean updatePassword(CB_User user, Map<String, String> map) throws Exception {  
  107.         // TODO Auto-generated method stub  
  108.         String passInfo = "";  
  109.         byte[] returnMsg = messageManSrv.exeCommand(user, passInfo);  
  110.         // 处理后台操作返回的结果  
  111.         return handleResult(returnMsg);  
  112.     }  
  113.       
  114.     /** 
  115.      * 删除用户 
  116.      * @parma user 
  117.      *          当前登陆user对象 
  118.      * @param str 
  119.      *          要删除的user的ID 
  120.      * @throws Exception 
  121.      *           
  122.      * @return boolean  
  123.      *          返回是否删除成功:true成功;false失败 
  124.      */  
  125.     @Override  
  126.     public String deleteUser(CB_User user, String str) throws Exception {  
  127.         user.setC_x(20);  
  128.         user.setC_y(3);  
  129.         // 处理返回的信息  
  130.         byte[] returnMsg = messageManSrv.exeCommand(user, str);  
  131.         String result = null;  
  132.         if (returnMsg != null && handleResult(returnMsg)) {  
  133.             if (handleResult(returnMsg)) {  
  134.                 result = "true";  
  135.             } else {  
  136.                 result = "false";  
  137.             }  
  138.         } else {  
  139.             result = "logout";  
  140.         }  
  141.         return result;  
  142.     }  
  143.       
  144.     /** 
  145.      * 获得单个用户信息 
  146.      */  
  147.     @Override  
  148.     public Map<String, String> getUserMap(CB_User user, String userID) throws Exception {  
  149.         // 处理后台传递的字符串进行解析  
  150.         Map<String, String> map = null;  
  151.         byte[] returnMsg = messageManSrv.exeCommand(user, "");  
  152.         // 处理字节转换为CB_User  
  153.         byte[] struct = new byte[returnMsg.length - 7];  
  154.         System.arraycopy(returnMsg, 7, struct, 0, returnMsg.length - 7);  
  155.         map = this.getUserInfoFromByte(struct);  
  156.         return map;  
  157.     }  
  158.   
  159.     /** 
  160.      * 返回的returnMsg格式【4个字节+1个字节+2个字节+结构体列表字节】 
  161.      * 4个字节表示传递长度但是不包括该4个字节在内 1个字节表示成功或者失败 
  162.      * 2个字节表示调用后台的方法坐标 结构体字节:每个结构体都是固定长度, 
  163.      * 不足补0,需要把结构体列表 进行解析处理 
  164.      */  
  165.     @Override  
  166.     public List<Map<String, String>> getUserListMap(CB_User user) throws Exception {      
  167.         // 设置调用后台的方法坐标  
  168.         user.setC_x(20);  
  169.         user.setC_y(2);  
  170.         // 处理后台传递的用户信息列表  
  171.         List<Map<String, String>> list = null;  
  172.         // 执行后台程序返回结果  
  173.         byte[] returnMsg = messageManSrv.exeCommand(user, "");  
  174.         if (returnMsg != null) {  
  175.             // 后台处理成功,进行解析字符串  
  176.             if (handleResult(returnMsg)) {  
  177.                 // 处理字节转换为List进行返回,returnMsg.length-7表示结构体列表长度  
  178.                 byte[] structs = new byte[returnMsg.length - 7];  
  179.                 // 把结构体列表信息字节复制到另外的字节数组,以便处理  
  180.                 System.arraycopy(returnMsg, 7, structs, 0, returnMsg.length - 7);  
  181.                 // 处理结构体数组,返回List<Map>,Map中存放一个结构体信息  
  182.                 list = this.getUserInfoListFromByte(structs);  
  183.             }  
  184.         } else {  
  185.             // 处理超时登陆  
  186.             list = new ArrayList<Map<String, String>>();  
  187.             Map<String, String> map = new HashMap<String, String>();  
  188.             map.put("logout""logout");  
  189.             list.add(map);  
  190.         }  
  191.           
  192.         return list;  
  193.     }  
  194.       
  195.     /** 
  196.      * 解析后台操作的结果 
  197.      *  
  198.      * @param msg 
  199.      * @return 
  200.      */  
  201.     private boolean handleResult(byte[] msg) {  
  202.         boolean flag = false;  
  203.         if (msg.length >= 7) {  
  204.             // 消息【4个字节+1个字节+2个字节+n个字节】  
  205.             int result = msg[4];  
  206.             if (0 == result) {  
  207.                 flag = true;  
  208.             }  
  209.         }  
  210.         return flag;  
  211.     }  
  212.   
  213.     /** 
  214.      * 依赖注入IMessageManSrv 
  215.      *  
  216.      * @param messageManSrv 
  217.      */  
  218.     public void setMesageManSrv(IMessageManSrv messageManSrv) {  
  219.         this.messageManSrv = messageManSrv;  
  220.     }  
  221.   
  222.     /** 
  223.      * 释放注入的IMessageManSrv 
  224.      *  
  225.      * @param messageManSrv 
  226.      */  
  227.     public void unSetMessageManSrv(IMessageManSrv messageManSrv) {  
  228.         this.messageManSrv = null;  
  229.     }  
  230.   
  231.     /** 
  232.      * 封装后台结构体,传递给后台进行存储操作  
  233.      * typedef struct {  
  234.      *  unsigned char cmdtype;  
  235.      *  unsigned char subcmd;  
  236.      *  unsigned char operate; //1.add, 2.change添加、修改  
  237.      *  unsigned char passflag;//是否采用USB-KEY方式登录  
  238.      *  unsigned char priority;  
  239.      *  unsigned char state;---//状态,禁用与否  
  240.      *  空两个字节 
  241.      *  int permission;  
  242.      *  int authtype;  
  243.      *  unsigned char userid[16];//用户ID  
  244.      *  unsigned char username[16];//用户名 
  245.      *  unsigned char passfirst[16];//密码  
  246.      *  unsigned char passsecond[16];  
  247.      *  unsigned char emailaddr[64];//邮箱  
  248.      *  unsigned char dept[32];//部门 
  249.      *  unsigned char mobile[16];//电话 
  250.      *  unsigned char role[16];//角色  
  251.      *  unsigned char memo[128];//描述  
  252.      * } PKT_USER_REQ; 
  253.      *  
  254.      * @param map 
  255.      * @return 
  256.      */  
  257.     private byte[] getSaveUserMessage(Map<String, String> map) {  
  258.         /*StringBuffer msg = new StringBuffer();*/  
  259.         byte[] msgByte = new byte[336];  
  260.           
  261.         byte[] preSixMsg = new byte[6];  
  262.         preSixMsg[0] = 0;// cmdtype  
  263.         preSixMsg[1] = 0;// subcmd  
  264.         preSixMsg[2] = 1;// operate 1.add, 2.change添加、修改  
  265.         // passflag是否采用USB-KEY方式登录  
  266.         preSixMsg[3] = Byte.parseByte(ObjectUtils.toString(map.get("isUkey")));  
  267.         preSixMsg[4] = 0;// priority  
  268.   
  269.         // state状态,禁用与否  
  270.         preSixMsg[5] = Byte.parseByte(ObjectUtils.toString(map.get("state")));  
  271.         System.arraycopy(preSixMsg, 0, msgByte, 0, preSixMsg.length);  
  272.           
  273.         // 补零操作  
  274.         byte[] completeZero = {0,0};  
  275.         System.arraycopy(completeZero, 0, msgByte, 6, completeZero.length);  
  276.           
  277.         byte[] permissionByte = ByteConvert.intToLBytes(  
  278.                 Integer.parseInt(ObjectUtils.toString(map.get("permission"))));//{0, 0, 0, 0};  
  279.         System.arraycopy(permissionByte, 0, msgByte, 8, permissionByte.length);  
  280.           
  281.         // permissionByte[0] = Byte.parseByte(ObjectUtils.toString(map.get("permission")),2);  
  282.         byte[] authtypeByte = {0000};  
  283.         System.arraycopy(authtypeByte, 0, msgByte, 12, authtypeByte.length);  
  284.           
  285.         // 用户ID构造字节数组,不足16个字节补0  
  286.         byte[] userid = new byte[16];  
  287.         userid = buildByteArray(ObjectUtils.toString(map.get("userId")).getBytes(), userid);  
  288.         System.arraycopy(userid, 0, msgByte, 16, userid.length);  
  289.           
  290.         // 用户名字节数组,不足16个字节补0  
  291.         byte[] username = new byte[16];  
  292.         username = buildByteArray(ObjectUtils.toString(map.get("userName")).getBytes(), username);  
  293.         System.arraycopy(username, 0, msgByte, 32, username.length);  
  294.   
  295.         // 密码字节数组,不足16个字节补0  
  296.         byte[] passfirst = new byte[16];  
  297.         passfirst = buildByteArray(ObjectUtils.toString(map.get("password")).getBytes(), passfirst);  
  298.         System.arraycopy(passfirst, 0, msgByte, 48, passfirst.length);  
  299.           
  300.         // 二次密码  
  301.         byte[] passsecond = new byte[16];  
  302.         passsecond = buildByteArray(ByteConvert.intToBytes(0), passsecond);  
  303.         System.arraycopy(passsecond, 0, msgByte, 64, passsecond.length);  
  304.   
  305.         // 邮箱字节数组,不足64个字节补0  
  306.         byte[] emailaddr = new byte[64];  
  307.         emailaddr = buildByteArray(ObjectUtils.toString(map.get("email")).getBytes(), emailaddr);  
  308.         System.arraycopy(emailaddr, 0, msgByte, 80, emailaddr.length);  
  309.           
  310.         // 部门字节数组,不足32个字节补0  
  311.         byte[] dept = new byte[32];  
  312.         dept = buildByteArray(ObjectUtils.toString(map.get("dept")).getBytes(), dept);  
  313.         System.arraycopy(dept, 0, msgByte, 144, dept.length);  
  314.           
  315.         // 电话  
  316.         byte[] mobile = new byte[16];  
  317.         mobile = buildByteArray(ObjectUtils.toString(map.get("mobile")).getBytes(), mobile);  
  318.         System.arraycopy(mobile, 0, msgByte, 176, mobile.length);  
  319.           
  320.         // 角色  
  321.         byte[] role = new byte[16];  
  322.         role = buildByteArray(ObjectUtils.toString(map.get("role")).getBytes(), role);  
  323.         System.arraycopy(role, 0, msgByte, 192, role.length);  
  324.           
  325.         // 描述  
  326.         byte[] memo = new byte[128];  
  327.         memo = buildByteArray(ObjectUtils.toString(map.get("memo")).getBytes(), memo);  
  328.         System.arraycopy(memo, 0, msgByte, 208, memo.length);  
  329.         /*msg.append(ChangeCharSet.toUTF8(preSixMsg)) 
  330.             .append(ChangeCharSet.toUTF8(completeZero)) 
  331.             .append(ChangeCharSet.toUTF8(permissionByte)) 
  332.             .append(ChangeCharSet.toUTF8(authtypeByte)) 
  333.             .append(ChangeCharSet.toUTF8(userid)) 
  334.             .append(ChangeCharSet.toUTF8(username)) 
  335.             .append(ChangeCharSet.toUTF8(passfirst)) 
  336.             .append(ChangeCharSet.toUTF8(passsecond)) 
  337.             .append(ChangeCharSet.toUTF8(emailaddr)) 
  338.             .append(ChangeCharSet.toUTF8(dept)) 
  339.             .append(ChangeCharSet.toUTF8(mobile)) 
  340.             .append(ChangeCharSet.toUTF8(role)) 
  341.             .append(ChangeCharSet.toUTF8(memo));*/  
  342.   
  343.         return msgByte;  
  344.     }  
  345.   
  346.     /** 
  347.      * typedef struct { 
  348.      *  unsigned char cmdtype; 
  349.      *  unsigned char subcmd; 
  350.      *  unsigned char userid[USER_NAME_SIZE]; 
  351.      *  unsigned char chpass; //0:不修改  1:修改 
  352.      *  unsigned char oldpass[USER_PASS_SIZE]; 
  353.      *  unsigned char newpass[USER_PASS_SIZE]; 
  354.      *  unsigned char passflag; 
  355.      *  unsigned char priority; 
  356.      *  unsigned char state; 
  357.      *  int permission; 
  358.      *  int authtype; 
  359.      *  unsigned char username[USER_NAME_SIZE]; 
  360.      *  unsigned char passfirst[USER_PASS_SIZE]; 
  361.      *  unsigned char passsecond[USER_PASS_SIZE]; 
  362.      *  unsigned char emailaddr[USER_MAIL_SIZE]; 
  363.      *  unsigned char dept[32]; 
  364.      *  unsigned char mobile[USER_TLE_SIZE]; 
  365.      *  unsigned char role[USER_ROLE_SIZE]; 
  366.      *  unsigned char memo[USER_MEMO_LEN]; 
  367.      * } PKT_EDIT_USER_REQ ; 
  368.      *  
  369.      * @param map 
  370.      * @return 
  371.      */  
  372.     private byte[] getUpdateUserMessage(Map<String, String> map){  
  373.         // StringBuffer msg = new StringBuffer();  
  374.         byte[] msgByte = new byte[368];  
  375.           
  376.         byte[] preTwo = new byte[2];  
  377.         preTwo[0] = 0;  
  378.         preTwo[1] = 0;  
  379.         System.arraycopy(preTwo, 0, msgByte, 0, preTwo.length);  
  380.           
  381.         // 用户ID构造字节数组,不足16个字节补0  
  382.         byte[] userid = new byte[16];  
  383.         userid = buildByteArray(ObjectUtils.toString(map.get("userId")).getBytes(), userid);  
  384.         System.arraycopy(userid, 0, msgByte, 2, userid.length);  
  385.           
  386.         // 是否修改密码  
  387.         byte[] chpass = new byte[1];  
  388.         chpass[0] = Byte.parseByte(ObjectUtils.toString(map.get("chpass")));  
  389.         System.arraycopy(chpass, 0, msgByte, 18, chpass.length);  
  390.           
  391.         // 旧密码  
  392.         byte[] oldPass = new byte[16];  
  393.         // 新密码  
  394.         byte[] newPass = new byte[16];  
  395.         // 修改密码为1  
  396.         if ("1".equals(ObjectUtils.toString(map.get("chpass")))) {  
  397.             oldPass = buildByteArray(ObjectUtils.toString(map.get("oldPass")).getBytes(), oldPass);  
  398.             newPass = buildByteArray(ObjectUtils.toString(map.get("newPass")).getBytes(), newPass);  
  399.         }   
  400.         System.arraycopy(oldPass, 0, msgByte, 19, oldPass.length);  
  401.         System.arraycopy(newPass, 0, msgByte, 35, newPass.length);  
  402.           
  403.         // 是否使用ukey  
  404.         byte[] passflag = new byte[1];  
  405.         passflag[0] = Byte.parseByte(ObjectUtils.toString(map.get("isUkey")));  
  406.         System.arraycopy(passflag, 0, msgByte, 51, passflag.length);  
  407.           
  408.         // priority,无用  
  409.         byte[] priority = {0};  
  410.         System.arraycopy(priority, 0, msgByte, 52, priority.length);  
  411.           
  412.         // 用户状态,是否被禁用  
  413.         byte[] state = new byte[1];  
  414.         state[0] = Byte.parseByte(ObjectUtils.toString(map.get("state")));  
  415.         System.arraycopy(state, 0, msgByte, 53, state.length);  
  416.           
  417.         // 补零  
  418.         byte[] completeZero = {00};  
  419.         System.arraycopy(completeZero, 0, msgByte, 54, completeZero.length);  
  420.           
  421.         // 无用  
  422.         byte[] permissionByte = ByteConvert.intToLBytes(  
  423.                 Integer.parseInt(ObjectUtils.toString(map.get("permission"))));  
  424.         System.arraycopy(permissionByte, 0, msgByte, 56, permissionByte.length);  
  425.           
  426.         byte[] authtypeByte = {0000};  
  427.         System.arraycopy(authtypeByte, 0, msgByte, 60, authtypeByte.length);  
  428.           
  429.         // 用户名字节数组,不足16个字节补0  
  430.         byte[] username = new byte[16];  
  431.         username = buildByteArray(ObjectUtils.toString(map.get("userName")).getBytes(), username);  
  432.         System.arraycopy(username, 0, msgByte, 64, username.length);  
  433.           
  434.         // 密码字节数组,不足16个字节补0,不用  
  435.         byte[] passfirst = new byte[16];  
  436.         passfirst = buildByteArray(ByteConvert.intToBytes(0), passfirst);  
  437.         System.arraycopy(passfirst, 0, msgByte, 80, passfirst.length);  
  438.           
  439.         // 二次密码,不用  
  440.         byte[] passsecond = new byte[16];  
  441.         passsecond = buildByteArray(ByteConvert.intToBytes(0), passsecond);  
  442.         System.arraycopy(passsecond, 0, msgByte, 96, passsecond.length);  
  443.           
  444.         // 邮箱字节数组,不足64个字节补0  
  445.         byte[] emailaddr = new byte[64];  
  446.         emailaddr = buildByteArray(ObjectUtils.toString(map.get("email")).getBytes(), emailaddr);  
  447.         System.arraycopy(emailaddr, 0, msgByte, 112, emailaddr.length);  
  448.           
  449.         // 部门字节数组,不足32个字节补0  
  450.         byte[] dept = new byte[32];  
  451.         dept = buildByteArray(ObjectUtils.toString(map.get("dept")).getBytes(), dept);  
  452.         System.arraycopy(dept, 0, msgByte, 176, dept.length);  
  453.           
  454.         // 电话  
  455.         byte[] mobile = new byte[16];  
  456.         mobile = buildByteArray(ObjectUtils.toString(map.get("mobile")).getBytes(), mobile);  
  457.         System.arraycopy(mobile, 0, msgByte, 208, mobile.length);  
  458.           
  459.         // 角色  
  460.         byte[] role = new byte[16];  
  461.         role = buildByteArray(ObjectUtils.toString(map.get("role")).getBytes(), role);  
  462.         System.arraycopy(role, 0, msgByte, 224, role.length);  
  463.           
  464.         // 描述  
  465.         byte[] memo = new byte[128];  
  466.         memo = buildByteArray(ObjectUtils.toString(map.get("memo")).getBytes(), memo);  
  467.         System.arraycopy(memo, 0, msgByte, 240, memo.length);  
  468.           
  469.         /*msg//.append(ChangeCharSet.toUTF8(preTwo)) 
  470.             .append(ChangeCharSet.toUTF8(userid)) 
  471.             .append(ChangeCharSet.toUTF8(chpass)) 
  472.             .append(ChangeCharSet.toUTF8(oldPass)) 
  473.             .append(ChangeCharSet.toUTF8(newPass)) 
  474.             .append(ChangeCharSet.toUTF8(passflag)) 
  475.             .append(ChangeCharSet.toUTF8(priority)) 
  476.             .append(ChangeCharSet.toUTF8(state)) 
  477.             .append(ChangeCharSet.toUTF8(completeZero)) 
  478.             .append(ChangeCharSet.toUTF8(permissionByte)) 
  479.             .append(ChangeCharSet.toUTF8(authtypeByte)) 
  480.             .append(ChangeCharSet.toUTF8(username)) 
  481.             .append(ChangeCharSet.toUTF8(passfirst)) 
  482.             .append(ChangeCharSet.toUTF8(passsecond)) 
  483.             .append(ChangeCharSet.toUTF8(emailaddr)) 
  484.             .append(ChangeCharSet.toUTF8(dept)) 
  485.             .append(ChangeCharSet.toUTF8(mobile)) 
  486.             .append(ChangeCharSet.toUTF8(role)) 
  487.             .append(ChangeCharSet.toUTF8(memo));*/  
  488.       
  489.         return msgByte;  
  490.     }  
  491.       
  492.     /** 
  493.      * 拷贝字节数组,不足长度补0,0的编码为48; 
  494.      *  
  495.      * @param srcArry 
  496.      * @param destArry 
  497.      * @param length 
  498.      * @return 
  499.      */  
  500.     private byte[] buildByteArray(byte[] srcArry, byte[] destArry) {  
  501.         int length = srcArry.length;  
  502.         if (srcArry.length > destArry.length) {  
  503.             length = destArry.length;  
  504.         }  
  505.         System.arraycopy(srcArry, 0, destArry, 0, length);  
  506.         return destArry;  
  507.     }  
  508.   
  509.     /** 
  510.      * 处理后台传递过来的多个结构体的方法,固定一个结构体的长度为313个字节 把结构体数组字节分割成单个结构体数组,然后进行对每个结构体数组进行 
  511.      * 解析,存放到Map中,Map中对应的是一个结构体,也就是用户信息 
  512.      *  
  513.      * @param structs 
  514.      *            结构体列表字节数组 
  515.      * @return 
  516.      */  
  517.     private List<Map<String, String>> getUserInfoListFromByte(byte[] structs) throws Exception {  
  518.         List<Map<String, String>> list = new ArrayList<Map<String, String>>();  
  519.         // 把结构体字节数组进行分割成多个结构体  
  520.         for (int i = 0; i < structs.length / STRUCTS_LENGTH; i++) {  
  521.             // 复制到新的byte数组  
  522.             byte[] struct = new byte[STRUCTS_LENGTH];  
  523.             // 在原数组复制操作的起始位置  
  524.             int srcPosition = i * STRUCTS_LENGTH;  
  525.             System.arraycopy(structs, srcPosition, struct, 0, STRUCTS_LENGTH);  
  526.             // 解析单个结构体  
  527.             Map<String, String> temp = getUserInfoFromByte(struct);  
  528.             list.add(temp);  
  529.         }  
  530.         return list;  
  531.     }  
  532.   
  533.     /** 
  534.      * 请求返回的用户列表结构体定义  
  535.      * typedef struct {  
  536.      * 0 unsigned char passflag; // 是否启用UKey 
  537.      * 1 unsigned char priority;  
  538.      * 2 unsigned char state;  
  539.      * 3 unsigned char reserve;  
  540.      * 4 unsigned char authtype;  
  541.      * 5 unsigned char userid[16];  
  542.      * 21 unsigned char username[16];  
  543.      * 37 unsigned char emailaddr[64];  
  544.      * 101 unsigned char dept[32];  
  545.      * 133 unsigned char mobile[16]; 
  546.      * 149 unsigned char role[16];  
  547.      * 168 int permission;  
  548.      * 172 unsigned int createtime; 
  549.      * 176 unsigned int lasttime;  
  550.      * 180 int logintimes;  
  551.      * 184 int failtimes; 181 unsigned 
  552.      * 188 char memo[128]; 185 } PKT_USER_LIST; 
  553.      *  
  554.      * @param struct 
  555.      * @return 
  556.      * @throws Exception 
  557.      */  
  558.     private Map<String, String> getUserInfoFromByte(byte[] struct) throws Exception {  
  559.         Map<String, String> map = new HashMap<String, String>();  
  560.         map.put("isUkey", String.valueOf(struct[0]));  
  561.         map.put("state", String.valueOf(struct[2]));  
  562.         map.put("userId", getFieldValue(struct, 516"string"));  
  563.         map.put("userName", getFieldValue(struct, 2116"string"));  
  564.         map.put("email", getFieldValue(struct, 3764"string"));  
  565.         map.put("dept", getFieldValue(struct, 10132"string"));  
  566.         map.put("mobile", getFieldValue(struct, 13316"string"));  
  567.         map.put("role", getFieldValue(struct, 14916"string"));  
  568.         map.put("permission", getFieldValue(struct, 1684"integer"));  
  569.         map.put("createtime", getFieldValue(struct, 1724"time"));  
  570.         map.put("lasttime", getFieldValue(struct, 1764"time"));  
  571.         map.put("logintimes", getFieldValue(struct, 1804"integer"));  
  572.         map.put("failtimes", getFieldValue(struct, 1844"integer"));  
  573.         map.put("memo", getFieldValue(struct, 188128"string"));  
  574.         return map;  
  575.     }  
  576.   
  577.     /** 
  578.      * 处理结构体中单个数据, 从给定的字节数组中取出特定位置的字节转换为字符串 
  579.      *  
  580.      * @param bty 
  581.      * @param srcPosition 
  582.      * @param length 
  583.      * @return 
  584.      */  
  585.     private String getFieldValue(byte[] bty, int srcPosition, int length, String converFlag) throws Exception {  
  586.         // 拷贝规定的字节长度  
  587.         byte[] temp = new byte[length];  
  588.         String returnStr = "";  
  589.         System.arraycopy(bty, srcPosition, temp, 0, length);  
  590.         if ("".equals(converFlag) || "string".equals(converFlag)) {  
  591.             // 记录有效字节的长度  
  592.             int position = 0;  
  593.             /* 
  594.              * 字节数组从后面截取,到第一个不为0停止,记录位置,0-position为有效字节 
  595.              * 例子:[12,52,89,0,0,0,0],有效字节为前面的非0字节 
  596.              */  
  597.             for (int i = temp.length - 1; i >= 0; i--) {  
  598.                 position = i;  
  599.                 if (temp[i] != 0) {  
  600.                     break;  
  601.                 }  
  602.             }  
  603.             // 把有效字节转为为字符串  
  604.             byte[] validValue = new byte[position + 1];  
  605.             System.arraycopy(temp, 0, validValue, 0, position + 1);  
  606.             returnStr = new String(validValue);  
  607.         } else if ("time".equals(converFlag)) {  
  608.             long time = ByteConvert.lBytesToInt(temp);  
  609.             returnStr = ObjectUtils.get24Time(time*1000);  
  610.             /*Date date = new Date(time*1000); 
  611.             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
  612.             returnStr = sdf.format(date);*/  
  613.         } else if ("integer".equals(converFlag)) {  
  614.             returnStr = String.valueOf(ByteConvert.lBytesToInt(temp));  
  615.         }  
  616.           
  617.         return returnStr;  
  618.     }  
  619.   
  620. }  


 


PS:

1、C中int类型的数据在内存中存放和int类型占用的字节数有关,因此有些地方需要补零操作。例如,C在系统中占用4个字节,int类型的数据 在内存中存放开始字节是被4整除的地址,通过socket传递过来的字节数组在相应的位置会进行补零(C端进行操作),在Java中解析字节时或者构造相 应的结构体字节数组时都要对补零的地方进行操作,去掉补充的零占位字节或者添加相应的补零操作,然后进行传递。

2、在C中的字符串会有结束标识"\0",从Java端传递的数据不要占满整个字节数组,例如:unsigned char emailaddr[64],java字节数组如果填充满了该字符数组,C进行构造该字节数组时会舍去一部分,会造成乱码。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多