org.msgpack.core.MessageFormat Java Examples

The following examples show how to use org.msgpack.core.MessageFormat. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Message.java    From locust4j with MIT License 4 votes vote down vote up
public Message(byte[] bytes) throws IOException {
    MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bytes);

    int arrayHeader = unpacker.unpackArrayHeader();
    this.type = unpacker.unpackString();

    // unpack data
    if (unpacker.getNextFormat() != MessageFormat.NIL) {
        int mapSize = unpacker.unpackMapHeader();
        this.data = new HashMap<>(6);
        while (mapSize > 0) {
            String key = null;
            // unpack key
            if (unpacker.getNextFormat() == MessageFormat.NIL) {
                unpacker.unpackNil();
            } else {
                key = unpacker.unpackString();
            }
            // unpack value
            MessageFormat messageFormat = unpacker.getNextFormat();
            Object value;

            switch (messageFormat.getValueType()) {
                case BOOLEAN:
                    value = unpacker.unpackBoolean();
                    break;
                case FLOAT:
                    value = unpacker.unpackFloat();
                    break;
                case INTEGER:
                    value = unpacker.unpackInt();
                    break;
                case NIL:
                    value = null;
                    unpacker.unpackNil();
                    break;
                case STRING:
                    value = unpacker.unpackString();
                    break;
                default:
                    throw new IOException("Message received unsupported type: " + messageFormat.getValueType());
            }
            if (null != key) {
                this.data.put(key, value);
            }
            mapSize--;
        }

    } else {
        unpacker.unpackNil();
        this.data = null;
    }
    if (unpacker.getNextFormat() != MessageFormat.NIL) {
        this.nodeID = unpacker.unpackString();
    } else {
        unpacker.unpackNil();
        this.nodeID = null;
    }
    unpacker.close();
}
 
Example #2
Source File: MsgpackSerializer.java    From gridgo with MIT License 4 votes vote down vote up
private BValue unpackValue(MessageFormat format, MessageUnpacker unpacker) throws IOException {
    var value = this.getFactory().newValue();
    switch (format.getValueType()) {
    case NIL:
        unpacker.unpackNil();
        break;
    case BINARY:
        int len = unpacker.unpackBinaryHeader();
        value.setData(unpacker.readPayload(len));
        break;
    case BOOLEAN:
        value.setData(unpacker.unpackBoolean());
        break;
    case FLOAT:
        value.setData(format == MessageFormat.FLOAT64 //
                ? unpacker.unpackDouble() //
                : unpacker.unpackFloat());
        break;
    case INTEGER:
        switch (format) {
        case INT8:
            value.setData(unpacker.unpackByte());
            break;
        case INT16:
        case UINT8:
            value.setData(unpacker.unpackShort());
            break;
        case UINT32:
        case INT64:
        case UINT64:
            value.setData(unpacker.unpackLong());
            break;
        default:
            value.setData(unpacker.unpackInt());
            break;
        }
        break;
    case STRING:
        value.setData(unpacker.unpackString());
        break;
    default:
        throw new InvalidTypeException("Cannot unpack value from data format: " + format);
    }
    return value;
}