Search in sources :

Example 1 with UnknownFieldSet

use of org.infinispan.protostream.UnknownFieldSet in project protostream by infinispan.

the class EnumMarshallerDelegate method unmarshall.

@Override
public T unmarshall(ProtobufTagMarshaller.ReadContext ctx, FieldDescriptor fd) throws IOException {
    final int expectedTag = fd.getWireTag();
    int enumValue;
    ProtoStreamReaderImpl reader = ((TagReaderImpl) ctx).getProtoStreamReader();
    UnknownFieldSet unknownFieldSet = reader.getUnknownFieldSet();
    Object value = unknownFieldSet.consumeTag(expectedTag);
    if (value != null) {
        enumValue = ((Long) value).intValue();
    } else {
        TagReader in = ctx.getReader();
        while (true) {
            int tag = in.readTag();
            if (tag == 0) {
                return null;
            }
            if (tag == expectedTag) {
                enumValue = in.readEnum();
                break;
            }
            unknownFieldSet.readSingleField(tag, in);
        }
    }
    return decode(expectedTag, enumValue, unknownFieldSet);
}
Also used : UnknownFieldSet(org.infinispan.protostream.UnknownFieldSet) TagReader(org.infinispan.protostream.TagReader)

Example 2 with UnknownFieldSet

use of org.infinispan.protostream.UnknownFieldSet in project protostream by infinispan.

the class MessageMarshallerDelegate method unmarshall.

@Override
public T unmarshall(ProtobufTagMarshaller.ReadContext ctx, FieldDescriptor fieldDescriptor) throws IOException {
    ProtoStreamReaderImpl reader = ((TagReaderImpl) ctx).getProtoStreamReader();
    ProtoStreamReaderImpl.ReadMessageContext messageContext = reader.enterContext(fieldDescriptor, messageDescriptor, (TagReaderImpl) ctx);
    T message = marshaller.readFrom(reader);
    UnknownFieldSet unknownFieldSet = messageContext.unknownFieldSet;
    unknownFieldSet.readAllFields(ctx.getReader());
    if (unknownFieldSetHandler != null && !unknownFieldSet.isEmpty()) {
        unknownFieldSetHandler.setUnknownFieldSet(message, unknownFieldSet);
    }
    // check that all required fields were seen in the stream, even if not actually read (because are unknown)
    for (FieldDescriptor fd : fieldDescriptors) {
        if (fd.isRequired() && !messageContext.isFieldMarked(fd.getNumber()) && !unknownFieldSet.hasTag(fd.getWireTag())) {
            throw new IOException("Required field \"" + fd.getFullName() + "\" was not encountered in the stream");
        }
    }
    reader.exitContext();
    return message;
}
Also used : IOException(java.io.IOException) UnknownFieldSet(org.infinispan.protostream.UnknownFieldSet) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 3 with UnknownFieldSet

use of org.infinispan.protostream.UnknownFieldSet in project protostream by infinispan.

the class MessageMarshallerDelegate method marshall.

@Override
public void marshall(ProtobufTagMarshaller.WriteContext ctx, FieldDescriptor fieldDescriptor, T message) throws IOException {
    ProtoStreamWriterImpl writer = ((TagWriterImpl) ctx).getProtoStreamWriter();
    ProtoStreamWriterImpl.WriteMessageContext messageContext = writer.enterContext(fieldDescriptor, messageDescriptor, (TagWriterImpl) ctx);
    marshaller.writeTo(writer, message);
    UnknownFieldSet unknownFieldSet = unknownFieldSetHandler != null ? unknownFieldSetHandler.getUnknownFieldSet(message) : null;
    if (unknownFieldSet != null && !unknownFieldSet.isEmpty()) {
        // validate that none of the unknown fields are actually declared by the known descriptor
        for (FieldDescriptor fd : fieldDescriptors) {
            if (unknownFieldSet.hasTag(fd.getWireTag())) {
                throw new IOException("Field " + fd.getFullName() + " is a known field so it is illegal to be present in the unknown field set");
            }
        }
        // write the unknown fields
        unknownFieldSet.writeTo(messageContext.out);
    }
    // validate that all the required fields were written either by the marshaller or by the UnknownFieldSet
    for (FieldDescriptor fd : fieldDescriptors) {
        if (fd.isRequired() && !messageContext.isFieldMarked(fd.getNumber()) && (unknownFieldSet == null || !unknownFieldSet.hasTag(fd.getWireTag()))) {
            throw new IllegalStateException("Required field \"" + fd.getFullName() + "\" should have been written by a calling a suitable method of " + MessageMarshaller.ProtoStreamWriter.class.getCanonicalName());
        }
    }
    writer.exitContext();
}
Also used : IOException(java.io.IOException) MessageMarshaller(org.infinispan.protostream.MessageMarshaller) UnknownFieldSet(org.infinispan.protostream.UnknownFieldSet) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Aggregations

UnknownFieldSet (org.infinispan.protostream.UnknownFieldSet)3 IOException (java.io.IOException)2 FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)2 MessageMarshaller (org.infinispan.protostream.MessageMarshaller)1 TagReader (org.infinispan.protostream.TagReader)1