Search in sources :

Example 1 with TagReader

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

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

the class ProtoStreamReaderImpl method readNestedObject.

/**
 * Read an Object or an Enum.
 *
 * @param length the actual length of the nested object or -1 if the length should be read from the stream
 */
private <A> A readNestedObject(FieldDescriptor fd, Class<A> clazz, ProtobufTagMarshaller.ReadContext ctx, int length) throws IOException {
    BaseMarshallerDelegate<A> marshallerDelegate = serCtx.getMarshallerDelegate(clazz);
    TagReader in = ctx.getReader();
    A a;
    if (fd.getType() == Type.GROUP) {
        a = marshallerDelegate.unmarshall(ctx, fd);
        in.checkLastTagWas(WireType.makeTag(fd.getNumber(), WireType.WIRETYPE_END_GROUP));
    } else if (fd.getType() == Type.MESSAGE) {
        if (length < 0) {
            length = in.readUInt32();
        }
        int oldLimit = in.pushLimit(length);
        a = marshallerDelegate.unmarshall(ctx, fd);
        in.checkLastTagWas(0);
        in.popLimit(oldLimit);
    } else {
        throw new IllegalArgumentException("Declared field type is not a message or an enum : " + fd.getFullName());
    }
    return a;
}
Also used : TagReader(org.infinispan.protostream.TagReader)

Example 3 with TagReader

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

the class FullBufferReadTest method illegalStateExceptionOnMixingReadWithFullBuffer.

@Test
public void illegalStateExceptionOnMixingReadWithFullBuffer() throws Exception {
    SerializationContext ctx = createContext();
    FileDescriptorSource fileDescriptorSource = new FileDescriptorSource().addProtoFile("file.proto", file);
    ctx.registerProtoFiles(fileDescriptorSource);
    class MockMarshallerFuncs implements MarshallerFuncs<X> {

        public byte[] actualBytes = null;

        @Override
        public X read(ReadContext rc) throws IOException {
            TagReader r = rc.getReader();
            // calling any tag or field read prior to fullBufferArray should call IllegalStateException
            r.readTag();
            actualBytes = r.fullBufferArray();
            return null;
        }

        @Override
        public void write(WriteContext wc, X p) throws IOException {
            TagWriter w = wc.getWriter();
            w.writeInt32(1, p.f1);
            w.writeInt64(2, p.f2);
        }
    }
    MockMarshallerFuncs mockMarshallerFuncs = new MockMarshallerFuncs();
    ctx.registerMarshallerProvider(new MockProtobufMarshaller<>(X.class, "test.X", mockMarshallerFuncs));
    byte[] fullMsgBytes = ProtobufUtil.toWrappedByteArray(ctx, new X(1234, 4321L));
    try {
        ProtobufUtil.fromWrappedByteArray(ctx, fullMsgBytes);
        fail("IllegalStateException expected");
    } catch (IllegalStateException e) {
        assertEquals("fullBufferArray in marshaller can only be used on an unprocessed buffer", e.getMessage());
        assertNull(mockMarshallerFuncs.actualBytes);
    }
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) ReadContext(org.infinispan.protostream.ProtobufTagMarshaller.ReadContext) TagWriter(org.infinispan.protostream.TagWriter) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) TagReader(org.infinispan.protostream.TagReader) WriteContext(org.infinispan.protostream.ProtobufTagMarshaller.WriteContext) Test(org.junit.Test)

Example 4 with TagReader

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

the class FullBufferReadTest method testFullArrayInputStreamMarshaller.

