offsetCodec
offsetCodec<
TCodec
>(codec
,config
):TCodec
Moves the offset of a given codec before and/or after encoding and decoding.
This function allows a codec to encode and decode values at custom offsets within a byte array. It modifies both the pre-offset (where encoding/decoding starts) and the post-offset (where the next operation should continue).
This is particularly useful when working with structured binary formats that require skipping reserved bytes, inserting padding, or aligning fields at specific locations.
Type Parameters
Type Parameter |
---|
TCodec extends AnyCodec |
Parameters
Parameter | Type | Description |
---|---|---|
codec | TCodec | The codec to adjust. |
config | OffsetConfig | An object specifying how the offset should be modified. |
Returns
TCodec
A new codec with adjusted offsets.
Examples
Moving the pre-offset forward by 2 bytes when encoding and decoding.
Moving the post-offset forward by 2 bytes when encoding and decoding.
Using wrapBytes
to loop around negative offsets.
Remarks
If you only need to adjust offsets for encoding, use offsetEncoder. If you only need to adjust offsets for decoding, use offsetDecoder.