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