Java Code Examples for org.elasticsearch.common.bytes.BytesReference#hasArray()

The following examples show how to use org.elasticsearch.common.bytes.BytesReference#hasArray() . 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: YamlXContent.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public XContentParser createParser(BytesReference bytes) throws IOException {
    if (bytes.hasArray()) {
        return createParser(bytes.array(), bytes.arrayOffset(), bytes.length());
    }
    return createParser(bytes.streamInput());
}
 
Example 3
Source File: CborXContent.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public XContentParser createParser(BytesReference bytes) throws IOException {
    if (bytes.hasArray()) {
        return createParser(bytes.array(), bytes.arrayOffset(), bytes.length());
    }
    return createParser(bytes.streamInput());
}
 
Example 4
Source File: JsonXContentGenerator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
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();
        }
    }
}
 
Example 5
Source File: JsonXContent.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public XContentParser createParser(BytesReference bytes) throws IOException {
    if (bytes.hasArray()) {
        return createParser(bytes.array(), bytes.arrayOffset(), bytes.length());
    }
    return createParser(bytes.streamInput());
}
 
Example 6
Source File: XContentBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * 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 7
Source File: XContentBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * 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 8
Source File: SmileXContent.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public XContentParser createParser(BytesReference bytes) throws IOException {
    if (bytes.hasArray()) {
        return createParser(bytes.array(), bytes.arrayOffset(), bytes.length());
    }
    return createParser(bytes.streamInput());
}
 
Example 9
Source File: XmlXContent.java    From elasticsearch-xml with Apache License 2.0 5 votes vote down vote up
@Override
public XContentParser createParser(BytesReference bytes) throws IOException {
    if (bytes.hasArray()) {
        return createParser(bytes.array(), bytes.arrayOffset(), bytes.length());
    }
    return createParser(bytes.streamInput());
}
 
Example 10
Source File: XmlXContentBuilder.java    From elasticsearch-xml with Apache License 2.0 5 votes vote down vote up
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 11
Source File: XmlXContentBuilder.java    From elasticsearch-xml with Apache License 2.0 5 votes vote down vote up
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 12
Source File: BlobRecoveryTarget.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@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 13
Source File: BlobRecoveryTarget.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@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 14
Source File: StreamInput.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public static StreamInput wrap(BytesReference reference) {
    if (reference.hasArray() == false) {
        reference = reference.toBytesArray();
    }
    return wrap(reference.array(), reference.arrayOffset(), reference.length());
}