whycxzp
2024-07-19 339df2d21fc3c2784300db90eeb2299e0f89dc84
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.whyc.util.tool;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
 
import android.util.Log;
 
public class StreamTool {
    private static final String TAG = "StreamTool";
 
    public static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();
        outStream.close();
        inStream.close();
        return data;
    }
 
    public static String getUnzipStream(InputStream is, String content_encoding, String charset) {
        String resp_content = "";
        GZIPInputStream gzin = null;
        if (content_encoding != null && !content_encoding.equals("")) {
            if (content_encoding.contains("gzip")) {
                try {
                    Log.d(TAG, "content_encoding=" + content_encoding);
                    gzin = new GZIPInputStream(is);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        try {
            if (gzin == null) {
                resp_content = new String(readInputStream(is), charset);
            } else {
                resp_content = new String(readInputStream(gzin), charset);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resp_content;
    }
 
}