Module lettuce.core

Class StringCodec

java.lang.Object
io.lettuce.core.codec.StringCodec
All Implemented Interfaces:
RedisCodec<String,​String>, ToByteBufEncoder<String,​String>
Direct Known Subclasses:
Utf8StringCodec

public class StringCodec
extends Object
implements RedisCodec<String,​String>, ToByteBufEncoder<String,​String>
Optimized String codec. This RedisCodec encodes and decodes String keys and values using a specified Charset. It accepts provided buffers so it does not need to allocate buffers during encoding.
Since:
4.3
Author:
Mark Paluch, Dimitris Mandalidis
  • Field Details

  • Constructor Details

    • StringCodec

      public StringCodec()
      Creates a new StringCodec with the default charset. The default is determined from the file.encoding system property.
    • StringCodec

      public StringCodec​(Charset charset)
      Creates a new StringCodec for the given Charset that encodes and decodes keys and values.
      Parameters:
      charset - must not be null.
  • Method Details

    • encodeKey

      public void encodeKey​(String key, ByteBuf target)
      Description copied from interface: ToByteBufEncoder
      Encode the key for output to redis.
      Specified by:
      encodeKey in interface ToByteBufEncoder<String,​String>
      Parameters:
      key - the key, may be null.
      target - the target buffer, must not be null.
    • estimateSize

      public int estimateSize​(Object keyOrValue)
      Description copied from interface: ToByteBufEncoder
      Estimates the size of the resulting byte stream. This method is called for keys and values to estimate the size for the temporary buffer to allocate.
      Specified by:
      estimateSize in interface ToByteBufEncoder<String,​String>
      Parameters:
      keyOrValue - the key or value, may be null.
      Returns:
      the estimated number of bytes in the encoded representation.
    • encodeValue

      public void encodeValue​(String value, ByteBuf target)
      Description copied from interface: ToByteBufEncoder
      Encode the value for output to redis.
      Specified by:
      encodeValue in interface ToByteBufEncoder<String,​String>
      Parameters:
      value - the value, may be null.
      target - the target buffer, must not be null.
    • decodeKey

      public String decodeKey​(ByteBuffer bytes)
      Description copied from interface: RedisCodec
      Decode the key output by redis.
      Specified by:
      decodeKey in interface RedisCodec<String,​String>
      Parameters:
      bytes - Raw bytes of the key, must not be null.
      Returns:
      The decoded key, may be null.
    • decodeValue

      public String decodeValue​(ByteBuffer bytes)
      Description copied from interface: RedisCodec
      Decode the value output by redis.
      Specified by:
      decodeValue in interface RedisCodec<String,​String>
      Parameters:
      bytes - Raw bytes of the value, must not be null.
      Returns:
      The decoded value, may be null.
    • encodeKey

      public ByteBuffer encodeKey​(String key)
      Description copied from interface: RedisCodec
      Encode the key for output to redis.
      Specified by:
      encodeKey in interface RedisCodec<String,​String>
      Parameters:
      key - the key, may be null.
      Returns:
      The encoded key, never null.
    • encodeValue

      public ByteBuffer encodeValue​(String value)
      Description copied from interface: RedisCodec
      Encode the value for output to redis.
      Specified by:
      encodeValue in interface RedisCodec<String,​String>
      Parameters:
      value - the value, may be null.
      Returns:
      The encoded value, never null.
    • encode

      public void encode​(String str, ByteBuf target)