Android HttpResponse Read readBody(HttpResponse resp, ByteBuffer target)

Here you can find the source of readBody(HttpResponse resp, ByteBuffer target)

Description

read Body

License

Open Source License

Declaration

public static boolean readBody(HttpResponse resp, ByteBuffer target)
            throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

public class Main {
    public static boolean readBody(HttpResponse resp, ByteBuffer target)
            throws IOException {
        final HttpEntity entity = resp.getEntity();
        if (entity == null) {
            throw new IllegalArgumentException(
                    "HTTP entity may not be null");
        }//  w w w. ja  v  a  2s.c  o m
        final InputStream is = entity.getContent();
        if (is == null) {
            return false;
        }

        if (entity.getContentLength() > Integer.MAX_VALUE) {
            throw new IllegalArgumentException(
                    "HTTP entity too large to be buffered in memory");
        } else if (target.remaining() < entity.getContentLength()) {
            throw new IOException(String.format(
                    "allocated buffer is too small (%d < %d)",
                    target.remaining(), entity.getContentLength()));
        }

        try {
            final byte[] tmp = new byte[8192];
            int l;
            while ((l = is.read(tmp)) != -1) {
                target.put(tmp, 0, l);
            }
            return true;
        } finally {
            is.close();
        }
    }
}

Related

  1. decodeJSONResponse(HttpResponse resp)
  2. handleJsonResponse(HttpResponse response)