public Blob valueToBlob()

in hugegraph-core/src/main/java/org/apache/hugegraph/type/define/DataType.java [174:208]


    public <V> Blob valueToBlob(V value) {
        if (!this.isBlob()) {
            return null;
        }
        if (value instanceof Blob) {
            return (Blob) value;
        } else if (value instanceof byte[]) {
            return Blob.wrap((byte[]) value);
        } else if (value instanceof ByteBuffer) {
            return Blob.wrap(((ByteBuffer) value).array());
        } else if (value instanceof BytesBuffer) {
            return Blob.wrap(((BytesBuffer) value).bytes());
        } else if (value instanceof String) {
            // Only base64 string or hex string accepted
            String str = ((String) value);
            if (str.startsWith("0x")) {
                return Blob.wrap(Bytes.fromHex(str.substring(2)));
            }
            return Blob.wrap(StringEncoding.decodeBase64(str));
        } else if (value instanceof List) {
            List<?> values = (List<?>) value;
            byte[] bytes = new byte[values.size()];
            for (int i = 0; i < bytes.length; i++) {
                Object v = values.get(i);
                if (v instanceof Byte || v instanceof Integer) {
                    bytes[i] = ((Number) v).byteValue();
                } else {
                    throw new IllegalArgumentException(String.format(
                              "expect byte or int value, but got '%s'", v));
                }
            }
            return Blob.wrap(bytes);
        }
        return null;
    }