Skip to content

Incorrect working in systems with constant msgpack types headers #870

Open
@nickkkccc

Description

@nickkkccc

Problem description

Let's imagine that I have a system that accepts only numbers encoded in UINT16.

Using the API with automatic header selection, it is impossible to encode values from 0 to 2^8-1 in UINT16 because MessagePacker will offer the types UINT8, INT8, NEGFIXINT, POSFIXINT. This will happen until the number exceeds the ranges UINT8, INT8, NEGFIXINT, POSFIXINT.

Suggestions for API improvements for statically typed systems

I suggest introducing an API in which the client can choose the type of encoded header.
For example:

    public MessagePacker packUINT(final short b, final MessageFormat type)
        throws IOException
    {
        switch (type) {
            case UINT8:
                writeByteAndByte(UINT8, (byte) b);
                break;
            case UINT16:
                writeByteAndShort(UINT16, b);
                break;
            case UINT32:
                writeByteAndInt(UINT32, b);
                break;
            case UINT64:
                writeByteAndLong(UINT64, b);
                break;
            default: 
                throw new MessagePackException("Cannot convert '" + type + "' to UINT family types.");
        }
        return this;
    }

Here, a method should be understood as a set of overloaded methods with short, int, long, BigInteger value arguments . Conversion based on narrowing casting

A similar solution for FLOAT32/64 types, INT8,16,32,64 families.
The code has already been written, it remains to send a PR

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