Class AbstractByteArrayFragmentTranscoder
java.lang.Object
com.couchbase.client.java.transcoder.subdoc.AbstractFragmentTranscoder
com.couchbase.client.java.transcoder.subdoc.AbstractByteArrayFragmentTranscoder
- All Implemented Interfaces:
FragmentTranscoder
- Direct Known Subclasses:
JacksonFragmentTranscoder
public abstract class AbstractByteArrayFragmentTranscoder extends AbstractFragmentTranscoder
An
AbstractFragmentTranscoder
that further implements decoding and encoding of messaging,
easing the implementation of a concrete FragmentTranscoder
based on byte arrays zero-copied
from ByteBuf
.
Note that the serialization/deserialization mean should be able to work with byte arrays and write to
an OutputStream
, and that byte arrays should be treated as transient (eg. not used to back long
living objects), as they may be tied to the original ByteBuf
which will get released.- Since:
- 2.3
- Author:
- Simon Baslé
-
Constructor Summary
Constructors Constructor Description AbstractByteArrayFragmentTranscoder()
-
Method Summary
Modifier and Type Method Description protected abstract <T> T
byteArrayToClass(byte[] byteArray, int offset, int length, Class<? extends T> clazz)
Deserializes a byte array into a specific class instance.protected abstract Object
byteArrayToGenericObject(byte[] byteArray, int offset, int length)
Deserializes a byte array into a generic Object.<T> T
decodeWithMessage(com.couchbase.client.deps.io.netty.buffer.ByteBuf encoded, Class<? extends T> clazz, String transcodingErrorMessage)
Decode content in aByteBuf
**without releasing it**.protected com.couchbase.client.deps.io.netty.buffer.ByteBuf
doEncodeMulti(MultiValue<?> multiValue, String transcodingErrorMessage)
Encode aspecial mutation value
that denotes multiple values being processed in bulk, to aByteBuf
suitable for use in the sub-document protocol.protected <T> com.couchbase.client.deps.io.netty.buffer.ByteBuf
doEncodeSingle(T value, String transcodingErrorMessage)
Encode a single mutation value to aByteBuf
suitable for use in the sub-document protocol.protected abstract <T> byte[]
writeValueAsBytes(T value)
Serializes a single value object as an array of bytes.protected abstract void
writeValueIntoStream(OutputStream out, Object o)
Serializes a single object out of a sequence of multiple values, into the sequence'sOutputStream
.Methods inherited from class com.couchbase.client.java.transcoder.subdoc.AbstractFragmentTranscoder
decode, encode, encodeWithMessage
-
Constructor Details
-
AbstractByteArrayFragmentTranscoder
public AbstractByteArrayFragmentTranscoder()
-
-
Method Details
-
decodeWithMessage
public <T> T decodeWithMessage(com.couchbase.client.deps.io.netty.buffer.ByteBuf encoded, Class<? extends T> clazz, String transcodingErrorMessage) throws TranscodingExceptionDescription copied from interface:FragmentTranscoder
Decode content in aByteBuf
**without releasing it**. Suitable for populating aDocumentFragment
's content.- Type Parameters:
T
- the type of the decoded fragment.- Parameters:
encoded
- the encoded fragment value (will not be released).clazz
- the target class for decoded value. UsingObject.class
implies a generic decode, where dictionaries are represented byJsonObject
and arrays byJsonArray
.transcodingErrorMessage
- the error message to be used in the thrownTranscodingException
if the decoding couldn't happen.- Returns:
- a decoded fragment.
- Throws:
TranscodingException
- if the decoding couldn't happen.
-
doEncodeSingle
protected <T> com.couchbase.client.deps.io.netty.buffer.ByteBuf doEncodeSingle(T value, String transcodingErrorMessage) throws TranscodingExceptionDescription copied from class:AbstractFragmentTranscoder
Encode a single mutation value to aByteBuf
suitable for use in the sub-document protocol.- Specified by:
doEncodeSingle
in classAbstractFragmentTranscoder
- Type Parameters:
T
- the type of the fragment being encoded.- Parameters:
value
- the value to encode.transcodingErrorMessage
- the error message to be used in the thrownTranscodingException
if the encoding couldn't happen.- Returns:
- a
ByteBuf
representation of the fragment value. - Throws:
TranscodingException
- if the encoding couldn't happen.
-
doEncodeMulti
protected com.couchbase.client.deps.io.netty.buffer.ByteBuf doEncodeMulti(MultiValue<?> multiValue, String transcodingErrorMessage) throws TranscodingExceptionDescription copied from class:AbstractFragmentTranscoder
Encode aspecial mutation value
that denotes multiple values being processed in bulk, to aByteBuf
suitable for use in the sub-document protocol.- Specified by:
doEncodeMulti
in classAbstractFragmentTranscoder
- Parameters:
multiValue
- the multivalue to encode.transcodingErrorMessage
- the error message to be used in the thrownTranscodingException
if the encoding couldn't happen.- Returns:
- a
ByteBuf
representation of the fragment multivalue. - Throws:
TranscodingException
- if the encoding couldn't happen.
-
byteArrayToGenericObject
protected abstract Object byteArrayToGenericObject(byte[] byteArray, int offset, int length) throws IOExceptionDeserializes a byte array into a generic Object. The provided offset and length must be considered when processing the array, which may hold more data that just the value to deserialize. Note that he byte array should not be considered reliable for long-term usage (eg. backing a String) as it might be tied to the originalByteBuf
, which will get released from the heap. If the array represents a non-scalar value, implementations may choose different classes like aJsonObject
or aMap
to instantiate it. This method is called bydecodeWithMessage(ByteBuf, Class, String)
when the clazz parameter isObject.class
.- Parameters:
byteArray
- the array of bytes containing the value to deserialize (you'll need to copy it if long term usage is needed).offset
- the offset in the array at which the value starts.length
- the number of bytes after the offset that represents the value.- Returns:
- an instance of a suitable generic Object representation of the value.
- Throws:
IOException
-
byteArrayToClass
protected abstract <T> T byteArrayToClass(byte[] byteArray, int offset, int length, Class<? extends T> clazz) throws IOExceptionDeserializes a byte array into a specific class instance. The provided offset and length must be considered when processing the array, which may hold more data that just the value to deserialize. Note that he byte array should not be considered reliable for long-term usage (eg. backing a String) as it might be tied to the originalByteBuf
, which will get released from the heap. This method is called bydecodeWithMessage(ByteBuf, Class, String)
when the clazz parameter isn'tObject.class
.- Parameters:
byteArray
- the array of bytes containing the value to deserialize (you'll need to copy it if long term usage is needed).offset
- the offset in the array at which the value starts.length
- the number of bytes after the offset that represents the value.clazz
- theClass
to deserialize to.- Returns:
- an instance of a suitable generic Object representation of the value.
- Throws:
IOException
-
writeValueAsBytes
Serializes a single value object as an array of bytes. The array will be backing aByteBuf
, so modifications to the array will be visible in the ByteBuf.- Parameters:
value
- the value object to serialize.- Returns:
- the array of bytes representing the serialized value object.
- Throws:
IOException
-
writeValueIntoStream
Serializes a single object out of a sequence of multiple values, into the sequence'sOutputStream
. Implementation should simply write the bytes corresponding to the serialized value object into the stream.- Parameters:
out
- theOutputStream
of bytes representing a JSON sequence of serialized values.o
- the value among the sequence that is currently serialized.- Throws:
IOException
-