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

The following examples show how to use org.elasticsearch.common.bytes.BytesReference#get() . 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: XmlXContentFactory.java    From elasticsearch-xml with Apache License 2.0 6 votes vote down vote up
/**
 * Guesses the content type based on the provided bytes.
 */
public static XmlXContentType xContentType(BytesReference bytes) {
    int length = bytes.length() < GUESS_HEADER_LENGTH ? bytes.length() : GUESS_HEADER_LENGTH;
    if (length == 0) {
        return null;
    }
    byte first = bytes.get(0);
    if (first == '{') {
        return XmlXContentType.JSON;
    }
    if (length > 2 && first == SmileConstants.HEADER_BYTE_1 && bytes.get(1) == SmileConstants.HEADER_BYTE_2 && bytes.get(2) == SmileConstants.HEADER_BYTE_3) {
        return XmlXContentType.SMILE;
    }
    if (length > 2 && first == '-' && bytes.get(1) == '-' && bytes.get(2) == '-') {
        return XmlXContentType.YAML;
    }
    if (length > 2 && first == '<' && bytes.get(1) == '?' && bytes.get(2) == 'x') {
        return XmlXContentType.XML;
    }
    for (int i = 0; i < length; i++) {
        if (bytes.get(i) == '{') {
            return XmlXContentType.JSON;
        }
    }
    return null;
}
 
Example 2
Source File: DeflateCompressor.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isCompressed(BytesReference bytes) {
    if (bytes.length() < HEADER.length) {
        return false;
    }
    for (int i = 0; i < HEADER.length; ++i) {
        if (bytes.get(i) != HEADER[i]) {
            return false;
        }
    }
    return true;
}
 
Example 3
Source File: LZFCompressor.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isCompressed(BytesReference bytes) {
    return bytes.length() >= 3 &&
            bytes.get(0) == LZFChunk.BYTE_Z &&
            bytes.get(1) == LZFChunk.BYTE_V &&
            (bytes.get(2) == LZFChunk.BLOCK_TYPE_COMPRESSED || bytes.get(2) == LZFChunk.BLOCK_TYPE_NON_COMPRESSED);
}
 
Example 4
Source File: MultiPercolateRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private int findNextMarker(byte marker, int from, BytesReference data, int length) {
    for (int i = from; i < length; i++) {
        if (data.get(i) == marker) {
            return i;
        }
    }
    return -1;
}
 
Example 5
Source File: MultiSearchRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private int findNextMarker(byte marker, int from, BytesReference data, int length) {
    for (int i = from; i < length; i++) {
        if (data.get(i) == marker) {
            return i;
        }
    }
    return -1;
}
 
Example 6
Source File: BulkRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private int findNextMarker(byte marker, int from, BytesReference data, int length) {
    for (int i = from; i < length; i++) {
        if (data.get(i) == marker) {
            return i;
        }
    }
    return -1;
}
 
Example 7
Source File: IngestRequest.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
private int findNextMarker(byte marker, int from, BytesReference data, int length) {
    for (int i = from; i < length; i++) {
        if (data.get(i) == marker) {
            return i;
        }
    }
    return -1;
}
 
Example 8
Source File: TcpTransport.java    From crate with Apache License 2.0 5 votes vote down vote up
private static boolean bufferStartsWith(BytesReference buffer, int offset, String method) {
    char[] chars = method.toCharArray();
    for (int i = 0; i < chars.length; i++) {
        if (buffer.get(offset + i) != chars[i]) {
            return false;
        }
    }

    return true;
}
 
Example 9
Source File: CompressorFactory.java    From crate with Apache License 2.0 5 votes vote down vote up
/** true if the bytes were compressed with LZF: only used before elasticsearch 2.0 */
private static boolean isAncient(BytesReference bytes) {
    return bytes.length() >= 3 &&
           bytes.get(0) == 'Z' &&
           bytes.get(1) == 'V' &&
           (bytes.get(2) == 0 || bytes.get(2) == 1);
}
 
Example 10
Source File: DeflateCompressor.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isCompressed(BytesReference bytes) {
    if (bytes.length() < HEADER.length) {
        return false;
    }
    for (int i = 0; i < HEADER.length; ++i) {
        if (bytes.get(i) != HEADER[i]) {
            return false;
        }
    }
    return true;
}
 
Example 11
Source File: XContentFactory.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Guesses the content type based on the provided bytes.
 */
public static XContentType xContentType(BytesReference bytes) {
    int length = bytes.length();
    if (length == 0) {
        return null;
    }
    byte first = bytes.get(0);
    if (first == '{') {
        return XContentType.JSON;
    }
    if (length > 2 && first == SmileConstants.HEADER_BYTE_1 && bytes.get(1) == SmileConstants.HEADER_BYTE_2 && bytes.get(2) == SmileConstants.HEADER_BYTE_3) {
        return XContentType.SMILE;
    }
    if (length > 2 && first == '-' && bytes.get(1) == '-' && bytes.get(2) == '-') {
        return XContentType.YAML;
    }
    // CBOR logic similar to CBORFactory#hasCBORFormat
    if (first == CBORConstants.BYTE_OBJECT_INDEFINITE && length > 1){
        return XContentType.CBOR;
    }
    if (CBORConstants.hasMajorType(CBORConstants.MAJOR_TYPE_TAG, first) && length > 2) {
        // Actually, specific "self-describe tag" is a very good indicator
        if (first == (byte) 0xD9 && bytes.get(1) == (byte) 0xD9 && bytes.get(2) == (byte) 0xF7) {
            return XContentType.CBOR;
        }
    }
    // for small objects, some encoders just encode as major type object, we can safely
    // say its CBOR since it doesn't contradict SMILE or JSON, and its a last resort
    if (CBORConstants.hasMajorType(CBORConstants.MAJOR_TYPE_OBJECT, first)) {
        return XContentType.CBOR;
    }

    int jsonStart = 0;
    // JSON may be preceded by UTF-8 BOM
    if (length > 3 && first == (byte) 0xEF && bytes.get(1) == (byte) 0xBB && bytes.get(2) == (byte) 0xBF) {
        jsonStart = 3;
    }

    // a last chance for JSON
    for (int i = jsonStart; i < length; i++) {
        byte b = bytes.get(i);
        if (b == '{') {
            return XContentType.JSON;
        }
        if (Character.isWhitespace(b) == false) {
            break;
        }
    }
    return null;
}
 
Example 12
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;
}