Functions
fixCodecSize
fixCodecSize<
TFrom
,TTo
,TSize
>(codec
,fixedBytes
):FixedSizeCodec
<TFrom
,TTo
,TSize
>
Creates a fixed-size codec from a given codec.
The resulting codec ensures that both encoding and decoding operate on a fixed number of bytes. When encoding:
- If the encoded value is larger than
fixedBytes
, it is truncated. - If it is smaller, it is padded with trailing zeroes.
- If it is exactly
fixedBytes
, it remains unchanged.
When decoding:
- Exactly
fixedBytes
bytes are read from the input. - If the nested decoder has a smaller fixed size, bytes are truncated or padded as necessary.
Type Parameters
Type Parameter | Description |
---|---|
TFrom | The type of the value to encode. |
TTo | The type of the decoded value. |
TSize extends number | The fixed size of the encoded value in bytes. |
Parameters
Parameter | Type | Description |
---|---|---|
codec | Codec <TFrom , TTo > | The codec to wrap into a fixed-size codec. |
fixedBytes | TSize | The fixed number of bytes to read/write. |
Returns
FixedSizeCodec
<TFrom
, TTo
, TSize
>
A FixedSizeCodec
that ensures both encoding and decoding conform to a fixed size.
Example
Remarks
If you only need to enforce a fixed size for encoding, use fixEncoderSize. If you only need to enforce a fixed size for decoding, use fixDecoderSize.