Java Code Examples for org.elasticsearch.common.bytes.BytesArray#arrayOffset()

The following examples show how to use org.elasticsearch.common.bytes.BytesArray#arrayOffset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XContentHelper.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public static String convertToJson(BytesReference bytes, boolean reformatJson, boolean prettyPrint) throws IOException {
    if (bytes.hasArray()) {
        return convertToJson(bytes.array(), bytes.arrayOffset(), bytes.length(), reformatJson, prettyPrint);
    }
    XContentType xContentType = XContentFactory.xContentType(bytes);
    if (xContentType == XContentType.JSON && !reformatJson) {
        BytesArray bytesArray = bytes.toBytesArray();
        return new String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length(), Charsets.UTF_8);
    }
    try (XContentParser parser = XContentFactory.xContent(xContentType).createParser(bytes.streamInput())) {
        parser.nextToken();
        XContentBuilder builder = XContentFactory.jsonBuilder();
        if (prettyPrint) {
            builder.prettyPrint();
        }
        builder.copyCurrentStructure(parser);
        return builder.string();
    }
}
 
Example 2
Source File: ElasticSearchHelper.java    From camunda-bpm-elasticsearch with Apache License 2.0 5 votes vote down vote up
public static String convertRequestToJson(ActionRequest request) throws IOException {
    BytesStreamOutput bytesStreamOutput = new BytesStreamOutput();
    request.writeTo(bytesStreamOutput);

    XContentBuilder builder = XContentFactory.jsonBuilder(bytesStreamOutput);
    builder.prettyPrint();

//    builder.startObject();
//    builder.endObject();
    BytesArray bytesArray = builder.bytes().toBytesArray();
    return new String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length());
  }
 
Example 3
Source File: XContentBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a string representation of the builder (only applicable for text based xcontent).
 */
public String string() throws IOException {
    close();
    BytesArray bytesArray = bytes().toBytesArray();
    return new String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length(), Charsets.UTF_8);
}
 
Example 4
Source File: XmlXContentBuilder.java    From elasticsearch-xml with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a string representation of the builder (only applicable for text based xcontent).
 */
public String string() throws IOException {
    close();
    BytesArray bytesArray = bytes().toBytesArray();
    return new String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length(), Charsets.UTF_8);
}