Search in sources :

Example 1 with PoolingByteArrayOutputStream

use of com.android.volley.toolbox.PoolingByteArrayOutputStream in project Douya by DreaminginCodeZH.

the class BasicNetwork method entityToBytes.

/** Reads the contents of HttpEntity into a byte[]. */
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
    PoolingByteArrayOutputStream bytes = new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
    byte[] buffer = null;
    try {
        InputStream in = entity.getContent();
        if (in == null) {
            throw new ServerError();
        }
        buffer = mPool.getBuf(1024);
        int count;
        while ((count = in.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        try {
            // Close the InputStream and release the resources by "consuming the content".
            entity.consumeContent();
        } catch (IOException e) {
            // This can happen if there was an exception above that left the entity in
            // an invalid state.
            VolleyLog.v("Error occured when calling consumingContent");
        }
        mPool.returnBuf(buffer);
        bytes.close();
    }
}
Also used : PoolingByteArrayOutputStream(com.android.volley.toolbox.PoolingByteArrayOutputStream) InputStream(java.io.InputStream) ServerError(com.android.volley.ServerError) IOException(java.io.IOException)

Aggregations

ServerError (com.android.volley.ServerError)1 PoolingByteArrayOutputStream (com.android.volley.toolbox.PoolingByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1