データの圧縮と解凍
6573 ワード
package chen.test;
import chen.util.util;
public class Compress {
protected static int compress(byte[] src, byte[] dst, int len){
byte current_byte;
int dst_len = 0;
byte count = 0;
dst[dst_len++] = (byte) 0xaa;
for (int i = 0; i < len; i++) {
current_byte = src[i];
if(current_byte!=0){
if(count!=0){
dst[dst_len++] = 0;
dst[dst_len++] = count;
count = 0;
}
dst[dst_len++] = current_byte;
}else {
count++;
if(count==255){
dst[dst_len++] = 0;
dst[dst_len++] = (byte) 255;
count = 0;
}
}
if(dst_len>len) return 0;
}
if(count!=0){
dst[dst_len++] = 0;
dst[dst_len++] = count;
count = 0;
}
return dst_len;
}
protected static int decompress(byte[] src ,byte[] dst ,int len) {
int dst_len = 0;
int current_byte = 0;
int i = 0;
if (src[i++]!=(byte)0xaa) {
return 0;
}
while (i < len) {
current_byte = util.byteArrayToInt(new byte[]{src[i]});
if (current_byte!=0) {
dst[dst_len++] = (byte) current_byte;
i++;
}else {
i++;
current_byte =util.byteArrayToInt(new byte[]{src[i]});
for (int j = 0; j < current_byte; j++) {
dst[dst_len++] = 0;
}
i++;
}
}
return dst_len;
}
public static void main(String[] args) {
String str = "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000010000000000000000000000004000000100000000000000000000000040000001000000000000000000000000400000010000000000000000000000004000000100000000000000000000000040000001000000000000000000000000400000010000000000000000000000002000000100000000000000000800000020000001000000000000000008000000200000020000000000000000080000002000000200000000000000000800000020000002000000000000000008000000200000020000000000000000087FFFFFFFFFFFFFFFFF8000000000000B800000200000020000000000000000080000002000000200000000000000000800000020000002000000000000000008000000200000020000000000000000080000002000000200000000000000000800000020000002000000000000000004000000200000040000000000000000040000002000000400000000000000000400000020000004000000000000000004000000200000040000000000000000040000002000000400000000000000000400000020000004000000000000000004000000400000040000000000007FE004000FFFFFFFFFFFFFE000000000001FFFFFF00040000008001FF8000000000002000000400000080000060000000000020000004000000800000000000000000200000040000010000000000000000002000000800000100000000000000000020000008000001000000000000000000200000080000010000000000000000002000000800000100000000000000000010000010000001000000000000000000100000100000020000000000000000001000001000000200000000000000000010000010000002000000000000000000100000100000020000000000000000001000FFFFFF000200000000000000007FFFFF001000FFFFFFF000000000003F8010000010000002000000000000000000080000100000020000000000000000000800001000000200000000000000000008000010000002000000000000000000080000100000020000000000000000000800001000000200000000000000000000000010000002000000000000000000000000100000020000000000000000000000001000000200000000000000000000000010000002000000000000000000000000100000020000000000000000000000001000000200000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
str = "0000000000000000000000000000000000000000000000003F000000000000000000000000000000C0E000000000000000000000000000010018000000000000000000000000000100060000000000000000000000000001000100000C0000000000000000000001000080000C0000000000000000000001000060000C00000000000000000000020000100014000000000000000000000200000C00140000000000000000000002000003001400000000003C0000000002000000802240000000000780000000020000004022C00000000001600000000200000020238000000000009800000002000000103F0000000000006700000004000000086300000000000010E000000400000005A5000000000000081C0000080000001E290000000000000403800010000000612880000000C000020060002000000180D0800000000000020018004000000600504000000000000100060080000038006040000000000000800181000000C00070400000000000004000620000030000502000000000000040001C00007C0000482000000000000040001C0007800000C42000000000000040002300780000014410000000000000200040FF80000001021000000000000020008FA000000002011000000000000020010C18000000020088000000000000C006020600000004004800000007FFC10008018100000008002800000003003E0010004080000008002400000000C000006000304000000800140000000020000180000820000010000C000000001C000200000418000010000A000000000300040000030600002000060000000000C7980000008100002000030000000000307000000080800040000300000000000C0000000040000040000180000000000380000000400000800000800000000000700000004000008000004000000000000E0000004000010000006000000000000180000080000100000020000000000000600001000001000000100000000000001F800100000200000018000000000000007C06000002000000180000000000000003F80000020000000C0000000000000000000000040000000C00000000000000000000000000000006000000000000000000000000000000050000000000000000000000000000000300000000000000000000000000000002800000000000000000000000000000018000000000000000000000000000000180000000000000000000000000000000C00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
byte[] src = util.HexStringToByteArray(str);
byte[] temp = new byte[2048];
int dst_len = compress(src, temp, src.length);//
byte[] dst = new byte[dst_len];
System.arraycopy(temp, 0, dst, 0, dst_len);
System.out.println("len: "+dst_len+"===:"+util.byteArray2Hex(dst));
dst_len = decompress(dst, temp, dst_len);//
dst = new byte[dst_len];
System.arraycopy(temp, 0, dst, 0, dst_len);
System.out.println("===:"+util.byteArray2Hex(dst));
}
}
上は直接データの圧縮と解凍であり、ハードウェアデータの伝送に用いられる場合、データ伝送の長さ制限に達し、データ伝送の正確性を保証する.