Example usage for org.apache.http.entity ByteArrayEntity ByteArrayEntity

List of usage examples for org.apache.http.entity ByteArrayEntity ByteArrayEntity

Introduction

In this page you can find the example usage for org.apache.http.entity ByteArrayEntity ByteArrayEntity.

Prototype

public ByteArrayEntity(byte[] bArr, int i, int i2, ContentType contentType) 

Source Link

Usage

From source file:org.elasticsearch.test.rest.yaml.ClientYamlTestExecutionContext.java

private HttpEntity createEntity(List<Map<String, Object>> bodies, Map<String, String> headers)
        throws IOException {
    if (bodies.isEmpty()) {
        return null;
    }/*w  w w.  j  a  va  2  s .c om*/
    if (bodies.size() == 1) {
        XContentType xContentType = getContentType(headers, XContentType.values());
        BytesRef bytesRef = bodyAsBytesRef(bodies.get(0), xContentType);
        return new ByteArrayEntity(bytesRef.bytes, bytesRef.offset, bytesRef.length,
                ContentType.create(xContentType.mediaTypeWithoutParameters(), StandardCharsets.UTF_8));
    } else {
        XContentType xContentType = getContentType(headers, STREAMING_CONTENT_TYPES);
        List<BytesRef> bytesRefList = new ArrayList<>();
        int totalBytesLength = 0;
        for (Map<String, Object> body : bodies) {
            BytesRef bytesRef = bodyAsBytesRef(body, xContentType);
            bytesRefList.add(bytesRef);
            totalBytesLength += bytesRef.length - bytesRef.offset + 1;
        }
        byte[] bytes = new byte[totalBytesLength];
        int position = 0;
        for (BytesRef bytesRef : bytesRefList) {
            for (int i = bytesRef.offset; i < bytesRef.length; i++) {
                bytes[position++] = bytesRef.bytes[i];
            }
            bytes[position++] = xContentType.xContent().streamSeparator();
        }
        return new ByteArrayEntity(bytes,
                ContentType.create(xContentType.mediaTypeWithoutParameters(), StandardCharsets.UTF_8));
    }
}