Skip to content

[Java] DTO's computeEncodedLength() will return invalid length for certain encodings #1072

Closed
@the-thing

Description

@the-thing

It is impossible to use String encodings with DTOs that encode string using different byte length and Unicode units - UTF-16, UTF-32 etc. This is problematic when using tryClaim or anything else that pre allocates the exact amount of bytes.

To recreate. Add/change custom UTF-16 field result in different estimated length and encoded length. Tests such as uk.co.real_logic.sbe.generation.java.DtoTest will fail.

<composite name="varUTF16Encoding">
    <type name="length" primitiveType="uint32" maxValue="1073741824"/>
    <type name="varData" primitiveType="uint8" length="0" characterEncoding="UTF-16"/>
</composite>

<sbe:message name="Car" id="1" description="Description of a basic Car">
    [...]
    <data name="activationCode" id="20" type="varUTF16Encoding"/>
</sbe:message>

Unfortunately this means that the string has to be converted to byte[] array using SBE encoding in the xml file for that field (e.g. extension.CarDecoder#activationCodeCharacterEncoding). It could be optimized by estimating string lengths for common charsets defined in java.nio.charset.StandardCharsets.

// encodedLength += this.activationCode.length();
encodedLength += this.activationCode.getBytes(CarDecoder.activationCodeCharacterEncoding()).length;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions