Java实现对象转成字节数组(整型支持按位写入,字符串则按字节写入)

闲着无聊,写了一个对象转换成byte[]的工具类,支持整型按位写入(大大节省空间),具体步骤如下:

1. 定义实体类和注解

public class User {

    /**
     * ID,4个字节,32bit
     */
    @JSONField(ordinal = 1)
    @BitPos(offset=0,size = 32)
    public int id;

    /**
     * 姓名,10个字节(80bit)
     */
    @JSONField(ordinal = 2)
    @BitPos(offset = 32, size= 80)
    public String name;

    /**
     * 性别,0:男,1:女,1Bit
     */
    @JSONField(ordinal = 3)
    @BitPos(offset = 112, size = 1)
    public int sex;

    /**
     * 年龄,最大127,7Bit
     */
    @JSONField(ordinal = 4)
    @BitPos(offset = 113, size=7)
    public int age;

    /**
     * 身高,最大2^10-1=1023cm,10Bit
     */
    @JSONField(ordinal = 5)
    @BitPos(offset = 120, size = 10)
    public int height;

    /**
     * 体重,最大2^10-1=1023kg,10Bit
     */
    @JSONField(ordinal = 6)
    @BitPos(offset = 130, size = 10)
    public int weight;

    /**
     * 多少个月的薪水,最大2^4-1=15个月薪,4Bit
     */
    @JSONField(ordinal = 7)
    @BitPos(offset = 140, size=4)
    public int monthSalary;

    /**
     * 地址:20字节,160bit
     */
    @JSONField(ordinal = 8)
    @BitPos(offset = 144, size = 160)
    public String address;


}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface BitPos {

    /**
     * 位置(占总长度的位置)
     */
    int offset() default  -1;

    /**
     * 长度(多少bit)
     */
    int size() default  -1;
}

2. 工具类

package com.hdwang.test.bit;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;

/**
 * 对象转位字节(整型实现按bit写入,字符串则按字节写入)
 *
 * 时间: 2024/1/23 11:12
 */
public class ObjToBitBytesUtil {

    public static void main(String[] args) throws IllegalAccessException, UnsupportedEncodingException {
        User user = new User();
        user.id = 10001;
        user.name = "张三";
        user.sex = 0;
        user.age = 18;
        user.height = 170;
        user.weight = 50;
        user.monthSalary = 13;
        user.address = "浙江杭州西湖";

        System.out.println("原始对象:");
        System.out.println(JSON.toJSONString(user, SerializerFeature.WriteNullStringAsEmpty));

        //对象写成字节数组
        byte[] bytes = writeObjToBitBytes(user, 38);
        System.out.println("对象的字节数组16进制表示:");
        printHex(bytes);


        //字节数组转成对象
        User readUser = readBitBytesToObj(bytes);
        System.out.println("字节数组转换成的对象:");
        System.out.println(JSON.toJSONString(readUser,SerializerFeature.WriteNullStringAsEmpty));
    }

    private static User readBitBytesToObj(byte[] bytes) throws IllegalAccessException, UnsupportedEncodingException {
        User user = new User();
        Field[] fields = user.getClass().getFields();
        for (Field field : fields) {
            BitPos bitPos = field.getAnnotation(BitPos.class);
            Object val = readField(bytes, field, bitPos.offset(), bitPos.size());
            field.set(user,val);
        }
        return user;
    }

    private static Object readField(byte[] buffer, Field field, int offset, int size) throws UnsupportedEncodingException {
        Object val = null;
       if(field.getType().equals(int.class) || field.getType().equals(Integer.class)){
           // 整型,按位读取
           int valInt = 0;

           //起始缓存位置(第几个字节,从0开始)
           int startBufferIndex = offset / 8;
           //起始字节已经占用了多少bit
           int startByteUsedBit = offset % 8;
           //起始字节需要读取多少bit(默认剩余bit全部读取)
           int startByteReadBit = 8 - startByteUsedBit;
           if(size < startByteReadBit){
               //需要读取的bit数少,重置为实际需要读取的bit数
               startByteReadBit = size;
           }
           //结束缓存位置(第几个字节)
           int endBufferIndex = (offset + size - 1) / 8;
           int endByteUseBit = ((offset + size - 1) % 8)+1;
           // 缓存间隔位置(缓存起止位置之间的间隔字节数)
           int gapByteCount = endBufferIndex - startBufferIndex - 1;

           // 1. 读取起始字节(读高位)
           byte lowerByte = buffer[startBufferIndex];
           lowerByte = (byte) (lowerByte >>> startByteUsedBit);
           int mask = (1 << startByteReadBit) - 1;
           valInt = lowerByte & mask;

           // 2. 读取中间字节(读整个字节)
           if(gapByteCount > 0) {
               for (int i = 0; i < gapByteCount; i++) {
                   int leftMove = startByteReadBit + (i * 8);
                   byte b = buffer[startBufferIndex+(i+1)];
                   valInt |= (b << leftMove);
               }
           }

           // 3. 读取结束字节(读取低位)
           if(endBufferIndex > startBufferIndex) {
               byte b = buffer[endBufferIndex];
               int leftMove = startByteReadBit + gapByteCount * 8;
               mask = (1 << endByteUseBit) - 1;
               valInt |= ((b & mask) << leftMove);
           }
           val = valInt;
       }else{
           // 字符串按字节读取
           byte[] bytes = new byte[size/8];
           int startBufferIndex = offset / 8;
           for(int i=0;i< bytes.length;i++){
               bytes[i] = buffer[startBufferIndex++];
           }
           String valStr = new String(bytes,"utf-8");
           val = valStr;
       }
       return val;
    }

