Search in sources :

Example 1 with VoidInputStream

use of org.nutz.lang.stream.VoidInputStream in project nutz by nutzam.

the class Sender method createResponse.

protected Response createResponse(Map<String, String> reHeaders) throws IOException {
    Response rep = null;
    if (reHeaders != null) {
        rep = new Response(conn, reHeaders);
        if (rep.isOK()) {
            InputStream is1 = conn.getInputStream();
            InputStream is2 = null;
            String encoding = conn.getContentEncoding();
            // 如果采用了压缩,则需要处理否则都是乱码
            if (encoding != null && encoding.contains("gzip")) {
                is2 = new GZIPInputStream(is1);
            } else if (encoding != null && encoding.contains("deflate")) {
                is2 = new InflaterInputStream(is1, new Inflater(true));
            } else {
                is2 = is1;
            }
            BufferedInputStream is = new BufferedInputStream(is2);
            rep.setStream(is);
        } else {
            try {
                rep.setStream(conn.getInputStream());
            } catch (IOException e) {
                try {
                    rep.setStream(conn.getErrorStream());
                } catch (Exception e1) {
                    rep.setStream(new VoidInputStream());
                }
            }
        }
    }
    if (this.interceptor != null)
        this.interceptor.afterResponse(request, conn, rep);
    return rep;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) VoidInputStream(org.nutz.lang.stream.VoidInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) VoidInputStream(org.nutz.lang.stream.VoidInputStream) Inflater(java.util.zip.Inflater) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 Inflater (java.util.zip.Inflater)1 InflaterInputStream (java.util.zip.InflaterInputStream)1 VoidInputStream (org.nutz.lang.stream.VoidInputStream)1