Search in sources :

Example 1 with WireType

use of org.infinispan.protostream.descriptors.WireType in project protostream by infinispan.

the class ProtobufParser method parseMessage.

private void parseMessage(TagHandler tagHandler, Descriptor messageDescriptor, TagReader in) throws IOException {
    int tag;
    while ((tag = in.readTag()) != 0) {
        final int fieldNumber = WireType.getTagFieldNumber(tag);
        final WireType wireType = WireType.fromTag(tag);
        final FieldDescriptor fd = messageDescriptor != null ? messageDescriptor.findFieldByNumber(fieldNumber) : null;
        switch(wireType) {
            case LENGTH_DELIMITED:
                {
                    if (fd == null) {
                        byte[] value = in.readByteArray();
                        tagHandler.onTag(fieldNumber, null, value);
                    } else if (fd.getType() == Type.STRING) {
                        String value = in.readString();
                        tagHandler.onTag(fieldNumber, fd, value);
                    } else if (fd.getType() == Type.BYTES) {
                        byte[] value = in.readByteArray();
                        tagHandler.onTag(fieldNumber, fd, value);
                    } else if (fd.getType() == Type.MESSAGE) {
                        int length = in.readUInt32();
                        int oldLimit = in.pushLimit(length);
                        tagHandler.onStartNested(fieldNumber, fd);
                        parseMessage(tagHandler, fd.getMessageType(), in);
                        tagHandler.onEndNested(fieldNumber, fd);
                        in.checkLastTagWas(0);
                        in.popLimit(oldLimit);
                    }
                    break;
                }
            case START_GROUP:
                {
                    if (fd != null) {
                        tagHandler.onStartNested(fieldNumber, null);
                        parseMessage(tagHandler, null, in);
                        in.checkLastTagWas(WireType.makeTag(fieldNumber, WireType.WIRETYPE_END_GROUP));
                        tagHandler.onEndNested(fieldNumber, null);
                    } else {
                        tagHandler.onStartNested(fieldNumber, fd);
                        parseMessage(tagHandler, fd.getMessageType(), in);
                        in.checkLastTagWas(WireType.makeTag(fieldNumber, WireType.WIRETYPE_END_GROUP));
                        tagHandler.onEndNested(fieldNumber, fd);
                    }
                    break;
                }
            case FIXED32:
            case FIXED64:
            case VARINT:
                {
                    if (fd == null) {
                        if (wireType == WireType.FIXED32) {
                            tagHandler.onTag(fieldNumber, null, in.readFixed32());
                        } else if (wireType == WireType.FIXED64) {
                            tagHandler.onTag(fieldNumber, null, in.readFixed64());
                        } else {
                            tagHandler.onTag(fieldNumber, null, in.readUInt64());
                        }
                    } else {
                        Object value;
                        switch(fd.getType()) {
                            case DOUBLE:
                                value = in.readDouble();
                                break;
                            case FLOAT:
                                value = in.readFloat();
                                break;
                            case BOOL:
                                value = in.readBool();
                                break;
                            case INT32:
                                value = in.readInt32();
                                break;
                            case SFIXED32:
                                value = in.readSFixed32();
                                break;
                            case FIXED32:
                                value = in.readFixed32();
                                break;
                            case UINT32:
                                value = in.readUInt32();
                                break;
                            case SINT32:
                                value = in.readSInt32();
                                break;
                            case INT64:
                                value = in.readInt64();
                                break;
                            case UINT64:
                                value = in.readUInt64();
                                break;
                            case FIXED64:
                                value = in.readFixed64();
                                break;
                            case SFIXED64:
                                value = in.readSFixed64();
                                break;
                            case SINT64:
                                value = in.readSInt64();
                                break;
                            case ENUM:
                                value = in.readEnum();
                                break;
                            default:
                                throw new IOException("Unexpected field type " + fd.getType() + " for field " + fieldNumber);
                        }
                        tagHandler.onTag(fieldNumber, fd, value);
                    }
                    break;
                }
            default:
                throw new IOException("Found tag with invalid wire type : tag=" + tag + ", wireType=" + wireType);
        }
    }
}
Also used : WireType(org.infinispan.protostream.descriptors.WireType) IOException(java.io.IOException) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 2 with WireType

use of org.infinispan.protostream.descriptors.WireType in project protostream by infinispan.

the class UnknownFieldSetImpl method writeField.

/**
 * Serializes a field, including field number, and writes it to {@code output}.
 */
private void writeField(int tag, Deque<?> values, TagWriter output) throws IOException {
    final WireType wireType = WireType.fromTag(tag);
    final int fieldNumber = WireType.getTagFieldNumber(tag);
    switch(wireType) {
        case VARINT:
            for (long value : (Deque<Long>) values) {
                output.writeUInt64(fieldNumber, value);
            }
            break;
        case FIXED32:
            for (int value : (Deque<Integer>) values) {
                output.writeFixed32(fieldNumber, value);
            }
            break;
        case FIXED64:
            for (long value : (Deque<Long>) values) {
                output.writeFixed64(fieldNumber, value);
            }
            break;
        case LENGTH_DELIMITED:
            for (byte[] value : (Deque<byte[]>) values) {
                output.writeBytes(fieldNumber, value);
            }
            break;
        case START_GROUP:
            for (UnknownFieldSetImpl value : (Deque<UnknownFieldSetImpl>) values) {
                output.writeVarint32(tag);
                value.writeTo(output);
                output.writeTag(fieldNumber, WireType.WIRETYPE_END_GROUP);
            }
            break;
        default:
            throw new IllegalArgumentException("Tag " + tag + " has invalid wire type " + wireType);
    }
}
Also used : WireType(org.infinispan.protostream.descriptors.WireType) Deque(java.util.Deque) ArrayDeque(java.util.ArrayDeque)

Example 3 with WireType

use of org.infinispan.protostream.descriptors.WireType in project protostream by infinispan.

the class UnknownFieldSetImpl method readSingleField.

@Override
public boolean readSingleField(int tag, TagReader input) throws IOException {
    WireType wireType = WireType.fromTag(tag);
    switch(wireType) {
        case VARINT:
            getField(tag).addLast(input.readInt64());
            return true;
        case FIXED64:
            getField(tag).addLast(input.readFixed64());
            return true;
        case LENGTH_DELIMITED:
            getField(tag).addLast(input.readByteArray());
            return true;
        case START_GROUP:
            UnknownFieldSetImpl unknownFieldSet = new UnknownFieldSetImpl();
            unknownFieldSet.readAllFields(input);
            input.checkLastTagWas(WireType.makeTag(WireType.getTagFieldNumber(tag), WireType.WIRETYPE_END_GROUP));
            getField(tag).addLast(unknownFieldSet);
            return true;
        case END_GROUP:
            return false;
        case FIXED32:
            getField(tag).addLast(input.readFixed32());
            return true;
        default:
            throw new IOException("Protocol message tag " + tag + " has invalid wire type " + wireType);
    }
}
Also used : WireType(org.infinispan.protostream.descriptors.WireType) IOException(java.io.IOException)

Aggregations

WireType (org.infinispan.protostream.descriptors.WireType)3 IOException (java.io.IOException)2 ArrayDeque (java.util.ArrayDeque)1 Deque (java.util.Deque)1 FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)1