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.
Constructor and Description |
---|
AbstractByteArrayFragmentTranscoder() |
Modifier and Type | Method and 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 a
ByteBuf without releasing it. |
protected com.couchbase.client.deps.io.netty.buffer.ByteBuf |
doEncodeMulti(MultiValue<?> multiValue,
String transcodingErrorMessage)
Encode a
special mutation value that denotes multiple values being processed in bulk, to a ByteBuf 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 a
ByteBuf 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’s
OutputStream . |
decode, encode, encodeWithMessage
public <T> T decodeWithMessage(com.couchbase.client.deps.io.netty.buffer.ByteBuf encoded, Class<? extends T> clazz, String transcodingErrorMessage) throws TranscodingException
FragmentTranscoder
Decode content in a ByteBuf
without releasing it. Suitable for populating a DocumentFragment
’s content.
T
- the type of the decoded fragment.encoded
- the encoded fragment value (will not be released).clazz
- the target class for decoded value. Using Object.class
implies a generic decode, where dictionaries are represented by JsonObject
and arrays by JsonArray
.transcodingErrorMessage
- the error message to be used in the thrown TranscodingException
if the decoding couldn’t happen.TranscodingException
- if the decoding couldn’t happen.protected <T> com.couchbase.client.deps.io.netty.buffer.ByteBuf doEncodeSingle(T value, String transcodingErrorMessage) throws TranscodingException
AbstractFragmentTranscoder
Encode a single mutation value to a ByteBuf
suitable for use in the sub-document protocol.
doEncodeSingle
in class AbstractFragmentTranscoder
T
- the type of the fragment being encoded.value
- the value to encode.transcodingErrorMessage
- the error message to be used in the thrown TranscodingException
if the encoding couldn’t happen.ByteBuf
representation of the fragment value.TranscodingException
- if the encoding couldn’t happen.protected com.couchbase.client.deps.io.netty.buffer.ByteBuf doEncodeMulti(MultiValue<?> multiValue, String transcodingErrorMessage) throws TranscodingException
AbstractFragmentTranscoder
Encode a special mutation value
that denotes multiple values being processed in bulk, to a ByteBuf
suitable for use in the sub-document protocol.
doEncodeMulti
in class AbstractFragmentTranscoder
multiValue
- the multivalue to encode.transcodingErrorMessage
- the error message to be used in the thrown TranscodingException
if the encoding couldn’t happen.ByteBuf
representation of the fragment multivalue.TranscodingException
- if the encoding couldn’t happen.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 original ByteBuf
, which will get released from the heap.
If the array represents a non-scalar value, implementations may choose different classes like a JsonObject
or a Map
to instantiate it.
This method is called by decodeWithMessage(ByteBuf, Class, String)
when the clazz parameter is Object.class
.
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.IOException
protected abstract <T> T byteArrayToClass(byte[] byteArray, int offset, int length, Class<? extends T> clazz) throws IOException
Deserializes 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 original ByteBuf
, which will get released from the heap.
This method is called by decodeWithMessage(ByteBuf, Class, String)
when the clazz parameter isn’t Object.class
.
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
- the Class
to deserialize to.IOException
protected abstract <T> byte[] writeValueAsBytes(T value) throws IOException
Serializes a single value object as an array of bytes. The array will be backing a ByteBuf
, so modifications to the array will be visible in the ByteBuf.
value
- the value object to serialize.IOException
protected abstract void writeValueIntoStream(OutputStream out, Object o) throws IOException
Serializes a single object out of a sequence of multiple values, into the sequence’s OutputStream
. Implementation should simply write the bytes corresponding to the serialized value object into the stream.
out
- the OutputStream
of bytes representing a JSON sequence of serialized values.o
- the value among the sequence that is currently serialized.IOException
Copyright © 2015 Couchbase, Inc.