Package io.hekate.codec
Interface CodecService
-
- All Superinterfaces:
Service
public interface CodecService extends Service
« start hereMain entry point to data serialization API.Overview
CodecService
represents theService
interface adaptor forCodecFactory
in order to make it easily accessible viaHekate.codec()
method. All data encoding/decoding operations are delegated to theCodecFactory
instance that is registered via theHekateBootstrap.setDefaultCodec(CodecFactory)
method.Accessing the Service
Instances of this service can be obtained via
Hekate.codec()
method as shown in the example below:CodecService codecService = hekate.codec();
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description <T> CodecFactory<T>
codecFactory()
Returns an underlying codec factory (seeHekateBootstrap.setDefaultCodec(CodecFactory)
).Object
decode(byte[] bytes)
Decodes an object from the specified array of bytes.Object
decode(byte[] bytes, int offset, int limit)
Decodes an object from the specified array of bytes.<T> T
decode(byte[] bytes, int offset, int limit, DecodeFunction<T> decoder)
Decodes an object from the specified byte array by using the supplied decode function.<T> T
decode(byte[] bytes, DecodeFunction<T> decoder)
Decodes an object from the specified byte array by using the supplied decode function.byte[]
encode(Object obj)
Encodes the specified object into an array of bytes.<T> byte[]
encode(T obj, EncodeFunction<T> encoder)
Encodes the specified object via the specified function and returns a byte array of encoded data.<T> EncoderDecoder<T>
forFactory(CodecFactory<T> codecFactory)
Returns encoder/decoder for the codec factory.<T> EncoderDecoder<T>
forType(Class<T> type)
Returns encoder/decoder for the specified type.<T> EncoderDecoder<T>
forType(Class<T> type, EncodeFunction<T> encode, DecodeFunction<T> decode)
Returns encoder/decoder for the specified type and encoding/decoding functions.
-
-
-
Method Detail
-
codecFactory
<T> CodecFactory<T> codecFactory()
Returns an underlying codec factory (seeHekateBootstrap.setDefaultCodec(CodecFactory)
).- Type Parameters:
T
- Type that should be supported by the returned codec factory.- Returns:
- Codec factory.
-
forType
<T> EncoderDecoder<T> forType(Class<T> type)
Returns encoder/decoder for the specified type.Such encoder/decoder uses the underlying
codecFactory()
of this service to perform encoding/decoding operations.- Type Parameters:
T
- Data type.- Parameters:
type
- Data type.- Returns:
- Encoder/decoder for the specified type.
- See Also:
HekateBootstrap.setDefaultCodec(CodecFactory)
-
forType
<T> EncoderDecoder<T> forType(Class<T> type, EncodeFunction<T> encode, DecodeFunction<T> decode)
Returns encoder/decoder for the specified type and encoding/decoding functions.- Type Parameters:
T
- Data type.- Parameters:
type
- Data type.encode
- Encode function.decode
- Decode function.- Returns:
- Encoder/decoder.
-
forFactory
<T> EncoderDecoder<T> forFactory(CodecFactory<T> codecFactory)
Returns encoder/decoder for the codec factory.- Type Parameters:
T
- Data type.- Parameters:
codecFactory
- Codec factory.- Returns:
- Encoder/decoder for the specified factory.
-
encode
<T> byte[] encode(T obj, EncodeFunction<T> encoder) throws CodecException
Encodes the specified object via the specified function and returns a byte array of encoded data.- Type Parameters:
T
- Object type.- Parameters:
obj
- Object to encode (can benull
, in such case theencoder
function's parameter will benull
too).encoder
- Encoder function.- Returns:
- Byte array of encoded data.
- Throws:
CodecException
- If object couldn't be decoded.- See Also:
decode(byte[], DecodeFunction)
-
encode
byte[] encode(Object obj) throws CodecException
Encodes the specified object into an array of bytes.- Parameters:
obj
- Object.- Returns:
- Bytes.
- Throws:
CodecException
- Signals encoding failure.- See Also:
decode(byte[])
-
decode
<T> T decode(byte[] bytes, DecodeFunction<T> decoder) throws CodecException
Decodes an object from the specified byte array by using the supplied decode function.- Type Parameters:
T
- Decoded object type.- Parameters:
bytes
- Bytes.decoder
- Decode function.- Returns:
- Decoded object.
- Throws:
CodecException
- If object couldn't be decoded.- See Also:
encode(Object, EncodeFunction)
-
decode
Object decode(byte[] bytes, int offset, int limit) throws CodecException
Decodes an object from the specified array of bytes.- Parameters:
bytes
- Bytes.offset
- Offset of the first byte to read.limit
- Maximum number of bytes to read.- Returns:
- Decoded object.
- Throws:
CodecException
- Signals decoding failure.
-
decode
Object decode(byte[] bytes) throws CodecException
Decodes an object from the specified array of bytes.- Parameters:
bytes
- Bytes.- Returns:
- Decoded object.
- Throws:
CodecException
- Signals decoding failure.- See Also:
encode(Object)
-
decode
<T> T decode(byte[] bytes, int offset, int limit, DecodeFunction<T> decoder) throws CodecException
Decodes an object from the specified byte array by using the supplied decode function.- Type Parameters:
T
- Decoded object type.- Parameters:
bytes
- Bytes.offset
- Offset tolimit
- Maximum number of bytes to read.decoder
- Decode function.- Returns:
- Decoded object.
- Throws:
CodecException
- If object couldn't be decoded.
-
-