xieshengjie 4 سال پیش
والد
کامیت
a76a20c9bb

+ 6 - 6
common/src/main/java/com/gyee/ygys/protocol/BitMapGroup.java

@@ -3,7 +3,6 @@ package com.gyee.ygys.protocol;
 import com.gyee.ygys.utils.BytesUtil;
 import lombok.Data;
 
-import java.io.ByteArrayOutputStream;
 import java.util.BitSet;
 
 /**
@@ -28,9 +27,10 @@ public class BitMapGroup {
         byte[] result = new byte[len];
         int offset = 0;
 
-//        byte[] giBytes = BytesUtil.short2Byte(groupId);
-//        System.arraycopy(giBytes,0,result,offset,2);
-//        offset += 2;
+        byte[] giBytes = BytesUtil.short2Byte(groupIndex);
+        System.arraycopy(giBytes,0,result,offset,2);
+        offset += 2;
+
 
         byte[] glBytes = BytesUtil.int2Byte(groupLength);
         System.arraycopy(glBytes,0, result, offset, 4);
@@ -39,8 +39,8 @@ public class BitMapGroup {
         byte[] bmlBytes = BytesUtil.int2Byte(bitMapLength);
         System.arraycopy(bmlBytes,0,result,offset,4);
         offset += 4;
-
-        System.arraycopy(bitMapL2.toByteArray(), 0, result, offset, bitMapLength);
+        byte[] bytes = BytesUtil.bitSet2ByteArray(bitMapL2);
+        System.arraycopy(BytesUtil.bitSet2ByteArray(bitMapL2), 0, result, offset, bitMapLength);
         offset += bitMapLength;
 
         System.arraycopy(data, 0, result, offset, data.length);

+ 2 - 3
common/src/main/java/com/gyee/ygys/protocol/BitMapMessage.java

@@ -8,7 +8,6 @@ import java.io.DataOutputStream;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.BitSet;
-import java.util.HashMap;
 
 /**
  * 描述:实现基于两级BitMap的TCP消息格式, 适用于带宽资源紧缺的场景,例如,4M带宽的电力专线网络传输10万点秒级传感器数据
@@ -50,8 +49,8 @@ public class BitMapMessage {
         DataOutputStream out = new DataOutputStream(buf);
         out.write(msgHead);
         out.writeByte(version);
-        out.write(msgLength);
-        out.write(bitMapL1.toByteArray());
+        out.writeInt(msgLength);
+        out.write(BytesUtil.bitSet2ByteArray(bitMapL1));
         out.writeByte(reserve);
         for(BitMapGroup bmg : groups) {
             out.write(bmg.toBytes());

+ 11 - 7
common/src/main/java/com/gyee/ygys/protocol/BitMapMessageParser.java

@@ -21,13 +21,14 @@ public class BitMapMessageParser {
 
         BitMapGroup bmg = new BitMapGroup();
         int offset = 0;
-//        bmg.setGroupId(BytesUtil.getShort(array, offset));
-//        offset += 2;
-
-        bmg.setGroupLength(BytesUtil.getInt32(array,offset));
+        bmg.setGroupIndex(BytesUtil.getShort(array, offset));
+        offset += 2;
+        byte[] tempByte = new byte[4];
+        System.arraycopy(array,offset,tempByte,0,4);
+        bmg.setGroupLength(BytesUtil.byte2Int(tempByte));
         offset += 4;
-
-        bmg.setBitMapLength(BytesUtil.getInt32(array, offset));
+        System.arraycopy(array,offset,tempByte,0,4);
+        bmg.setBitMapLength(BytesUtil.byte2Int(tempByte));
         offset += 4;
 
         bmg.setBitMapL2(BytesUtil.getBitSet(array,offset, bmg.getBitMapLength()));
@@ -71,7 +72,10 @@ public class BitMapMessageParser {
         offset += 1; //越过保留位
         ArrayList<BitMapGroup> groups = new ArrayList<>();
         while(offset < array.length-3) {
-            int groupLength = BytesUtil.getInt32(array,offset);
+            //int groupLength = BytesUtil.getInt32(array,offset+2);
+            byte[] groupLengthByte = new byte[4];
+            System.arraycopy(array,offset+2,groupLengthByte,0,4);
+            int groupLength = BytesUtil.byte2Int(groupLengthByte);
             byte[] groupBytes = BytesUtil.getSubBytes(array, offset, groupLength);
             BitMapGroup bmg = parserBitMapGroup(groupBytes);
             groups.add(bmg);

+ 33 - 1
common/src/main/java/com/gyee/ygys/utils/BytesUtil.java

@@ -23,6 +23,7 @@ public class BytesUtil {
         return value;
     }
 
+
     public static long getLong(byte[] bytes, int offset)
     {
         long num = 0;
@@ -38,7 +39,8 @@ public class BytesUtil {
     {
         byte[] buf = new byte[length];
         System.arraycopy(bytes, offset,buf,0,length);
-        return BitSet.valueOf(buf);
+        //return BitSet.valueOf(buf);
+        return byteArray2BitSet(buf);
     }
 
     public static byte[] getSubBytes(byte[] bytes, int offset, int length)
@@ -219,4 +221,34 @@ public class BytesUtil {
     }
 
 
+    /**
+     * BitSet转byte[]
+     * @param bitSet
+     * @return
+     */
+    public static byte[] bitSet2ByteArray(BitSet bitSet) {
+        byte[] bytes = new byte[bitSet.size() / 8];
+        for (int i = 0; i < bitSet.size(); i++) {
+            int index = i / 8;
+            int offset = 7 - i % 8;
+            bytes[index] |= (bitSet.get(i) ? 1 : 0) << offset;
+        }
+        return bytes;
+    }
+    /**
+     * byte[]转BitSet
+     * @param bytes
+     * @return
+     */
+    public static BitSet byteArray2BitSet(byte[] bytes) {
+        BitSet bitSet = new BitSet(bytes.length * 8);
+        int index = 0;
+        for (int i = 0; i < bytes.length; i++) {
+            for (int j = 7; j >= 0; j--) {
+                bitSet.set(index++, (bytes[i] & (1 << j)) >> j == 1 ? true
+                        : false);
+            }
+        }
+        return bitSet;
+    }
 }