    private static byte[] writeObjToBitBytes(User user, int bufferSize) throws IllegalAccessException, UnsupportedEncodingException {
        byte[] buffer = new byte[bufferSize];
        Field[] fields = user.getClass().getFields();
        for (Field field : fields) {
            BitPos bitPos = field.getAnnotation(BitPos.class);
            Object val = field.get(user);
            writeField(buffer, val, bitPos.offset(), bitPos.size());
        }
        return buffer;
    }

    private static void writeField(byte[] buffer, Object val, int offset, int size) throws UnsupportedEncodingException {
        if (val instanceof Integer) {
            // 整型,按位写入
            int valInt = (int) val;

            //起始缓存位置(第几个字节,从0开始)
            int startBufferIndex = offset / 8;
            //起始字节已经占用了多少bit
            int startByteUsedBit = offset % 8;
            //起始字节需要写入多少bit(默认剩余bit全部写入)
            int startByteWriteBit = 8 - startByteUsedBit;
            if(size < startByteWriteBit){
                //需要写入的bit数少,重置为实际需要写入的bit数
                startByteWriteBit = size;
            }
            //结束缓存位置(第几个字节)
            int endBufferIndex = (offset + size - 1) / 8;
            int endByteUseBit = ((offset + size - 1) % 8)+1;
            // 缓存间隔位置(缓存起止位置之间的间隔字节数)
            int gapByteCount = endBufferIndex - startBufferIndex - 1;

            // 1. 写入起始字节(之前在低位可能已经存在值了,现在写入的写到高位去)
            int mask = (1 << startByteWriteBit) - 1;
            //取低startByteWriteBit位,左移startByteUsedBit位,与原来的值或操作即可写入高位
            buffer[startBufferIndex] |= ((valInt & mask) << startByteUsedBit);

            // 2. 写中间字节(全字节写入)
            if(gapByteCount > 0) {
                for (int i = 0; i < gapByteCount; i++) {
                    int rightMove = startByteWriteBit + (i * 8);
                    buffer[startBufferIndex+(i+1)] = (byte) ((valInt >> rightMove) & 0xFF);
                }
            }

            // 3. 写结束字节(写入低位即可)
            if(endBufferIndex > startBufferIndex) {
                int rightMove = startByteWriteBit + gapByteCount * 8;
                mask = (1 << endByteUseBit) - 1;
                buffer[endBufferIndex] = (byte) ((valInt >> rightMove) & mask);
            }
        } else {
            // 字符串直接按字节写入
            byte[] bytes = val.toString().getBytes("utf-8");
            int actualByteCount = bytes.length;
            int startBufferIndex = offset / 8;
            int endBufferIndex = startBufferIndex + size/8-1;
            int byteIndex = 0;
            for(int i = startBufferIndex; i<=endBufferIndex; i++) {
                if(byteIndex <= actualByteCount-1) {
                    buffer[i] = bytes[byteIndex++];
                }else{
                    //补空格
                    buffer[i] = ' ';
                }
            }
        }

    }


    private static void printHex(byte[] byteArray) {
        for (byte b : byteArray) {
            // 将每个字节转换为16进制字符串
            String hex = String.format("%02X", b);
            System.out.print(hex + " ");
        }
        System.out.println();
    }
}

 

 

3. 测试结果

原始对象:
{"id":10001,"name":"张三","sex":0,"age":18,"height":170,"weight":50,"monthSalary":13,"address":"浙江杭州西湖"}
对象的字节数组16进制表示:
11 27 00 00 E5 BC A0 E4 B8 89 20 20 20 20 24 AA C8 D0 E6 B5 99 E6 B1 9F E6 9D AD E5 B7 9E E8 A5 BF E6 B9 96 20 20 
字节数组转换成的对象:
{"id":10001,"name":"张三    ","sex":0,"age":18,"height":170,"weight":50,"monthSalary":13,"address":"浙江杭州西湖  "}

 

 

参考文章:

https://www.cnblogs.com/Dotnet9-com/p/17981055

posted @ 2024-01-23 17:59  追极  阅读(51)  评论(0编辑  收藏  举报