@Test
public void testFullArrayInputStreamMarshaller() throws Exception {
    SerializationContext ctx = createContext();
    FileDescriptorSource fileDescriptorSource = new FileDescriptorSource().addProtoFile("file.proto", file);
    ctx.registerProtoFiles(fileDescriptorSource);
    class MockMarshallerFuncs implements MarshallerFuncs<X> {

        public InputStream actualStream = null;

        public boolean isInputStream = false;

        @Override
        public X read(ReadContext rc) throws IOException {
            TagReader r = rc.getReader();
            isInputStream = r.isInputStream();
            actualStream = r.fullBufferInputStream();
            return null;
        }

        @Override
        public void write(WriteContext wc, X p) throws IOException {
            TagWriter w = wc.getWriter();
            w.writeInt32(1, p.f1);
            w.writeInt64(2, p.f2);
        }
    }
    MockMarshallerFuncs mockMarshallerFuncs = new MockMarshallerFuncs();
    ctx.registerMarshallerProvider(new MockProtobufMarshaller<>(X.class, "test.X", mockMarshallerFuncs));
    byte[] fullMsgBytes = ProtobufUtil.toWrappedByteArray(ctx, new X(1234, 4321L));
    InputStream in = new ByteArrayInputStream(fullMsgBytes);
    ProtobufUtil.fromWrappedStream(ctx, in);
    assertNotNull(mockMarshallerFuncs.actualStream);
    assertEquals(6, mockMarshallerFuncs.actualStream.available());
    // assertTrue(mockMarshallerFuncs.isInputStream); // Currently always false - the InputStream appears to be converted to a byte array decoder after WrappedMessage processed
    byte[] actualBytes = new byte[mockMarshallerFuncs.actualStream.available()];
    mockMarshallerFuncs.actualStream.read(actualBytes);
    byte[] expectedBytes = { 8, -46, 9, 16, -31, 33 };
    assertNotNull(expectedBytes);
    assertTrue(Arrays.equals(actualBytes, expectedBytes));
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TagReader(org.infinispan.protostream.TagReader) WriteContext(org.infinispan.protostream.ProtobufTagMarshaller.WriteContext) ByteArrayInputStream(java.io.ByteArrayInputStream) ReadContext(org.infinispan.protostream.ProtobufTagMarshaller.ReadContext) TagWriter(org.infinispan.protostream.TagWriter) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) Test(org.junit.Test)

Example 5 with TagReader

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

the class ProtoStreamReaderImpl method readPrimitive.

private Object readPrimitive(String fieldName, JavaType javaType) throws IOException {
    final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
    final Type type = fd.getType();
    if (type == Type.ENUM || type == Type.GROUP || type == Type.MESSAGE) {
        throw new IllegalArgumentException("Declared field type is not a primitive : " + fd.getFullName());
    }
    if (fd.getJavaType() != javaType) {
        throw new IllegalArgumentException("Declared field type is not of the expected type : " + fd.getFullName());
    }
    checkFieldRead(fd, false);
    final int expectedTag = fd.getWireTag();
    Object o = messageContext.unknownFieldSet.consumeTag(expectedTag);
    if (o != null) {
        return convertWireTypeToJavaType(type, o);
    }
    TagReader in = messageContext.in;
    while (true) {
        int tag = in.readTag();
        if (tag == 0) {
            break;
        }
        if (tag == expectedTag) {
            switch(type) {
                case DOUBLE:
                    return in.readDouble();
                case FLOAT:
                    return in.readFloat();
                case BOOL:
                    return in.readBool();
                case STRING:
                    return in.readString();
                case BYTES:
                    return in.readByteArray();
                case INT32:
                    return in.readInt32();
                case SFIXED32:
                    return in.readSFixed32();
                case FIXED32:
                    return in.readFixed32();
                case UINT32:
                    return in.readUInt32();
                case SINT32:
                    return in.readSInt32();
                case INT64:
                    return in.readInt64();
                case UINT64:
                    return in.readUInt64();
                case FIXED64:
                    return in.readFixed64();
                case SFIXED64:
                    return in.readSFixed64();
                case SINT64:
                    return in.readSInt64();
                default:
                    throw new IOException("Unexpected field type : " + type);
            }
        }
        messageContext.unknownFieldSet.readSingleField(tag, in);
    }
    if (fd.hasDefaultValue()) {
        return fd.getDefaultValue();
    }
    if (fd.isRequired()) {
        throw new IOException("Field " + fd.getFullName() + " is required but is not present in the stream");
    }
    return null;
}
Also used : Type(org.infinispan.protostream.descriptors.Type) WireType(org.infinispan.protostream.descriptors.WireType) JavaType(org.infinispan.protostream.descriptors.JavaType) IOException(java.io.IOException) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) TagReader(org.infinispan.protostream.TagReader)

Aggregations

TagReader (org.infinispan.protostream.TagReader)10 SerializationContext (org.infinispan.protostream.SerializationContext)6 TagWriter (org.infinispan.protostream.TagWriter)5 Test (org.junit.Test)5 FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ReadContext (org.infinispan.protostream.ProtobufTagMarshaller.ReadContext)3 WriteContext (org.infinispan.protostream.ProtobufTagMarshaller.WriteContext)3 IOException (java.io.IOException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 Random (java.util.Random)1 BaseMarshaller (org.infinispan.protostream.BaseMarshaller)1 UnknownFieldSet (org.infinispan.protostream.UnknownFieldSet)1 FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)1 JavaType (org.infinispan.protostream.descriptors.JavaType)1 Type (org.infinispan.protostream.descriptors.Type)1 WireType (org.infinispan.protostream.descriptors.WireType)1