Java Code Examples for org.apache.cassandra.utils.ByteBufferUtil#readBytes()

The following examples show how to use org.apache.cassandra.utils.ByteBufferUtil#readBytes() . 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: AbstractCompositeType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(ByteBuffer bytes) throws MarshalException
{
    ByteBuffer bb = bytes.duplicate();
    readIsStatic(bb);

    int i = 0;
    ByteBuffer previous = null;
    while (bb.remaining() > 0)
    {
        AbstractType<?> comparator = validateComparator(i, bb);

        if (bb.remaining() < 2)
            throw new MarshalException("Not enough bytes to read value size of component " + i);
        int length = ByteBufferUtil.readShortLength(bb);

        if (bb.remaining() < length)
            throw new MarshalException("Not enough bytes to read value of component " + i);
        ByteBuffer value = ByteBufferUtil.readBytes(bb, length);

        comparator.validateCollectionMember(value, previous);

        if (bb.remaining() == 0)
            throw new MarshalException("Not enough bytes to read the end-of-component byte of component" + i);
        byte b = bb.get();
        if (b != 0 && bb.remaining() != 0)
            throw new MarshalException("Invalid bytes remaining after an end-of-component at component" + i);

        previous = value;
        ++i;
    }
}
 
Example 2
Source File: TupleType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public int compare(ByteBuffer o1, ByteBuffer o2)
{
    if (!o1.hasRemaining() || !o2.hasRemaining())
        return o1.hasRemaining() ? 1 : o2.hasRemaining() ? -1 : 0;

    ByteBuffer bb1 = o1.duplicate();
    ByteBuffer bb2 = o2.duplicate();

    for (int i = 0; bb1.remaining() > 0 && bb2.remaining() > 0; i++)
    {
        AbstractType<?> comparator = types.get(i);

        int size1 = bb1.getInt();
        int size2 = bb2.getInt();

        // Handle nulls
        if (size1 < 0)
        {
            if (size2 < 0)
                continue;
            return -1;
        }
        if (size2 < 0)
            return 1;

        ByteBuffer value1 = ByteBufferUtil.readBytes(bb1, size1);
        ByteBuffer value2 = ByteBufferUtil.readBytes(bb2, size2);
        int cmp = comparator.compare(value1, value2);
        if (cmp != 0)
            return cmp;
    }

    if (bb1.remaining() == 0)
        return bb2.remaining() == 0 ? 0 : -1;

    // bb1.remaining() > 0 && bb2.remaining() == 0
    return 1;
}
 
Example 3
Source File: CollectionSerializer.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public static ByteBuffer readValue(ByteBuffer input, int version)
{
    if (version >= Server.VERSION_3)
    {
        int size = input.getInt();
        if (size < 0)
            return null;

        return ByteBufferUtil.readBytes(input, size);
    }
    else
    {
        return ByteBufferUtil.readBytesWithShortLength(input);
    }
}
 
Example 4
Source File: DynamicCompositeType.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
protected AbstractType<?> validateComparator(int i, ByteBuffer bb) throws MarshalException
{
    AbstractType<?> comparator = null;
    if (bb.remaining() < 2)
        throw new MarshalException("Not enough bytes to header of the comparator part of component " + i);
    int header = ByteBufferUtil.readShortLength(bb);
    if ((header & 0x8000) == 0)
    {
        if (bb.remaining() < header)
            throw new MarshalException("Not enough bytes to read comparator name of component " + i);

        ByteBuffer value = ByteBufferUtil.readBytes(bb, header);
        String valueStr = null;
        try
        {
            valueStr = ByteBufferUtil.string(value);
            comparator = TypeParser.parse(valueStr);
        }
        catch (CharacterCodingException ce) 
        {
            // ByteBufferUtil.string failed. 
            // Log it here and we'll further throw an exception below since comparator == null
            logger.error("Failed with [{}] when decoding the byte buffer in ByteBufferUtil.string()", 
               ce.toString());
        }
        catch (Exception e)
        {
            // parse failed. 
            // Log it here and we'll further throw an exception below since comparator == null
            logger.error("Failed to parse value string \"{}\" with exception: [{}]", 
               valueStr, e.toString());
        }
    }
    else
    {
        comparator = aliases.get((byte)(header & 0xFF));
    }

    if (comparator == null)
        throw new MarshalException("Cannot find comparator for component " + i);
    else
        return comparator;
}