Example usage for org.apache.cassandra.db.marshal AbstractType validate

List of usage examples for org.apache.cassandra.db.marshal AbstractType validate

Introduction

In this page you can find the example usage for org.apache.cassandra.db.marshal AbstractType validate.

Prototype

public void validate(ByteBuffer bytes) throws MarshalException 

Source Link

Usage

From source file:com.datastax.driver.core.DataType.java

License:Apache License

/**
 * Deserialize a value of this type from the provided bytes.
 * <p>//from  w  w w  .ja  va2  s .co  m
 * The format of {@code bytes} must correspond to the Cassandra
 * encoding for this type.
 *
 * @param bytes bytes holding the value to deserialize.
 * @return the deserialized value (of class {@code this.asJavaClass()}).
 * Will return {@code null} if either {@code bytes} is {@code null} or if
 * {@code bytes.remaining() == 0} and this type has no value corresponding
 * to an empty byte buffer (the latter somewhat strange behavior is due to
 * the fact that for historical/technical reason, Cassandra types always
 * accept empty byte buffer as valid value of those type, and so we avoid
 * throwing an exception in that case. It is however highly discouraged to
 * store empty byte buffers for types for which it doesn't make sense, so
 * this implementation can generally be ignored).
 *
 * @throws InvalidTypeException if {@code bytes} is not a valid
 * encoding of an object of this {@code DataType}.
 */
public Object deserialize(ByteBuffer bytes) {
    AbstractType<?> codec = Codec.getCodec(this);
    try {
        codec.validate(bytes);
    } catch (MarshalException e) {
        throw new InvalidTypeException(
                String.format("Invalid serialized value for type %s (%s)", toString(), e.getMessage()));
    }

    try {
        return codec.compose(bytes);
    } catch (IndexOutOfBoundsException e) {
        // As it happens, types like Int32Type will accept empty byte buffers
        // in their validate method, but their compose method will throw. We
        // should probably fix that Cassandra side, but in the meantime ...
        return null;
    }
}