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());
}
}
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();
}
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);
}
}
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());
}
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();
}
Aggregations