Search in sources :

Example 1 with TagReaderImpl

use of org.infinispan.protostream.impl.TagReaderImpl in project protostream by infinispan.

the class WrappedMessage method readMessage.

private static <T> T readMessage(ImmutableSerializationContext ctx, TagReader in, boolean nulls) throws IOException {
    String typeName = null;
    Integer typeId = null;
    int enumValue = -1;
    byte[] messageBytes = null;
    Object value = null;
    int fieldCount = 0;
    int expectedFieldCount = 1;
    int tag;
    out: while ((tag = in.readTag()) != 0) {
        fieldCount++;
        switch(tag) {
            case WRAPPED_CONTAINER_SIZE << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
            case WRAPPED_CONTAINER_TYPE_ID << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
            case WRAPPED_CONTAINER_TYPE_NAME << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_LENGTH_DELIMITED:
            case WRAPPED_CONTAINER_MESSAGE << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_LENGTH_DELIMITED:
                {
                    expectedFieldCount = 1;
                    value = readContainer(ctx, in, tag);
                    break out;
                }
            case WRAPPED_EMPTY << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                {
                    if (!nulls) {
                        throw new IllegalStateException("Encountered a null message but nulls are not accepted");
                    }
                    expectedFieldCount = 1;
                    // We ignore the actual boolean value! Will be returning null anyway.
                    in.readBool();
                    break out;
                }
            case WRAPPED_TYPE_NAME << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_LENGTH_DELIMITED:
                {
                    expectedFieldCount = 2;
                    typeName = in.readString();
                    break;
                }
            case WRAPPED_TYPE_ID << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                {
                    expectedFieldCount = 2;
                    typeId = mapTypeIdIn(in.readInt32(), ctx);
                    break;
                }
            case WRAPPED_ENUM << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                {
                    expectedFieldCount = 2;
                    enumValue = in.readEnum();
                    break;
                }
            case WRAPPED_MESSAGE << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_LENGTH_DELIMITED:
                {
                    expectedFieldCount = 2;
                    messageBytes = in.readByteArray();
                    break;
                }
            case WRAPPED_STRING << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_LENGTH_DELIMITED:
                {
                    expectedFieldCount = 1;
                    value = in.readString();
                    break out;
                }
            case WRAPPED_CHAR << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                {
                    expectedFieldCount = 1;
                    value = (char) in.readInt32();
                    break out;
                }
            case WRAPPED_SHORT << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                {
                    expectedFieldCount = 1;
                    value = (short) in.readInt32();
                    break out;
                }
            case WRAPPED_BYTE << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                {
                    expectedFieldCount = 1;
                    value = (byte) in.readInt32();
                    break out;
                }
            case WRAPPED_DATE_MILLIS << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                {
                    expectedFieldCount = 1;
                    value = new Date(in.readInt64());
                    break out;
                }
            case WRAPPED_INSTANT_SECONDS << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                {
                    expectedFieldCount = 2;
                    long seconds = in.readInt64();
                    value = value == null ? Instant.ofEpochSecond(seconds, 0) : Instant.ofEpochSecond(seconds, ((Instant) value).getNano());
                    break;
                }
            case WRAPPED_INSTANT_NANOS << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                {
                    expectedFieldCount = 2;
                    int nanos = in.readInt32();
                    value = value == null ? Instant.ofEpochSecond(0, nanos) : Instant.ofEpochSecond(((Instant) value).getEpochSecond(), nanos);
                    break;
                }
            case WRAPPED_BYTES << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_LENGTH_DELIMITED:
                {
                    expectedFieldCount = 1;
                    value = in.readByteArray();
                    break out;
                }
            case WRAPPED_BOOL << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                {
                    expectedFieldCount = 1;
                    value = in.readBool();
                    break out;
                }
            case WRAPPED_DOUBLE << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_FIXED64:
                {
                    expectedFieldCount = 1;
                    value = in.readDouble();
                    break out;
                }
            case WRAPPED_FLOAT << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_FIXED32:
                {
                    expectedFieldCount = 1;
                    value = in.readFloat();
                    break out;
                }
            case WRAPPED_FIXED32 << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_FIXED32:
                {
                    expectedFieldCount = 1;
                    value = in.readFixed32();
                    break out;
                }
            case WRAPPED_SFIXED32 << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_FIXED32:
                {
                    expectedFieldCount = 1;
                    value = in.readSFixed32();
                    break out;
                }
            case WRAPPED_FIXED64 << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_FIXED64:
                {
                    expectedFieldCount = 1;
                    value = in.readFixed64();
                    break out;
                }
            case WRAPPED_SFIXED64 << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_FIXED64:
                {
                    expectedFieldCount = 1;
                    value = in.readSFixed64();
                    break out;
                }
            case WRAPPED_INT64 << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                {
                    expectedFieldCount = 1;
                    value = in.readInt64();
                    break out;
                }
            case WRAPPED_UINT64 << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                {
                    expectedFieldCount = 1;
                    value = in.readUInt64();
                    break out;
                }
            case WRAPPED_SINT64 << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                {
                    expectedFieldCount = 1;
                    value = in.readSInt64();
                    break out;
                }
            case WRAPPED_INT32 << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                {
                    expectedFieldCount = 1;
                    value = in.readInt32();
                    break out;
                }
            case WRAPPED_UINT32 << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                {
                    expectedFieldCount = 1;
                    value = in.readUInt32();
                    break out;
                }
            case WRAPPED_SINT32 << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                {
                    expectedFieldCount = 1;
                    value = in.readSInt32();
                    break out;
                }
            default:
                throw new IllegalStateException("Unexpected tag : " + tag + " (Field number : " + WireType.getTagFieldNumber(tag) + ", Wire type : " + WireType.getTagWireType(tag) + ")");
        }
    }
    if (value == null && typeName == null && typeId == null && messageBytes == null) {
        return null;
    }
    if (value != null) {
        if (fieldCount != expectedFieldCount) {
            throw new IOException("Invalid WrappedMessage encoding.");
        }
        return (T) value;
    }
    if (typeName == null && typeId == null || typeName != null && typeId != null || fieldCount != 2) {
        throw new IOException("Invalid WrappedMessage encoding.");
    }
    if (typeId != null) {
        typeName = ctx.getDescriptorByTypeId(typeId).getFullName();
    }
    BaseMarshallerDelegate marshallerDelegate = ((SerializationContextImpl) ctx).getMarshallerDelegate(typeName);
    if (messageBytes != null) {
        // it's a Message type
        TagReaderImpl nestedInput = TagReaderImpl.newInstance(ctx, messageBytes);
        return (T) marshallerDelegate.unmarshall(nestedInput, null);
    } else {
        // it's an Enum
        EnumMarshaller marshaller = (EnumMarshaller) marshallerDelegate.getMarshaller();
        T e = (T) marshaller.decode(enumValue);
        if (e == null) {
            // Unknown enum value cause by schema evolution. We cannot handle data loss here so we throw!
            throw new IOException("Unknown enum value " + enumValue + " for Protobuf enum type " + typeName);
        }
        return e;
    }
}
Also used : TagReaderImpl(org.infinispan.protostream.impl.TagReaderImpl) IOException(java.io.IOException) Date(java.util.Date) SerializationContextImpl(org.infinispan.protostream.impl.SerializationContextImpl) BaseMarshallerDelegate(org.infinispan.protostream.impl.BaseMarshallerDelegate)

