Search in sources :

Example 1 with IndexedElementContainerAdapter

use of org.infinispan.protostream.containers.IndexedElementContainerAdapter in project protostream by infinispan.

the class WrappedMessage method writeContainer.

private static void writeContainer(ImmutableSerializationContext ctx, TagWriter out, BaseMarshallerDelegate marshallerDelegate, Object container) throws IOException {
    BaseMarshaller containerMarshaller = marshallerDelegate.getMarshaller();
    String typeName = containerMarshaller.getTypeName();
    int typeId = mapTypeIdOut(typeName, ctx);
    if (typeId < 0) {
        out.writeString(WRAPPED_CONTAINER_TYPE_NAME, typeName);
    } else {
        out.writeUInt32(WRAPPED_CONTAINER_TYPE_ID, typeId);
    }
    int containerSize = ((ElementContainerAdapter) containerMarshaller).getNumElements(container);
    out.writeUInt32(WRAPPED_CONTAINER_SIZE, containerSize);
    ByteArrayOutputStreamEx buffer = new ByteArrayOutputStreamEx();
    TagWriterImpl nestedCtx = TagWriterImpl.newInstanceNoBuffer(ctx, buffer);
    marshallerDelegate.marshall(nestedCtx, null, container);
    nestedCtx.flush();
    out.writeBytes(WRAPPED_CONTAINER_MESSAGE, buffer.getByteBuffer());
    if (containerMarshaller instanceof IterableElementContainerAdapter) {
        Iterator elements = ((IterableElementContainerAdapter) containerMarshaller).getElements(container);
        for (int i = 0; i < containerSize; i++) {
            Object e = elements.next();
            writeMessage(ctx, out, e, true);
        }
        if (elements.hasNext()) {
            throw new IllegalStateException("Container number of elements mismatch");
        }
    } else if (containerMarshaller instanceof IndexedElementContainerAdapter) {
        IndexedElementContainerAdapter adapter = (IndexedElementContainerAdapter) containerMarshaller;
        for (int i = 0; i < containerSize; i++) {
            Object e = adapter.getElement(container, i);
            writeMessage(ctx, out, e, true);
        }
    } else {
        throw new IllegalStateException("Unknown container adapter kind : " + containerMarshaller.getJavaClass().getName());
    }
}
Also used : IndexedElementContainerAdapter(org.infinispan.protostream.containers.IndexedElementContainerAdapter) IterableElementContainerAdapter(org.infinispan.protostream.containers.IterableElementContainerAdapter) IndexedElementContainerAdapter(org.infinispan.protostream.containers.IndexedElementContainerAdapter) IterableElementContainerAdapter(org.infinispan.protostream.containers.IterableElementContainerAdapter) ElementContainerAdapter(org.infinispan.protostream.containers.ElementContainerAdapter) ByteArrayOutputStreamEx(org.infinispan.protostream.impl.ByteArrayOutputStreamEx) Iterator(java.util.Iterator) TagWriterImpl(org.infinispan.protostream.impl.TagWriterImpl)

Example 2 with IndexedElementContainerAdapter

use of org.infinispan.protostream.containers.IndexedElementContainerAdapter 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

ElementContainerAdapter (org.infinispan.protostream.containers.ElementContainerAdapter)2 IndexedElementContainerAdapter (org.infinispan.protostream.containers.IndexedElementContainerAdapter)2 IterableElementContainerAdapter (org.infinispan.protostream.containers.IterableElementContainerAdapter)2 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 ByteArrayOutputStreamEx (org.infinispan.protostream.impl.ByteArrayOutputStreamEx)1 SerializationContextImpl (org.infinispan.protostream.impl.SerializationContextImpl)1 TagReaderImpl (org.infinispan.protostream.impl.TagReaderImpl)1 TagWriterImpl (org.infinispan.protostream.impl.TagWriterImpl)1