Class CodecUtils


  • public final class CodecUtils
    extends Object
    A set of utility methods to deal with type conversion and serialization.
    • Method Detail

      • pack

        public static ByteBuffer pack​(ByteBuffer[] buffers,
                                      int elements,
                                      ProtocolVersion version)
        Utility method that "packs" together a list of ByteBuffers containing serialized collection elements. Mainly intended for use with collection codecs when serializing collections.
        Parameters:
        buffers - the collection elements
        elements - the total number of elements
        version - the protocol version to use
        Returns:
        The serialized collection
      • readSize

        public static int readSize​(ByteBuffer input,
                                   ProtocolVersion version)
        Utility method that reads a size value. Mainly intended for collection codecs when deserializing CQL collections.
        Parameters:
        input - The ByteBuffer to read from.
        version - The protocol version to use.
        Returns:
        The size value.
      • writeSize

        public static void writeSize​(ByteBuffer output,
                                     int size,
                                     ProtocolVersion version)
        Utility method that writes a size value. Mainly intended for collection codecs when serializing CQL collections.
        Parameters:
        output - The ByteBuffer to write to.
        size - The collection size.
        version - The protocol version to use.
      • readValue

        public static ByteBuffer readValue​(ByteBuffer input,
                                           ProtocolVersion version)
        Utility method that reads a value. Mainly intended for collection codecs when deserializing CQL collections.
        Parameters:
        input - The ByteBuffer to read from.
        version - The protocol version to use.
        Returns:
        The collection element.
      • writeValue

        public static void writeValue​(ByteBuffer output,
                                      ByteBuffer value,
                                      ProtocolVersion version)
        Utility method that writes a value. Mainly intended for collection codecs when deserializing CQL collections.
        Parameters:
        output - The ByteBuffer to write to.
        value - The value to write.
        version - The protocol version to use.
      • readBytes

        public static ByteBuffer readBytes​(ByteBuffer bb,
                                           int length)
        Read length bytes from bb into a new ByteBuffer.
        Parameters:
        bb - The ByteBuffer to read.
        length - The number of bytes to read.
        Returns:
        The read bytes.
      • fromUnsignedToSignedInt

        public static int fromUnsignedToSignedInt​(int unsigned)
        Converts an "unsigned" int read from a DATE value into a signed int.

        The protocol encodes DATE values as unsigned ints with the Epoch in the middle of the range (2^31). This method handles the conversion from an "unsigned" to a signed int.

      • fromSignedToUnsignedInt

        public static int fromSignedToUnsignedInt​(int signed)
        Converts an int into an "unsigned" int suitable to be written as a DATE value.

        The protocol encodes DATE values as unsigned ints with the Epoch in the middle of the range (2^31). This method handles the conversion from a signed to an "unsigned" int.

      • fromCqlDateToDaysSinceEpoch

        public static int fromCqlDateToDaysSinceEpoch​(long raw)
        Convert from a raw CQL long representing a numeric DATE literal to the number of days since the Epoch. In CQL, numeric DATE literals are longs (unsigned integers actually) between 0 and 2^32 - 1, with the epoch in the middle; this method re-centers the epoch at 0.
        Parameters:
        raw - The CQL date value to convert.
        Returns:
        The number of days since the Epoch corresponding to the given raw value.
        Throws:
        IllegalArgumentException - if the value is out of range.
      • fromDaysSinceEpochToCqlDate

        public static long fromDaysSinceEpochToCqlDate​(int days)
        Convert the number of days since the Epoch into a raw CQL long representing a numeric DATE literal.

        In CQL, numeric DATE literals are longs (unsigned integers actually) between 0 and 2^32 - 1, with the epoch in the middle; this method re-centers the epoch at 2^31.

        Parameters:
        days - The number of days since the Epoch convert.
        Returns:
        The CQL date value corresponding to the given value.