public class TranscoderUtils extends Object
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 byte[] |
copyByteBufToByteArray(com.couchbase.client.deps.io.netty.buffer.ByteBuf input)
Converts a
ByteBuf 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.
|
public static final int COMMON_FORMAT_MASK
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)
flags
- the flags to check.public static boolean hasCompressionFlags(int flags)
flags
- the flags to check.public static boolean hasCommonFormat(int flags, int expectedCommonFlag)
flags
- the 32 bits flags to checkexpectedCommonFlag
- the expected common flags format bitspublic static int extractCommonFlags(int flags)
flags
- the flags to check.public static int createCommonFlags(int flags)
flags
- the flags to shift.public static boolean hasJsonFlags(int flags)
flags
- the flags to check.public static boolean hasStringFlags(int flags)
flags
- the flags to check.public static boolean hasSerializableFlags(int flags)
flags
- the flags to check.public static boolean hasBinaryFlags(int flags)
flags
- the flags to check.public static Serializable deserialize(com.couchbase.client.deps.io.netty.buffer.ByteBuf content) throws Exception
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
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)
source
- the source document.public static TranscoderUtils.ByteBufToArray byteBufToByteArray(com.couchbase.client.deps.io.netty.buffer.ByteBuf input)
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 byte[] copyByteBufToByteArray(com.couchbase.client.deps.io.netty.buffer.ByteBuf input)
ByteBuf
to a byte[] in the most straightforward manner available.
the byte[] returned is a copy of the actual datainput
- the ByteBuf to convert.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
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.T
- the decoded type.input
- the ByteBuf to decode.clazz
- the class to decode to.mapper
- the mapper to use for decoding.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
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.