package com.shinemo.mpush.tools;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;

/**
 * Created by ohun on 2015/12/25.
 */
public final class IOUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(IOUtils.class);

    public static void close(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (Exception e) {
                LOGGER.error("close closeable ex", e);
            }
        }
    }

    public static byte[] compress(byte[] data) {
        ByteArrayOutputStream out = new ByteArrayOutputStream(data.length / 4);
        DeflaterOutputStream zipOut = new DeflaterOutputStream(out);
        try {
            zipOut.write(data);
            zipOut.finish();
            zipOut.close();
        } catch (IOException e) {
            LOGGER.error("compress ex", e);
            return Constants.EMPTY_BYTES;
        } finally {
            close(zipOut);
        }
        return out.toByteArray();
    }

    public static byte[] uncompress(byte[] data) {
        InflaterInputStream zipIn = new InflaterInputStream(new ByteArrayInputStream(data));
        ByteArrayOutputStream out = new ByteArrayOutputStream(data.length * 4);
        byte[] buffer = new byte[1024];
        int length;
        try {
            while ((length = zipIn.read(buffer)) != -1) {
                out.write(buffer, 0, length);
            }
        } catch (IOException e) {
            LOGGER.error("uncompress ex", e);
            return Constants.EMPTY_BYTES;
        } finally {
            close(zipIn);
        }
        return out.toByteArray();
    }
}

@@ -754,32 +754,83 @@
 (byte%5B%5D data) %7B%0A
+    %09%0A    %09Profiler.enter(%22start compress%22);%0A    %09%0A
         ByteArra
@@ -889,24 +889,24 @@
 ength / 4);%0A
-
         Defl
@@ -1228,24 +1228,56 @@
 se(zipOut);%0A
+            Profiler.release();%0A
         %7D%0A  
@@ -1362,24 +1362,65 @@
 e%5B%5D data) %7B%0A
+    %09Profiler.enter(%22start uncompress%22);%0A
         Infl
@@ -1906,32 +1906,32 @@
     %7D finally %7B%0A
-
             clos
@@ -1940,16 +1940,48 @@
 zipIn);%0A
+            Profiler.release();%0A
         

add press 埋点