Class TranscoderUtils
java.lang.Object
com.couchbase.client.java.transcoder.TranscoderUtils
public class TranscoderUtils extends Object
Helper methods and flags for the shipped
Transcoder
s.- Since:
- 2.0
- Author:
- Michael Nitschinger, Simon Baslé
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TranscoderUtils.ByteBufToArray
A class that holds information from aByteBuf
that allows to read its corresponding byte array.static class
TranscoderUtils.CommonFlags
The common flags enum. -
Field Summary
Fields Modifier and Type Field Description static int
BINARY_COMMON_FLAGS
static int
BINARY_COMPAT_FLAGS
static int
BINARY_LEGACY_FLAGS
static int
BOOLEAN_COMPAT_FLAGS
static int
BOOLEAN_LEGACY_FLAGS
static int
COMMON_FORMAT_MASK
32bit flag is composed of: - 3 compression bits - 1 bit reserved for future use - 4 format flags bits.static int
DOUBLE_COMPAT_FLAGS
static int
DOUBLE_LEGACY_FLAGS
static int
JSON_COMMON_FLAGS
static int
JSON_COMPAT_FLAGS
static int
JSON_LEGACY_FLAGS
static int
LONG_COMPAT_FLAGS
static int
LONG_LEGACY_FLAGS
static int
PRIVATE_COMMON_FLAGS
static int
SERIALIZED_COMPAT_FLAGS
static int
SERIALIZED_LEGACY_FLAGS
static int
STRING_COMMON_FLAGS
static int
STRING_COMPAT_FLAGS
static int
STRING_LEGACY_FLAGS
-
Method Summary
Modifier and Type Method Description static TranscoderUtils.ByteBufToArray
byteBufToByteArray(com.couchbase.client.deps.io.netty.buffer.ByteBuf input)
Converts aByteBuf
to a byte[] in the most straightforward manner available.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)
Decode aByteBuf
representing a valid JSON entity to the requested target class, using theObjectMapper
provided and without releasing the buffer.static Object
byteBufToGenericObject(com.couchbase.client.deps.io.netty.buffer.ByteBuf input, com.couchbase.client.deps.com.fasterxml.jackson.databind.ObjectMapper mapper)
Converts aByteBuf
representing a valid JSON entity to a genericObject
, without releasing the buffer.static byte[]
copyByteBufToByteArray(com.couchbase.client.deps.io.netty.buffer.ByteBuf input)
Converts aByteBuf
to a byte[] in the most straightforward manner available.static int
createCommonFlags(int flags)
Takes a integer representation of flags and moves them to the common flags MSBs.static Serializable
deserialize(com.couchbase.client.deps.io.netty.buffer.ByteBuf content)
Takes the input content and deserializes it.static com.couchbase.client.deps.io.netty.buffer.ByteBuf
encodeStringAsUtf8(String source)
Helper method to encode a String into UTF8 via fast-path methods.static int
extractCommonFlags(int flags)
Returns only the common flags from the full flags.static boolean
hasBinaryFlags(int flags)
Checks if the flags identify a binary document.static boolean
hasCommonFlags(int flags)
Checks whether the upper 8 bits are set, indicating common flags presence.static boolean
hasCommonFormat(int flags, int expectedCommonFlag)
Checks that flags has common flags bits set and that they correspond to expected common flags format.static boolean
hasCompressionFlags(int flags)
Checks whether the upper 3 bits are set, indicating compression presence.static boolean
hasJsonFlags(int flags)
Checks if the flags identify a JSON document.static boolean
hasSerializableFlags(int flags)
Checks if the flags identify a serialized document.static boolean
hasStringFlags(int flags)
Checks if the flags identify a String document.static com.couchbase.client.deps.io.netty.buffer.ByteBuf
serialize(Serializable serializable)
Serializes the input into a ByteBuf.
-
Field Details
-
COMMON_FORMAT_MASK
public static final int COMMON_FORMAT_MASK32bit 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"). -
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
-
BINARY_LEGACY_FLAGS
public static final int BINARY_LEGACY_FLAGS- See Also:
- Constant Field Values
-
STRING_LEGACY_FLAGS
public static final int STRING_LEGACY_FLAGS- See Also:
- Constant Field Values
-
JSON_LEGACY_FLAGS
public static final int JSON_LEGACY_FLAGS- See Also:
- Constant Field Values
-
BOOLEAN_LEGACY_FLAGS
public static final int BOOLEAN_LEGACY_FLAGS- See Also:
- Constant Field Values
-
LONG_LEGACY_FLAGS
public static final int LONG_LEGACY_FLAGS- See Also:
- Constant Field Values
-
DOUBLE_LEGACY_FLAGS
public static final int DOUBLE_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 Details
-
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 checkexpectedCommonFlag
- 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 ExceptionTakes 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 ExceptionSerializes the input into a ByteBuf.- Parameters:
serializable
- the object to serialize.- Returns:
- the serialized object.
- Throws:
Exception
- if something goes wrong during serialization.
-
encodeStringAsUtf8
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 aByteBuf
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 aByteBuf
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 IOExceptionDecode aByteBuf
representing a valid JSON entity to the requested target class, using theObjectMapper
provided and without releasing the buffer. Mapper uses aByteBufInputStream
reading directly the nettyByteBuf
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 IOExceptionConverts aByteBuf
representing a valid JSON entity to a genericObject
, 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 aJsonObject
and JSON arrays to aJsonArray
. 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 toJsonObject
orJsonArray
(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.
-