/** * Returns a {@link String} view of the data. */ public String string() { if (text == null) { if (!bytes.hasArray()) { bytes = bytes.toBytesArray(); } text = new String(bytes.array(), bytes.arrayOffset(), bytes.length(), StandardCharsets.UTF_8); } return text; }
public static int bytesHashCode(BytesReference a) { if (a.hasArray()) { return hashCode(a.array(), a.arrayOffset(), a.length()); } else { return slowHashCode(a); } }
public static StreamInput wrap(BytesReference reference) { if (reference.hasArray() == false) { reference = reference.toBytesArray(); } return wrap(reference.array(), reference.arrayOffset(), reference.length()); }
@Override public XContentParser createParser(BytesReference bytes) throws IOException { if (bytes.hasArray()) { return createParser(bytes.array(), bytes.arrayOffset(), bytes.length()); } return createParser(bytes.streamInput()); }
@Override public XContentParser createParser(BytesReference bytes) throws IOException { if (bytes.hasArray()) { return createParser(bytes.array(), bytes.arrayOffset(), bytes.length()); } return createParser(bytes.streamInput()); }
/** * Writes the binary content of the given BytesReference * Use {@link org.elasticsearch.common.xcontent.XContentParser#binaryValue()} to read the value back */ public XContentBuilder field(String name, BytesReference value) throws IOException { field(name); if (!value.hasArray()) { value = value.toBytesArray(); } generator.writeBinary(value.array(), value.arrayOffset(), value.length()); return this; }
/** * Writes the binary content of the given BytesReference * Use {@link org.elasticsearch.common.xcontent.XContentParser#binaryValue()} to read the value back */ public XContentBuilder field(XContentBuilderString name, BytesReference value) throws IOException { field(name); if (!value.hasArray()) { value = value.toBytesArray(); } generator.writeBinary(value.array(), value.arrayOffset(), value.length()); return this; }
@Override public XContentParser createParser(BytesReference bytes) throws IOException { if (bytes.hasArray()) { return createParser(bytes.array(), bytes.arrayOffset(), bytes.length()); } return createParser(bytes.streamInput()); }
@Override public XContentParser createParser(BytesReference bytes) throws IOException { if (bytes.hasArray()) { return createParser(bytes.array(), bytes.arrayOffset(), bytes.length()); } return createParser(bytes.streamInput()); }
/** * Writes the binary content of the given BytesReference * Use {@link org.elasticsearch.common.xcontent.XContentParser#binaryValue()} to read the value back */ public XContentBuilder value(BytesReference value) throws IOException { if (value == null) { return nullValue(); } if (!value.hasArray()) { value = value.toBytesArray(); } generator.writeBinary(value.array(), value.arrayOffset(), value.length()); return this; }
protected void copyRawValue(BytesReference content, XContent xContent) throws IOException { XContentParser parser = null; try { if (content.hasArray()) { parser = xContent.createParser(content.array(), content.arrayOffset(), content.length()); } else { parser = xContent.createParser(content.streamInput()); } copyCurrentStructure(parser); } finally { if (parser != null) { parser.close(); } } }
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(); } }
public XContentBuilder field(String name, Text value) throws IOException { field(name); if (value.hasBytes() && value.bytes().hasArray()) { generator.writeUTF8String(value.bytes().array(), value.bytes().arrayOffset(), value.bytes().length()); return this; } if (value.hasString()) { generator.writeString(value.string()); return this; } // TODO: TextBytesOptimization we can use a buffer here to convert it? maybe add a request to jackson to support InputStream as well? BytesArray bytesArray = value.bytes().toBytesArray(); generator.writeUTF8String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length()); return this; }
} else if (value instanceof BytesReference) { BytesReference bytes = (BytesReference) value; if (!bytes.hasArray()) { bytes = bytes.toBytesArray(); } else if (value instanceof Text) { Text text = (Text) value; if (text.hasBytes() && text.bytes().hasArray()) { generator.writeUTF8String(text.bytes().array(), text.bytes().arrayOffset(), text.bytes().length()); } else if (text.hasString()) {
public XContentBuilder field(XContentBuilderString name, Text value) throws IOException { field(name); if (value.hasBytes() && value.bytes().hasArray()) { generator.writeUTF8String(value.bytes().array(), value.bytes().arrayOffset(), value.bytes().length()); return this; } if (value.hasString()) { generator.writeString(value.string()); return this; } // TODO: TextBytesOptimization we can use a buffer here to convert it? maybe add a request to jackson to support InputStream as well? BytesArray bytesArray = value.bytes().toBytesArray(); generator.writeUTF8String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length()); return this; }
public XContentBuilder value(Text value) throws IOException { if (value == null) { return nullValue(); } if (value.hasBytes() && value.bytes().hasArray()) { generator.writeUTF8String(value.bytes().array(), value.bytes().arrayOffset(), value.bytes().length()); return this; } if (value.hasString()) { generator.writeString(value.string()); return this; } BytesArray bytesArray = value.bytes().toBytesArray(); generator.writeUTF8String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length()); return this; }
if (!content.hasArray()) { content = content.toBytesArray();
if (!source.hasArray()) { source = source.toBytesArray();