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
AnAbstractFragmentTranscoderthat further implements decoding and encoding of messaging, easing the implementation of a concreteFragmentTranscoderbased on byte arrays zero-copied fromByteBuf. Note that the serialization/deserialization mean should be able to work with byte arrays and write to anOutputStream, and that byte arrays should be treated as transient (eg. not used to back long living objects), as they may be tied to the originalByteBufwhich will get released.- Since:
- 2.3
- Author:
- Simon Baslé
-
-
Constructor Summary
Constructors Constructor Description AbstractByteArrayFragmentTranscoder()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract <T> TbyteArrayToClass(byte[] byteArray, int offset, int length, Class<? extends T> clazz)Deserializes a byte array into a specific class instance.protected abstract ObjectbyteArrayToGenericObject(byte[] byteArray, int offset, int length)Deserializes a byte array into a generic Object.<T> TdecodeWithMessage(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.ByteBufdoEncodeMulti(MultiValue<?> multiValue, String transcodingErrorMessage)Encode aspecial mutation valuethat denotes multiple values being processed in bulk, to aByteBufsuitable for use in the sub-document protocol.protected <T> com.couchbase.client.deps.io.netty.buffer.ByteBufdoEncodeSingle(T value, String transcodingErrorMessage)Encode a single mutation value to aByteBufsuitable 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 voidwriteValueIntoStream(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
-
-
-
-
Method Detail
-
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:FragmentTranscoderDecode 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.classimplies a generic decode, where dictionaries are represented byJsonObjectand arrays byJsonArray.transcodingErrorMessage- the error message to be used in the thrownTranscodingExceptionif 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:AbstractFragmentTranscoderEncode a single mutation value to aByteBufsuitable for use in the sub-document protocol.- Specified by:
doEncodeSinglein 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 thrownTranscodingExceptionif the encoding couldn't happen.- Returns:
- a
ByteBufrepresentation 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 TranscodingException
Description copied from class:AbstractFragmentTranscoderEncode aspecial mutation valuethat denotes multiple values being processed in bulk, to aByteBufsuitable for use in the sub-document protocol.- Specified by:
doEncodeMultiin classAbstractFragmentTranscoder- Parameters:
multiValue- the multivalue to encode.transcodingErrorMessage- the error message to be used in the thrownTranscodingExceptionif the encoding couldn't happen.- Returns:
- a
ByteBufrepresentation of the fragment multivalue. - Throws:
TranscodingException- if the encoding couldn't happen.
-
byteArrayToGenericObject
protected abstract Object byteArrayToGenericObject(byte[] byteArray, int offset, int length) throws IOException
Deserializes 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 aJsonObjector aMapto 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- theClassto deserialize to.- Returns:
- an instance of a suitable generic Object representation of the value.
- Throws:
IOException
-
writeValueAsBytes
protected abstract <T> byte[] writeValueAsBytes(T value) throws IOExceptionSerializes 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
protected abstract void writeValueIntoStream(OutputStream out, Object o) throws IOException
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- theOutputStreamof bytes representing a JSON sequence of serialized values.o- the value among the sequence that is currently serialized.- Throws:
IOException
-
-