Class DefaultCodecs

java.lang.Object
io.r2dbc.postgresql.codec.DefaultCodecs
All Implemented Interfaces:
CodecRegistry, Codecs, Iterable<Codec<?>>

public final class DefaultCodecs extends Object implements Codecs, CodecRegistry
The default Codec implementation. Delegates to type-specific codec implementations.

Codecs can be configured to prefer or avoid attached buffers for certain data types. Using attached buffers is more memory-efficient as data doesn't need to be copied. In turn, attached buffers require release or consumption to avoid memory leaks. By default, codecs don't use attached buffers to minimize the risk of memory leaks.

  • Constructor Summary

    Constructors
    Constructor
    Description
    DefaultCodecs(io.netty.buffer.ByteBufAllocator byteBufAllocator)
    Create a new instance of DefaultCodecs preferring detached (copied buffers).
    DefaultCodecs(io.netty.buffer.ByteBufAllocator byteBufAllocator, boolean preferAttachedBuffers)
    Create a new instance of DefaultCodecs preferring detached (copied buffers).
    DefaultCodecs(io.netty.buffer.ByteBufAllocator byteBufAllocator, boolean preferAttachedBuffers, CodecConfiguration configuration)
    Create a new instance of DefaultCodecs.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addFirst(Codec<?> codec)
    Register codec before all other codecs.
    void
    addLast(Codec<?> codec)
    Register codec after all other codecs.
    <T> @Nullable T
    decode(@Nullable io.netty.buffer.ByteBuf buffer, int dataType, io.r2dbc.postgresql.message.Format format, Class<? extends T> type)
    Decode a data to a value.
    io.r2dbc.postgresql.client.EncodedParameter
    encode(Object value)
    Encode a value.
    io.r2dbc.postgresql.client.EncodedParameter
    encodeNull(Class<?> type)
    Encode a null value.
     
    preferredType(int dataType, io.r2dbc.postgresql.message.Format format)
    Returns the preferred Java type for a given data type and format.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface Iterable

    forEach, spliterator
  • Constructor Details

    • DefaultCodecs

      public DefaultCodecs(io.netty.buffer.ByteBufAllocator byteBufAllocator)
      Create a new instance of DefaultCodecs preferring detached (copied buffers).
      Parameters:
      byteBufAllocator - the ByteBufAllocator to use for encoding
    • DefaultCodecs

      public DefaultCodecs(io.netty.buffer.ByteBufAllocator byteBufAllocator, boolean preferAttachedBuffers)
      Create a new instance of DefaultCodecs preferring detached (copied buffers).
      Parameters:
      byteBufAllocator - the ByteBufAllocator to use for encoding
      preferAttachedBuffers - whether to prefer attached (pooled) buffers. Use false (default) to use detached buffers which minimize the risk of memory leaks.
    • DefaultCodecs

      public DefaultCodecs(io.netty.buffer.ByteBufAllocator byteBufAllocator, boolean preferAttachedBuffers, CodecConfiguration configuration)
      Create a new instance of DefaultCodecs.
      Parameters:
      byteBufAllocator - the ByteBufAllocator to use for encoding
      preferAttachedBuffers - whether to prefer attached (pooled) buffers. Use false (default) to use detached buffers which minimize the risk of memory leaks.
      configuration - the CodecConfiguration to use for encoding/decoding
  • Method Details

    • addFirst

      public void addFirst(Codec<?> codec)
      Description copied from interface: CodecRegistry
      Register codec before all other codecs.
      Specified by:
      addFirst in interface CodecRegistry
      Parameters:
      codec - the codec to register
    • addLast

      public void addLast(Codec<?> codec)
      Description copied from interface: CodecRegistry
      Register codec after all other codecs.
      Specified by:
      addLast in interface CodecRegistry
      Parameters:
      codec - the codec to register
    • decode

      public <T> @Nullable T decode(@Nullable io.netty.buffer.ByteBuf buffer, int dataType, io.r2dbc.postgresql.message.Format format, Class<? extends T> type)
      Description copied from interface: Codecs
      Decode a data to a value.
      Specified by:
      decode in interface Codecs
      Type Parameters:
      T - the type of item being returned
      Parameters:
      buffer - the ByteBuf to decode
      dataType - the data type of the data
      format - the format of the data
      type - the type to decode to
      Returns:
      the decoded value
    • encode

      public io.r2dbc.postgresql.client.EncodedParameter encode(Object value)
      Description copied from interface: Codecs
      Encode a value.
      Specified by:
      encode in interface Codecs
      Parameters:
      value - the value to encode
      Returns:
      the encoded value
    • encodeNull

      public io.r2dbc.postgresql.client.EncodedParameter encodeNull(Class<?> type)
      Description copied from interface: Codecs
      Encode a null value.
      Specified by:
      encodeNull in interface Codecs
      Parameters:
      type - the type to encode
      Returns:
      the encoded value
    • preferredType

      public Class<?> preferredType(int dataType, io.r2dbc.postgresql.message.Format format)
      Description copied from interface: Codecs
      Returns the preferred Java type for a given data type and format.
      Specified by:
      preferredType in interface Codecs
      Parameters:
      dataType - the data type fo the data
      format - the format of the data
      Returns:
      the preferred Java type for a given data type and format
    • iterator

      public Iterator<Codec<?>> iterator()
      Specified by:
      iterator in interface Iterable<Codec<?>>