模拟客户端与服务端大数据二进制压缩传输

最近项目和其他项目信息交互,因数据量比较大造成效率比较低,我就研究了下数据压缩传输,以下为模拟代码,希望有用的人可以借鉴一下,我写的并非接口调用,为了节约代码,就把原理写了下来,希望你们可以看的明白,如果不明白,可以留言,代码如下:

package com.test.compress;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

import org.junit.Assert;

public final class CompressionUtil {  
  
    private static final int BUFFER_SIZE = 4 * 1024;  
  
    public static byte[] compress(byte[] data, Level level) throws IOException {  
  
        Assert.assertNotNull(data);  
        Assert.assertNotNull(level);  
  
        Deflater deflater = new Deflater();  
        // set compression level  
        deflater.setLevel(level.getLevel());  
        deflater.setInput(data);  
  
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);  
  
        deflater.finish();  
        byte[] buffer = new byte[BUFFER_SIZE];  
        while (!deflater.finished()) {  
            int count = deflater.deflate(buffer); // returns the generated  
                                                  // code... index  
            outputStream.write(buffer, 0, count);  
        }  
        byte[] output = outputStream.toByteArray();  
        outputStream.close();  
        return output;  
    }  
  
    public static byte[] decompress(byte[] data) throws IOException, DataFormatException {  
  
        Assert.assertNotNull(data);  
  
        Inflater inflater = new Inflater();  
        inflater.setInput(data);  
  
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);  
        byte[] buffer = new byte[BUFFER_SIZE];  
        while (!inflater.finished()) {  
            int count = inflater.inflate(buffer);  
            outputStream.write(buffer, 0, count);  
        }  
        byte[] output = outputStream.toByteArray();  
        outputStream.close();  
        return output;  
    }  
  
    /** 
     * Compression level 
     */  
    public static enum Level {  
  
        /** 
         * Compression level for no compression. 
         */  
        NO_COMPRESSION(0),  
  
        /** 
         * Compression level for fastest compression. 
         */  
        BEST_SPEED(1),  
  
        /** 
         * Compression level for best compression. 
         */  
        BEST_COMPRESSION(9),  
  
        /** 
         * Default compression level. 
         */  
        DEFAULT_COMPRESSION(-1);  
  
        private int level;  
  
        Level(int level) {  
            this.level = level;  
        }  
        public int getLevel() {  
            return level;  
        }  
    } 
}


package com.test.json;


import java.util.ArrayList;
import java.util.List;

import com.test.compress.CompressionUtil;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

public class TestCompress {
public static JSONArray ListToJSONArray(List list) {
    return JSONArray.fromObject(list);
    }

@SuppressWarnings("unchecked")
public static List JSONArrayToList(JSONArray jsonArray) {
    //参数1为要转换的JSONArray数据,参数2为要转换的目标数据,即List盛装的数据
    return JSONArray.toList(jsonArray, new Student(), new JsonConfig());
    }

public static String toString(String[] strs) {
    String str = "[";
    for(String s : strs) {
    str += '"' + s + '"' + ',';
    }
    return str.substring(0, str.length()-1) + "]";
    }

public static void main(String[] args) throws Exception {
/*---------------------------- 客户端代码 -----------------------------------*/
//创建JavaBean
Student student = null;

//创建测试用的List
List list = new ArrayList();
for(int i = 0; i < 10000; i++) {
student = new Student();
student.setId(i);
student.setName("张" + i);
student.setAge(i);
student.setSex("男");
student.setHobby(new String[]{"篮球","上网","跑步","游戏"});
list.add(student);
}

//List 转 JSONArray
JSONArray jsonArray = ListToJSONArray(list);

//二进制压缩
byte[] output = CompressionUtil.compress(jsonArray.toString().getBytes(), CompressionUtil.Level.BEST_COMPRESSION);
// 接口调用传输数据 参数为 output

/*---------------------------- 服务端代码 -----------------------------------*/
// 接收output参数 为客户端传入数据
//二进制解压
byte[] jsonMessage = CompressionUtil.decompress(output);

//String 转 JSONArray
JSONArray jsonArray2 = JSONArray.fromObject(new String(jsonMessage));

//JSONArray 转 List
List studentList = JSONArrayToList(jsonArray2); 

//遍历信息
for(Student stu : studentList) {
System.out.println("-----------------------------");
System.out.println("ID:" + stu.getId());
System.out.println("NAME:" + stu.getName());
System.out.println("SEX:" + stu.getSex());
System.out.println("AGE:" + stu.getAge());
System.out.println("HOBBY:" + toString(stu.getHobby()));
}
}
}

你可能感兴趣的:(模拟客户端与服务端大数据二进制压缩传输)