Java Code Examples for org.elasticsearch.common.bytes.BytesReference#toBytesArray()
The following examples show how to use
org.elasticsearch.common.bytes.BytesReference#toBytesArray() .
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 |
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: SourceToParse.java From Elasticsearch with Apache License 2.0 | 5 votes |
private SourceToParse(Origin origin, BytesReference source) { this.origin = origin; // we always convert back to byte array, since we store it and Field only supports bytes.. // so, we might as well do it here, and improve the performance of working with direct byte arrays this.source = source.toBytesArray(); this.parser = null; }
Example 3
Source File: XContentBuilder.java From Elasticsearch with Apache License 2.0 | 5 votes |
/** * 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; }
Example 4
Source File: XContentBuilder.java From Elasticsearch with Apache License 2.0 | 5 votes |
/** * 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; }
Example 5
Source File: XmlXContentBuilder.java From elasticsearch-xml with Apache License 2.0 | 5 votes |
public XmlXContentBuilder 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; }
Example 6
Source File: XmlXContentBuilder.java From elasticsearch-xml with Apache License 2.0 | 5 votes |
public XmlXContentBuilder 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; }
Example 7
Source File: BlobRecoveryTarget.java From Elasticsearch with Apache License 2.0 | 4 votes |
@Override public void messageReceived(BlobRecoveryChunkRequest request, TransportChannel channel) throws Exception { BlobRecoveryStatus onGoingRecovery = onGoingRecoveries.get(request.recoveryId()); if (onGoingRecovery == null) { // shard is getting closed on us throw new IllegalBlobRecoveryStateException("Could not retrieve onGoingRecoveryStatus"); } BlobRecoveryTransferStatus transferStatus = onGoingRecovery.onGoingTransfers().get(request.transferId()); BlobShard shard = onGoingRecovery.blobShard; if (onGoingRecovery.canceled()) { onGoingRecovery.sentCanceledToSource(); throw new IndexShardClosedException(onGoingRecovery.shardId()); } if (transferStatus == null) { throw new IndexShardClosedException(onGoingRecovery.shardId()); } BytesReference content = request.content(); if (!content.hasArray()) { content = content.toBytesArray(); } transferStatus.outputStream().write( content.array(), content.arrayOffset(), content.length() ); if (request.isLast()) { transferStatus.outputStream().close(); File source = new File(shard.blobContainer().getBaseDirectory(), transferStatus.sourcePath() ); File target = new File(shard.blobContainer().getBaseDirectory(), transferStatus.targetPath() ); if (target.exists()) { logger.info("target file {} exists already.", target.getName()); // this might happen on bad timing while recovering/relocating. // noop } else { if (!source.renameTo(target)) { throw new BlobWriteException(target.getName(), target.length(), null); } } onGoingRecovery.onGoingTransfers().remove(request.transferId()); } channel.sendResponse(TransportResponse.Empty.INSTANCE); }
Example 8
Source File: BlobRecoveryTarget.java From Elasticsearch with Apache License 2.0 | 4 votes |
@Override public void messageReceived(BlobRecoveryStartTransferRequest request, TransportChannel channel) throws Exception { BlobRecoveryStatus status = onGoingRecoveries.get(request.recoveryId()); logger.debug("received BlobRecoveryStartTransferRequest for file {} with size {}", request.path(), request.size()); if (status == null) { throw new IllegalBlobRecoveryStateException("Could not retrieve onGoingRecoveryStatus"); } if (status.canceled()) { throw new IndexShardClosedException(status.shardId()); } BlobShard shard = status.blobShard; String tmpPath = request.path() + "." + request.transferId(); FileOutputStream outputStream = new FileOutputStream( new File(shard.blobContainer().getBaseDirectory(), tmpPath) ); BytesReference content = request.content(); if (!content.hasArray()) { content = content.toBytesArray(); } outputStream.write(content.array(), content.arrayOffset(), content.length()); if (request.size() == request.content().length()) { // start request contains the whole file. outputStream.close(); File source = new File(shard.blobContainer().getBaseDirectory(), tmpPath); File target = new File(shard.blobContainer().getBaseDirectory(), request.path()); if (!target.exists()) { if (!source.renameTo(target)) { throw new IllegalBlobRecoveryStateException( "couldn't rename file to " + request.path() ); } } } else { BlobRecoveryTransferStatus transferStatus= new BlobRecoveryTransferStatus( request.transferId(), outputStream, tmpPath, request.path() ); status.onGoingTransfers().put(request.transferId(), transferStatus); } channel.sendResponse(TransportResponse.Empty.INSTANCE); }
Example 9
Source File: StreamInput.java From Elasticsearch with Apache License 2.0 | 4 votes |
public static StreamInput wrap(BytesReference reference) { if (reference.hasArray() == false) { reference = reference.toBytesArray(); } return wrap(reference.array(), reference.arrayOffset(), reference.length()); }