Example usage for org.apache.http.util ByteArrayBuffer byteAt

List of usage examples for org.apache.http.util ByteArrayBuffer byteAt

Introduction

In this page you can find the example usage for org.apache.http.util ByteArrayBuffer byteAt.

Prototype

public int byteAt(int i) 

Source Link

Usage

From source file:org.zywx.wbpalmstar.engine.eservice.EServiceTest.java

private static byte[] toByteArray(HttpEntity entity) throws Exception {
    if (entity == null) {
        throw new Exception("HTTP entity may not be null");
    }/*w ww . ja v a  2s . c  o m*/
    InputStream instream = entity.getContent();
    if (instream == null) {
        return new byte[] {};
    }
    long len = entity.getContentLength();
    if (len > Integer.MAX_VALUE) {
        throw new Exception("HTTP entity too large to be buffered in memory");
    }
    Header contentEncoding = entity.getContentEncoding();
    boolean gzip = false;
    if (null != contentEncoding) {
        if ("gzip".equalsIgnoreCase(contentEncoding.getValue())) {
            instream = new GZIPInputStream(instream, 2048);
            gzip = true;
        }
    }
    ByteArrayBuffer buffer = new ByteArrayBuffer(1024 * 8);
    // \&:38, \n:10, \r:13, \':39, \":34, \\:92
    try {
        if (gzip) {
            int lenth = 0;
            while (lenth != -1) {
                byte[] buf = new byte[2048];
                try {
                    lenth = instream.read(buf, 0, buf.length);
                    if (lenth != -1) {
                        buffer.append(buf, 0, lenth);
                    }
                } catch (EOFException e) {
                    int tl = buf.length;
                    int surpl;
                    for (int k = 0; k < tl; ++k) {
                        surpl = buf[k];
                        if (surpl != 0) {
                            buffer.append(surpl);
                        }
                    }
                    lenth = -1;
                }
            }
            int bl = buffer.length();
            ByteArrayBuffer temBuffer = new ByteArrayBuffer((int) (bl * 1.4));
            for (int j = 0; j < bl; ++j) {
                int cc = buffer.byteAt(j);
                //               if (cc == 34 || cc == 39 || cc == 92 || cc == 10
                //                     || cc == 13 || cc == 38) {
                //                  temBuffer.append('\\');
                //               }
                temBuffer.append(cc);
            }
            buffer = temBuffer;
        } else {
            int c;
            while ((c = instream.read()) != -1) {
                //               if (c == 34 || c == 39 || c == 92 || c == 10 || c == 13
                //                     || c == 38) {
                //                  buffer.append('\\');
                //               }
                buffer.append(c);
            }
        }
    } catch (Exception e) {
        instream.close();
    } finally {
        instream.close();
    }
    return buffer.toByteArray();
}