Search in sources :

Example 1 with TagWriterImpl

use of org.infinispan.protostream.impl.TagWriterImpl 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 TagWriterImpl

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

the class ProtobufUtil method computeMessageSize.

public static <A> int computeMessageSize(ImmutableSerializationContext ctx, A t) throws IOException {
    TagWriterImpl out = TagWriterImpl.newInstance(ctx);
    write(ctx, out, t);
    return out.getWrittenBytes();
}
Also used : TagWriterImpl(org.infinispan.protostream.impl.TagWriterImpl)

Example 3 with TagWriterImpl

use of org.infinispan.protostream.impl.TagWriterImpl in project wildfly by wildfly.

the class DefaultProtoStreamWriter method writeObjectNoTag.

@Override
public void writeObjectNoTag(Object value) throws IOException {
    ImmutableSerializationContext context = this.getSerializationContext();
    ProtoStreamMarshaller<Object> marshaller = this.findMarshaller(value.getClass());
    OptionalInt size = this.size(marshaller, value);
    try (ByteBufferOutputStream output = new ByteBufferOutputStream(size)) {
        TagWriterImpl writer = size.isPresent() ? TagWriterImpl.newInstance(context, output, size.getAsInt()) : TagWriterImpl.newInstance(context, output);
        marshaller.writeTo(new DefaultProtoStreamWriter(writer), value);
        writer.flush();
        ByteBuffer buffer = output.getBuffer();
        int offset = buffer.arrayOffset();
        int length = buffer.limit() - offset;
        this.writeVarint32(length);
        this.writeRawBytes(buffer.array(), offset, length);
    }
}
Also used : ByteBufferOutputStream(org.wildfly.clustering.marshalling.spi.ByteBufferOutputStream) ImmutableSerializationContext(org.infinispan.protostream.ImmutableSerializationContext) OptionalInt(java.util.OptionalInt) TagWriterImpl(org.infinispan.protostream.impl.TagWriterImpl) ByteBuffer(java.nio.ByteBuffer)

Example 4 with TagWriterImpl

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

the class GeneratedMarshallerBase method writeNestedMessage.

/**
 * Invoked by generated code.
 */
protected final <T> void writeNestedMessage(BaseMarshallerDelegate<T> marshallerDelegate, ProtobufTagMarshaller.WriteContext ctx, int fieldNumber, T message) throws IOException {
    ByteArrayOutputStreamEx baos = new ByteArrayOutputStreamEx();
    TagWriterImpl nested = TagWriterImpl.newNestedInstance(ctx, baos);
    writeMessage(marshallerDelegate, nested, message);
    ctx.getWriter().writeBytes(fieldNumber, baos.getByteBuffer());
}
Also used : ByteArrayOutputStreamEx(org.infinispan.protostream.impl.ByteArrayOutputStreamEx) TagWriterImpl(org.infinispan.protostream.impl.TagWriterImpl)

Example 5 with TagWriterImpl

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

the class ProtobufUtil method computeWrappedMessageSize.

public static <A> int computeWrappedMessageSize(ImmutableSerializationContext ctx, A t) throws IOException {
    TagWriterImpl out = TagWriterImpl.newInstance(ctx);
    WrappedMessage.write(ctx, out, t);
    return out.getWrittenBytes();
}
Also used : TagWriterImpl(org.infinispan.protostream.impl.TagWriterImpl)

Aggregations

TagWriterImpl (org.infinispan.protostream.impl.TagWriterImpl)5 ByteArrayOutputStreamEx (org.infinispan.protostream.impl.ByteArrayOutputStreamEx)2 ByteBuffer (java.nio.ByteBuffer)1 Iterator (java.util.Iterator)1 OptionalInt (java.util.OptionalInt)1 ImmutableSerializationContext (org.infinispan.protostream.ImmutableSerializationContext)1 ElementContainerAdapter (org.infinispan.protostream.containers.ElementContainerAdapter)1 IndexedElementContainerAdapter (org.infinispan.protostream.containers.IndexedElementContainerAdapter)1 IterableElementContainerAdapter (org.infinispan.protostream.containers.IterableElementContainerAdapter)1 ByteBufferOutputStream (org.wildfly.clustering.marshalling.spi.ByteBufferOutputStream)1