Android 数据压缩

public class CommonUtils {
    
    public static boolean isNotNull(String s){
        if(s==null || "".equals(s)) {
            return false;
        }
        return true;
    }
    
    public static boolean isNull(String s){
        return !isNotNull(s) || "null".equals(s);
    }

    public static byte[] compress(String s) throws IOException, DataFormatException{
        byte[] input = s.getBytes("UTF-8");

        // 创建一个压缩器,进行压缩处理
        Deflater compressor = new Deflater();
        compressor.setLevel(Deflater.BEST_COMPRESSION);

        compressor.setInput(input);
        compressor.finish();

        // 压缩的数据写入内存数组中
        ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

        byte[] buf = new byte[1024];
        int len=0;
        GZIPOutputStream out;
        while(!compressor.finished()) {
            len = compressor.deflate(buf);
            bos.write(buf, 0, len);
        }
        bos.close();

        return bos.toByteArray();
    }
    
    public static byte[] decompress(byte[] compressData) throws IOException, DataFormatException{

        // 创建解压缩器
        Inflater decompressor = new Inflater();
        decompressor.setInput(compressData);
        
        // 解压缩后的数据写入内存数组中
        ByteArrayOutputStream bos = new ByteArrayOutputStream(compressData.length);
        
        byte[] buf = new byte[1024];
        int len=0;
        while(!decompressor.finished()) {
            len = decompressor.inflate(buf);
            bos.write(buf, 0, len);
        }
        bos.close();
        
        return bos.toByteArray();
    }
    
    public static void main(String[] args)throws IOException {
          ByteArrayOutputStream bout = new ByteArrayOutputStream();
          DataOutputStream dout = new DataOutputStream(bout);
          
          String name = "{test:111,test1:222,test1:22312}";
           int age = 84;
           dout.writeUTF(name);
         dout.writeInt(age);
        
         byte[] buff = bout.toByteArray();
          ByteArrayInputStream bin = new ByteArrayInputStream(buff);
          DataInputStream dis = new DataInputStream(bin);
          String newName = dis.readUTF();
         int newAge = dis.readInt();
          System.out.println(newName+":"+newAge);
    }

    private final static double EARTH_RADIUS = 6378137.0;    
    @SuppressWarnings("unused")
    public static double gps2m(double lat_a, double lng_a, double lat_b, double lng_b) {  
       double radLat1 = (lat_a * Math.PI / 180.0);  
       double radLat2 = (lat_b * Math.PI / 180.0);  
       double a = radLat1 - radLat2;  
       double b = (lng_a - lng_b) * Math.PI / 180.0;  
       double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)  
              + Math.cos(radLat1) * Math.cos(radLat2)  
              * Math.pow(Math.sin(b / 2), 2)));  
       s = s * EARTH_RADIUS;  
       s = Math.round(s * 10000) / 10000;  
       return s;  
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值