近期由于物联网的发展,IOT设备对接,成为每个公司必不可少的事情。硬件方面都是采用的MQTT和服务端通讯,这里我们不讲MQTT,因为网上的资料很多,我们来点干货:如何把MQTT的通讯数组(字节数组)转换成我们熟悉的javaBean。
首先做一下简单的介绍一下。java的数组类型占的字节长度。
byte占一个字节
short占两个字节
int占4四个字节
long占8个字节
以上4个类型,是我们转换中最常用的,根据占用的字节长度来选择对应的类型。举个例子:

根据以上协议,我们定义的javaBean是这样子的。

OK,准备工作完成。接下来上工具类。
ArrayEntityUtil .java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.dlysapie.mqtt.util;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.Queue;
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
/**
* 将byte[]转换为实体
*
* @author baibai
*/
public class ArrayEntityUtil {
private static Logger LOG = LogManager.getLogger(ArrayEntityUtil.class);
public static <T> T toEntity(Class<T> entityClass, byte[] data) {
try {
Queue<Byte> list = new LinkedList<Byte>();
for (int i = 0; i < data.length; i++) {
list.add(data[i]);
}
return toEntity(entityClass, list);
} catch (Exception e) {
String temp = "";
for (int i = 0; i < data.length; i++) {
temp += ((Integer.toString(data[i] & 0xff, 16)) + ",");
}
LOG.error("解析数据异常,数据【" + temp + "】", e);
}
return null;
}
/**
*
* @param <T>
* @param entityClass 转换目标实体
* @param list byte[]队列数据
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InstantiationException
*/
private static <T> T toEntity(Class<T> entityClass, Queue<Byte> list)
throws IllegalArgumentException, IllegalAccessException, InstantiationException {
Field[] field = entityClass.getDeclaredFields();
T obj = entityClass.newInstance();
for (Field f : field) {
if (!list.isEmpty()) {
f.setAccessible(true);
// 1个字节不代替
if (f.getType().equals(byte.class)) {
f.setByte(obj, list.poll());
}
// 4个字节用long代替
else if (f.getType().equals(long.class)) {
if (f.getAnnotation(ByteCount.class).count() == 4) {
int[] longs = new int[8];
for (int i = 0; i < 4; i++) {
longs[i] = list.poll();
}
for (int i = 4; i < 8; i++) {
longs[i] = 0x00;
}
f.setLong(obj, DataConUtil.intArray2Long(longs));
} else {
int[] longs = new int[8];
for (int i = 0; i < 8; i++) {
longs[i] = list.poll();
}
f.setLong(obj, DataConUtil.intArray2Long(longs));
}
}
// 8个字节用long代替
else if (f.getType().equals(Long.class)) {
int[] longs = new int[8];
for (int i = 0; i < 8; i++) {
longs[i] = list.poll();
}
f.set(obj, new Long(DataConUtil.intArray2Long(longs)));
}
// 2个字节不代替
else if (f.getType().equals(short.class)) {
int[] shorts = new int[2];
for (int i = 0; i < 2; i++) {
shorts[i] = list.poll();
}
f.setShort(obj, DataConUtil.lBytesToShort(shorts));
}
// 4个字节float不代替
else if (f.getType().equals(float.class)) {
int[] floats = new int[4];
for (int i = 0; i < 4; i++) {
floats[i] = list.poll();
}
f.setFloat(obj, DataConUtil.lBytesToFloat(floats));
}
// 4个字节int不代替
else if (f.getType().equals(Integer.class) || f.getType().equals(int.class)) {
int[] ints = new int[4];
for (int i = 0; i < 4; i++) {
ints[i] = list.poll();
}
f.setInt(obj, DataConUtil.lBytesToInt(ints));
}
// 4个字节int不代替
else if (f.getType().equals(int.class) || f.getType().equals(int.class)) {
int[] ints = new int[4];
for (in

最低0.47元/天 解锁文章
945





