public class TranscoderUtils extends Object
Helper methods and flags for the shipped Transcoder
s.
Modifier and Type | Class and Description |
---|---|
static class |
TranscoderUtils.ByteBufToArray
A class that holds information from a
ByteBuf that allows to read its corresponding byte array. |
static class |
TranscoderUtils.CommonFlags
The common flags enum.
|
Modifier and Type | Field and 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 |
Modifier and Type | Method and Description |
---|---|
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. |
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 a
ByteBuf representing a valid JSON entity to the requested target class, using the ObjectMapper 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 a
ByteBuf representing a valid JSON entity to a generic Object , without releasing the buffer. |
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.
|
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”).
public static final int PRIVATE_COMMON_FLAGS
public static final int JSON_COMMON_FLAGS
public static final int BINARY_COMMON_FLAGS
public static final int STRING_COMMON_FLAGS
public static final int SERIALIZED_LEGACY_FLAGS
public static final int BINARY_LEGACY_FLAGS
public static final int STRING_LEGACY_FLAGS
public static final int JSON_LEGACY_FLAGS
public static final int BOOLEAN_LEGACY_FLAGS
public static final int LONG_LEGACY_FLAGS
public static final int DOUBLE_LEGACY_FLAGS
public static final int SERIALIZED_COMPAT_FLAGS
public static final int JSON_COMPAT_FLAGS
public static final int BINARY_COMPAT_FLAGS
public static final int BOOLEAN_COMPAT_FLAGS
public static final int LONG_COMPAT_FLAGS
public static final int DOUBLE_COMPAT_FLAGS
public static final int STRING_COMPAT_FLAGS
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.
flags
- the flags to check.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.
flags
- the flags to check.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.
flags
- the 32 bits flags to checkexpectedCommonFlag
- the expected common flags format bitspublic static int extractCommonFlags(int flags)
Returns only the common flags from the full flags.
flags
- the flags to check.public static int createCommonFlags(int flags)
Takes a integer representation of flags and moves them to the common flags MSBs.
flags
- the flags to shift.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).
flags
- the flags to check.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).
flags
- the flags to check.public static boolean hasSerializableFlags(int flags)
Checks if the flags identify a serialized document.
flags
- the flags to check.public static boolean hasBinaryFlags(int flags)
Checks if the flags identify a binary document.
flags
- the flags to check.public static Serializable deserialize(com.couchbase.client.deps.io.netty.buffer.ByteBuf content) throws Exception
Takes the input content and deserializes it.
content
- the content to deserialize.Exception
- if something goes wrong during deserialization.public static com.couchbase.client.deps.io.netty.buffer.ByteBuf serialize(Serializable serializable) throws Exception
Serializes the input into a ByteBuf.
serializable
- the object to serialize.Exception
- if something goes wrong during serialization.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.
source
- the source document.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.
input
- the ByteBuf to convert.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).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 the byte[], performing the most straightforward conversion from ByteBuf to byte[] available. (see byteBufToByteArray(ByteBuf)
).
input
- the ByteBuf to decode.clazz
- the class to decode to.mapper
- the mapper to use for decoding.T
- the decoded type.IOException
- in case decoding failed.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).
input
- the buffer to convert. It won’t be released.IOException
- if the decoding fails.Copyright © 2015 Couchbase, Inc.