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. 工具類

/**
 * 對象轉位字節(整型實現按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;
           int startByteRemainBit = 8 - startByteUsedBit;
           //結束緩存位置(第幾個字節)
           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 << startByteRemainBit) - 1;
           valInt = lowerByte & mask;

           // 2. 讀取中間字節(讀整個字節)
           if(gapByteCount > 0) {
               for (int i = 0; i < gapByteCount; i++) {
                   int leftMove = startByteRemainBit + (i * 8);
                   byte b = buffer[startBufferIndex+(i+1)];
                   valInt |= (b << leftMove);
               }
           }

           // 3. 讀取結束字節(讀取低位)
           if(endBufferIndex > startBufferIndex) {
               byte b = buffer[endBufferIndex];
               int leftMove = startByteRemainBit + 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;
            int startByteRemainBit = 8 - startByteUsedBit;
            //結束緩存位置(第幾個字節)
            int endBufferIndex = (offset + size - 1) / 8;
            int endByteUseBit = ((offset + size - 1) % 8)+1;
            // 緩存間隔位置(緩存起止位置之間的間隔字節數)
            int gapByteCount = endBufferIndex - startBufferIndex - 1;

            // 1. 寫入起始字節(之前在低位可能已經存在值了,現在寫入的寫到高位去)
            int mask = (1 << startByteRemainBit) - 1;
            byte lowerByte = (byte) (valInt & mask);
            //取低startByteRemainBit位,則左移startByteUsedBit位,空出來的與原來的值或操作即可
            buffer[startBufferIndex] |= ((lowerByte & 0xFF) << startByteUsedBit);

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

            // 3. 寫結束字節(寫入低位即可)
            if(endBufferIndex > startBufferIndex) {
                int rightMove = startByteRemainBit + 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":36,"age":18,"height":170,"weight":50,"monthSalary":13,"address":"浙江杭州西湖  "}

 

參考文章:

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章