Example 2 with TagReaderImpl

use of org.infinispan.protostream.impl.TagReaderImpl in project protostream by infinispan.

the class WrappedMessage method readContainer.

private static Object readContainer(ImmutableSerializationContext ctx, TagReader in, int tag) throws IOException {
    int containerSize = -1;
    String containerTypeName = null;
    Integer containerTypeId = null;
    byte[] containerMessage = null;
    int fieldCount = 0;
    while (tag != 0) {
        switch(tag) {
            case WRAPPED_CONTAINER_SIZE << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                containerSize = in.readInt32();
                break;
            case WRAPPED_CONTAINER_TYPE_NAME << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_LENGTH_DELIMITED:
                {
                    containerTypeName = in.readString();
                    break;
                }
            case WRAPPED_CONTAINER_TYPE_ID << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_VARINT:
                {
                    containerTypeId = mapTypeIdIn(in.readInt32(), ctx);
                    break;
                }
            case WRAPPED_CONTAINER_MESSAGE << WireType.TAG_TYPE_NUM_BITS | WireType.WIRETYPE_LENGTH_DELIMITED:
                containerMessage = in.readByteArray();
                break;
            default:
                throw new IllegalStateException("Unexpected tag : " + tag + " (Field number : " + WireType.getTagFieldNumber(tag) + ", Wire type : " + WireType.getTagWireType(tag) + ")");
        }
        if (++fieldCount == 3) {
            break;
        }
        tag = in.readTag();
    }
    if (fieldCount != 3 || containerSize < 0 || containerMessage == null || containerTypeId == null && containerTypeName == null || containerTypeId != null && containerTypeName != null) {
        throw new IOException("Invalid WrappedMessage encoding.");
    }
    if (containerTypeId != null) {
        containerTypeName = ctx.getDescriptorByTypeId(containerTypeId).getFullName();
    }
    BaseMarshallerDelegate<?> marshallerDelegate = ((SerializationContextImpl) ctx).getMarshallerDelegate(containerTypeName);
    BaseMarshaller<?> containerMarshaller = marshallerDelegate.getMarshaller();
    if (!(containerMarshaller instanceof ElementContainerAdapter)) {
        throw new IllegalStateException("The unmarshaller is not a container adapter : " + containerMarshaller.getJavaClass().getName());
    }
    TagReaderImpl nestedInput = TagReaderImpl.newNestedInstance((ProtobufTagMarshaller.ReadContext) in, containerMessage);
    // pass the size to the marshaller of the container object
    nestedInput.setParam(CONTAINER_SIZE_CONTEXT_PARAM, containerSize);
    Object container = marshallerDelegate.unmarshall(nestedInput, null);
    if (container == null) {
        throw new IllegalStateException("The unmarshalled container must not be null");
    }
    containerMessage = null;
    nestedInput = null;
    if (containerMarshaller instanceof IterableElementContainerAdapter) {
        IterableElementContainerAdapter adapter = (IterableElementContainerAdapter) containerMarshaller;
        for (int i = 0; i < containerSize; i++) {
            Object e = readMessage(ctx, in, true);
            adapter.appendElement(container, e);
        }
    } else if (containerMarshaller instanceof IndexedElementContainerAdapter) {
        IndexedElementContainerAdapter adapter = (IndexedElementContainerAdapter) containerMarshaller;
        for (int i = 0; i < containerSize; i++) {
            Object e = readMessage(ctx, in, true);
            adapter.setElement(container, i, e);
        }
    } else {
        throw new IllegalStateException("Unknown container adapter kind : " + containerMarshaller.getJavaClass().getName());
    }
    return container;
}
Also used : IndexedElementContainerAdapter(org.infinispan.protostream.containers.IndexedElementContainerAdapter) IterableElementContainerAdapter(org.infinispan.protostream.containers.IterableElementContainerAdapter) TagReaderImpl(org.infinispan.protostream.impl.TagReaderImpl) IOException(java.io.IOException) SerializationContextImpl(org.infinispan.protostream.impl.SerializationContextImpl) IndexedElementContainerAdapter(org.infinispan.protostream.containers.IndexedElementContainerAdapter) IterableElementContainerAdapter(org.infinispan.protostream.containers.IterableElementContainerAdapter) ElementContainerAdapter(org.infinispan.protostream.containers.ElementContainerAdapter)

Aggregations

IOException (java.io.IOException)2 SerializationContextImpl (org.infinispan.protostream.impl.SerializationContextImpl)2 TagReaderImpl (org.infinispan.protostream.impl.TagReaderImpl)2 Date (java.util.Date)1 ElementContainerAdapter (org.infinispan.protostream.containers.ElementContainerAdapter)1 IndexedElementContainerAdapter (org.infinispan.protostream.containers.IndexedElementContainerAdapter)1 IterableElementContainerAdapter (org.infinispan.protostream.containers.IterableElementContainerAdapter)1 BaseMarshallerDelegate (org.infinispan.protostream.impl.BaseMarshallerDelegate)1