设为首页 加入收藏

TOP

Netty编解码技术和UDP实现(五)
2018-03-02 06:57:07 】 浏览:552
Tags:Netty 解码 技术 UDP 实现
tachment) {
        this.attachment = attachment;
    }
}


public class Resp implements Serializable {


    private static final long serialVersionUID = 1L;


    private String id;
    private String name;
    private String responseMessage;


    public String getId() {
        return id;
    }


    public void setId(String id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getResponseMessage() {
        return responseMessage;
    }


    public void setResponseMessage(String responseMessage) {
        this.responseMessage = responseMessage;
    }


}


工具类:


public class GzipUtils {


    public static byte[] gzip(byte[] data) throws Exception {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(bos);
        gzip.write(data);
        gzip.finish();
        gzip.close();
        byte[] ret = bos.toByteArray();
        bos.close();
        return ret;
    }


    public static byte[] ungzip(byte[] data) throws Exception {
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        GZIPInputStream gzip = new GZIPInputStream(bis);
        byte[] buf = new byte[1024];
        int num = -1;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((num = gzip.read(buf, 0, buf.length)) != -1) {
            bos.write(buf, 0, num);
        }
        gzip.close();
        bis.close();
        byte[] ret = bos.toByteArray();
        bos.flush();
        bos.close();
        return ret;
    }


    public static void main(String[] args) throws Exception {


        // 读取文件
        String readPath = System.getProperty("user.dir") + File.separatorChar + "sources" + File.separatorChar
                + "006.jpg";
        File file = new File(readPath);
        FileInputStream in = new FileInputStream(file);
        byte[] data = new byte[in.available()];
        in.read(data);
        in.close();


        System.out.println("文件原始大小:" + data.length);
        // 测试压缩


        byte[] ret1 = GzipUtils.gzip(data);
        System.out.println("压缩之后大小:" + ret1.length);


        byte[] ret2 = GzipUtils.ungzip(ret1);
        System.out.println("还原之后大小:&quo

首页 上一页 2 3 4 5 6 下一页 尾页 5/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C++ 重载运算符简单例子 下一篇基于注解的简单SSH保存用户小案例

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目