Package io.lettuce.core.codec
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. ThisRedisCodec
encodes and decodesString
keys and values using a specifiedCharset
. It accepts providedbuffers
so it does not need to allocate buffers during encoding.- Since:
- 4.3
- Author:
- Mark Paluch, Dimitris Mandalidis
-
-
Field Summary
Fields Modifier and Type Field Description static StringCodec
ASCII
static StringCodec
UTF8
-
Constructor Summary
Constructors Constructor Description StringCodec()
Creates a newStringCodec
with the defaultcharset
.StringCodec(Charset charset)
Creates a newStringCodec
for the givenCharset
that encodes and decodes keys and values.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
decodeKey(ByteBuffer bytes)
Decode the key output by redis.String
decodeValue(ByteBuffer bytes)
Decode the value output by redis.void
encode(String str, ByteBuf target)
ByteBuffer
encodeKey(String key)
Encode the key for output to redis.void
encodeKey(String key, ByteBuf target)
Encode the key for output to redis.ByteBuffer
encodeValue(String value)
Encode the value for output to redis.void
encodeValue(String value, ByteBuf target)
Encode the value for output to redis.int
estimateSize(Object keyOrValue)
Estimates the size of the resulting byte stream.boolean
isEstimateExact()
Returnstrue
ifToByteBufEncoder.estimateSize(Object)
returns exact size This is used as an optimization to reduce memory allocations when encoding data.
-
-
-
Field Detail
-
UTF8
public static final StringCodec UTF8
-
ASCII
public static final StringCodec ASCII
-
-
Constructor Detail
-
StringCodec
public StringCodec()
Creates a newStringCodec
with the defaultcharset
. The default is determined from thefile.encoding
system property.
-
StringCodec
public StringCodec(Charset charset)
Creates a newStringCodec
for the givenCharset
that encodes and decodes keys and values.- Parameters:
charset
- must not benull
.
-
-
Method Detail
-
encodeKey
public void encodeKey(String key, ByteBuf target)
Description copied from interface:ToByteBufEncoder
Encode the key for output to redis.- Specified by:
encodeKey
in interfaceToByteBufEncoder<String,String>
- Parameters:
key
- the key, may benull
.target
- the target buffer, must not benull
.
-
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 interfaceToByteBufEncoder<String,String>
- Parameters:
keyOrValue
- the key or value, may benull
.- Returns:
- the estimated number of bytes in the encoded representation.
-
isEstimateExact
public boolean isEstimateExact()
Description copied from interface:ToByteBufEncoder
Returnstrue
ifToByteBufEncoder.estimateSize(Object)
returns exact size This is used as an optimization to reduce memory allocations when encoding data.- Specified by:
isEstimateExact
in interfaceToByteBufEncoder<String,String>
- Returns:
true
ifToByteBufEncoder.estimateSize(Object)
returns exact size.
-
encodeValue
public void encodeValue(String value, ByteBuf target)
Description copied from interface:ToByteBufEncoder
Encode the value for output to redis.- Specified by:
encodeValue
in interfaceToByteBufEncoder<String,String>
- Parameters:
value
- the value, may benull
.target
- the target buffer, must not benull
.
-
decodeKey
public String decodeKey(ByteBuffer bytes)
Description copied from interface:RedisCodec
Decode the key output by redis.- Specified by:
decodeKey
in interfaceRedisCodec<String,String>
- Parameters:
bytes
- Raw bytes of the key, must not benull
.- 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 interfaceRedisCodec<String,String>
- Parameters:
bytes
- Raw bytes of the value, must not benull
.- 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 interfaceRedisCodec<String,String>
- Parameters:
key
- the key, may benull
.- 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 interfaceRedisCodec<String,String>
- Parameters:
value
- the value, may benull
.- Returns:
- The encoded value, never
null
.
-
-