Class TranscoderUtils


  • public class TranscoderUtils
    extends Object
    Helper methods and flags for the shipped Transcoders.
    Since:
    2.0
    Author:
    Michael Nitschinger, Simon Baslé
    • Field Detail

      • COMMON_FORMAT_MASK

        public static final int COMMON_FORMAT_MASK
        32bit flag is composed of: - 3 compression bits - 1 bit reserved for future use - 4 format flags bits. those 8 upper bits make up the common flags - 8 bits reserved for future use - 16 bits for legacy flags This mask allows to compare a 32 bits flags with the 4 common flag format bits ("00001111 00000000 00000000 00000000").
        See Also:
        extractCommonFlags(int), hasCommonFlags(int), hasCompressionFlags(int), Constant Field Values
      • PRIVATE_COMMON_FLAGS

        public static final int PRIVATE_COMMON_FLAGS
      • JSON_COMMON_FLAGS

        public static final int JSON_COMMON_FLAGS
      • BINARY_COMMON_FLAGS

        public static final int BINARY_COMMON_FLAGS
      • STRING_COMMON_FLAGS

        public static final int STRING_COMMON_FLAGS
      • SERIALIZED_LEGACY_FLAGS

        public static final int SERIALIZED_LEGACY_FLAGS
        See Also:
        Constant Field Values
      • SERIALIZED_COMPAT_FLAGS

        public static final int SERIALIZED_COMPAT_FLAGS
      • JSON_COMPAT_FLAGS

        public static final int JSON_COMPAT_FLAGS
      • BINARY_COMPAT_FLAGS

        public static final int BINARY_COMPAT_FLAGS
      • BOOLEAN_COMPAT_FLAGS

        public static final int BOOLEAN_COMPAT_FLAGS
      • LONG_COMPAT_FLAGS

        public static final int LONG_COMPAT_FLAGS
      • DOUBLE_COMPAT_FLAGS

        public static final int DOUBLE_COMPAT_FLAGS
      • STRING_COMPAT_FLAGS

        public static final int STRING_COMPAT_FLAGS
    • Method Detail

      • hasCommonFlags

        public static boolean hasCommonFlags​(int flags)
        Checks whether the upper 8 bits are set, indicating common flags presence. It does this by shifting bits to the right until only the most significant bits are remaining and then checks if one of them is set.
        Parameters:
        flags - the flags to check.
        Returns:
        true if set, false otherwise.
      • hasCompressionFlags

        public static boolean hasCompressionFlags​(int flags)
        Checks whether the upper 3 bits are set, indicating compression presence. It does this by shifting bits to the right until only the most significant bits are remaining and then checks if one of them is set.
        Parameters:
        flags - the flags to check.
        Returns:
        true if compression set, false otherwise.
      • hasCommonFormat

        public static boolean hasCommonFormat​(int flags,
                                              int expectedCommonFlag)
        Checks that flags has common flags bits set and that they correspond to expected common flags format.
        Parameters:
        flags - the 32 bits flags to check
        expectedCommonFlag - the expected common flags format bits
        Returns:
        true if common flags bits are set and correspond to expectedCommonFlag format
      • extractCommonFlags

        public static int extractCommonFlags​(int flags)
        Returns only the common flags from the full flags.
        Parameters:
        flags - the flags to check.
        Returns:
        only the common flags simple representation (8 bits).
      • createCommonFlags

        public static int createCommonFlags​(int flags)
        Takes a integer representation of flags and moves them to the common flags MSBs.
        Parameters:
        flags - the flags to shift.
        Returns:
        an integer having the common flags set.
      • hasJsonFlags

        public static boolean hasJsonFlags​(int flags)
        Checks if the flags identify a JSON document. This method is strict if it finds common flags set, and if not falls back to a check of legacy JSON string (identified by 0 flags and no compression).
        Parameters:
        flags - the flags to check.
        Returns:
        true if JSON, false otherwise.
      • hasStringFlags

        public static boolean hasStringFlags​(int flags)
        Checks if the flags identify a String document. This method is strict if it finds common flags set, and if not falls back to a check of legacy String (identified by 0 flags and no compression).
        Parameters:
        flags - the flags to check.
        Returns:
        true if String, false otherwise.
      • hasSerializableFlags

        public static boolean hasSerializableFlags​(int flags)
        Checks if the flags identify a serialized document.
        Parameters:
        flags - the flags to check.
        Returns:
        true if serializable, false otherwise.
      • hasBinaryFlags

        public static boolean hasBinaryFlags​(int flags)
        Checks if the flags identify a binary document.
        Parameters:
        flags - the flags to check.
        Returns:
        true if binary, false otherwise.
      • deserialize

        public static Serializable deserialize​(com.couchbase.client.deps.io.netty.buffer.ByteBuf content)
                                        throws Exception
        Takes the input content and deserializes it.
        Parameters:
        content - the content to deserialize.
        Returns:
        the serializable object.
        Throws:
        Exception - if something goes wrong during deserialization.
      • serialize

        public static com.couchbase.client.deps.io.netty.buffer.ByteBuf serialize​(Serializable serializable)
                                                                           throws Exception
        Serializes the input into a ByteBuf.
        Parameters:
        serializable - the object to serialize.
        Returns:
        the serialized object.
        Throws:
        Exception - if something goes wrong during serialization.
      • encodeStringAsUtf8

        public static com.couchbase.client.deps.io.netty.buffer.ByteBuf encodeStringAsUtf8​(String source)
        Helper method to encode a String into UTF8 via fast-path methods.
        Parameters:
        source - the source document.
        Returns:
        the encoded byte buffer.
      • byteBufToByteArray

        public static TranscoderUtils.ByteBufToArray byteBufToByteArray​(com.couchbase.client.deps.io.netty.buffer.ByteBuf input)
        Converts a ByteBuf to a byte[] in the most straightforward manner available.
        Parameters:
        input - the ByteBuf to convert.
        Returns:
        a TranscoderUtils.ByteBufToArray containing the byte[] array, as well as the offset and length to use (in case the actual array is longer than the data the ByteBuf represents for instance).
      • copyByteBufToByteArray

        public static byte[] copyByteBufToByteArray​(com.couchbase.client.deps.io.netty.buffer.ByteBuf input)
        Converts a ByteBuf to a byte[] in the most straightforward manner available. the byte[] returned is a copy of the actual data
        Parameters:
        input - the ByteBuf to convert.
        Returns:
        a byte[] array
      • byteBufToClass

        public static <T> T byteBufToClass​(com.couchbase.client.deps.io.netty.buffer.ByteBuf input,
                                           Class<? extends T> clazz,
                                           com.couchbase.client.deps.com.fasterxml.jackson.databind.ObjectMapper mapper)
                                    throws IOException
        Decode a ByteBuf representing a valid JSON entity to the requested target class, using the ObjectMapper provided and without releasing the buffer. Mapper uses a ByteBufInputStream reading directly the netty ByteBuf if its backed by a direct buffer and avoids a memory copy if backed by a heap buffer by using the original one with an offset.
        Type Parameters:
        T - the decoded type.
        Parameters:
        input - the ByteBuf to decode.
        clazz - the class to decode to.
        mapper - the mapper to use for decoding.
        Returns:
        the decoded value.
        Throws:
        IOException - in case decoding failed.
      • byteBufToGenericObject

        public static Object byteBufToGenericObject​(com.couchbase.client.deps.io.netty.buffer.ByteBuf input,
                                                    com.couchbase.client.deps.com.fasterxml.jackson.databind.ObjectMapper mapper)
                                             throws IOException
        Converts a ByteBuf representing a valid JSON entity to a generic Object, without releasing the buffer. The entity can either be a JSON object, array or scalar value, potentially with leading whitespace (which gets ignored). JSON objects are converted to a JsonObject and JSON arrays to a JsonArray. Detection of JSON objects and arrays is attempted in order not to incur an additional conversion step (JSON to Map to JsonObject for example), but if a Map or List is produced, it will be transformed to JsonObject or JsonArray (with a warning logged).
        Parameters:
        input - the buffer to convert. It won't be released.
        Returns:
        a Object decoded from the buffer
        Throws:
        IOException - if the decoding fails.