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 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 TranscodingException
      Description copied from interface: FragmentTranscoder
      Decode content in a ByteBuf **without releasing it**. Suitable for populating a DocumentFragment'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. 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.
      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 TranscodingException
      Description copied from class: AbstractFragmentTranscoder
      Encode a single mutation value to a ByteBuf suitable for use in the sub-document protocol.
      Specified by:
      doEncodeSingle in class AbstractFragmentTranscoder
      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 thrown TranscodingException 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 TranscodingException
      Description copied from class: 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.
      Specified by:
      doEncodeMulti in class AbstractFragmentTranscoder
      Parameters:
      multiValue - the multivalue to encode.
      transcodingErrorMessage - the error message to be used in the thrown TranscodingException 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 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.
      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 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.
      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 - the Class to 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 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.
      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's OutputStream. Implementation should simply write the bytes corresponding to the serialized value object into the stream.
      Parameters:
      out - the OutputStream of bytes representing a JSON sequence of serialized values.
      o - the value among the sequence that is currently serialized.
      Throws:
      IOException