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

The following examples show how to use org.elasticsearch.common.bytes.BytesReference#streamInput() . 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 crate with Apache License 2.0 6 votes vote down vote up
public static String convertToJson(BytesReference bytes, XContentType xContentType) throws IOException {
    Objects.requireNonNull(xContentType);
    if (xContentType == XContentType.JSON) {
        return bytes.utf8ToString();
    }
    // It is safe to use EMPTY here because this never uses namedObject
    try (InputStream stream = bytes.streamInput();
         XContentParser parser = XContentFactory.xContent(xContentType).createParser(
             NamedXContentRegistry.EMPTY,
             DeprecationHandler.THROW_UNSUPPORTED_OPERATION,
             stream)) {
        parser.nextToken();
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.copyCurrentStructure(parser);
        return Strings.toString(builder);
    }
}
 
Example 2
Source File: XContentHelper.java    From crate with Apache License 2.0 5 votes vote down vote up
private static InputStream getUncompressedInputStream(BytesReference bytes) throws IOException {
    Compressor compressor = CompressorFactory.compressor(bytes);
    if (compressor == null) {
        return bytes.streamInput();
    }
    InputStream compressedStreamInput = compressor.streamInput(bytes.streamInput());
    if (compressedStreamInput.markSupported()) {
        return compressedStreamInput;
    } else {
        return new BufferedInputStream(compressedStreamInput);
    }
}
 
Example 3
Source File: BlobStoreRepository.java    From crate with Apache License 2.0 4 votes vote down vote up
private void writeAtomic(final String blobName, final BytesReference bytesRef, boolean failIfAlreadyExists) throws IOException {
    try (InputStream stream = bytesRef.streamInput()) {
        blobContainer().writeBlobAtomic(blobName, stream, bytesRef.length(), failIfAlreadyExists);
    }
}
 
Example 4
Source File: TcpTransport.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * Validates the first N bytes of the message header and returns <code>false</code> if the message is
 * a ping message and has no payload ie. isn't a real user level message.
 *
 * @throws IllegalStateException    if the message is too short, less than the header or less that the header plus the message size
 * @throws HttpOnTransportException if the message has no valid header and appears to be an HTTP message
 * @throws IllegalArgumentException if the message is greater that the maximum allowed frame size. This is dependent on the available
 *                                  memory.
 */
public static boolean validateMessageHeader(BytesReference buffer) throws IOException {
    final int sizeHeaderLength = TcpHeader.MARKER_BYTES_SIZE + TcpHeader.MESSAGE_LENGTH_SIZE;
    if (buffer.length() < sizeHeaderLength) {
        throw new IllegalStateException("message size must be >= to the header size");
    }
    int offset = 0;
    if (buffer.get(offset) != 'E' || buffer.get(offset + 1) != 'S') {
        // special handling for what is probably HTTP
        if (bufferStartsWith(buffer, offset, "GET ") ||
            bufferStartsWith(buffer, offset, "POST ") ||
            bufferStartsWith(buffer, offset, "PUT ") ||
            bufferStartsWith(buffer, offset, "HEAD ") ||
            bufferStartsWith(buffer, offset, "DELETE ") ||
            bufferStartsWith(buffer, offset, "OPTIONS ") ||
            bufferStartsWith(buffer, offset, "PATCH ") ||
            bufferStartsWith(buffer, offset, "TRACE ")) {

            throw new HttpOnTransportException("This is not an HTTP port");
        }

        // we have 6 readable bytes, show 4 (should be enough)
        throw new StreamCorruptedException("invalid internal transport message format, got ("
            + Integer.toHexString(buffer.get(offset) & 0xFF) + ","
            + Integer.toHexString(buffer.get(offset + 1) & 0xFF) + ","
            + Integer.toHexString(buffer.get(offset + 2) & 0xFF) + ","
            + Integer.toHexString(buffer.get(offset + 3) & 0xFF) + ")");
    }

    final int dataLen;
    try (StreamInput input = buffer.streamInput()) {
        input.skip(TcpHeader.MARKER_BYTES_SIZE);
        dataLen = input.readInt();
        if (dataLen == PING_DATA_SIZE) {
            // discard the messages we read and continue, this is achieved by skipping the bytes
            // and returning null
            return false;
        }
    }

    if (dataLen <= 0) {
        throw new StreamCorruptedException("invalid data length: " + dataLen);
    }
    // safety against too large frames being sent
    if (dataLen > NINETY_PER_HEAP_SIZE) {
        throw new IllegalArgumentException("transport content length received [" + new ByteSizeValue(dataLen) + "] exceeded ["
            + new ByteSizeValue(NINETY_PER_HEAP_SIZE) + "]");
    }

    if (buffer.length() < dataLen + sizeHeaderLength) {
        throw new IllegalStateException("buffer must be >= to the message size but wasn't");
    }
    return true;
}