Search in sources :

Example 6 with TagReader

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

the class SerializationContextImplTest method testMarshallerProvider.

@Test
public void testMarshallerProvider() throws Exception {
    SerializationContext ctx = createContext();
    String file = "package test;\n" + "message X {\n" + "   optional int32 f = 1;\n" + "}";
    class X {

        Integer f;

        private X(Integer f) {
            this.f = f;
        }
    }
    FileDescriptorSource fileDescriptorSource = new FileDescriptorSource().addProtoFile("file.proto", file);
    ctx.registerProtoFiles(fileDescriptorSource);
    ctx.registerMarshallerProvider(new SerializationContext.MarshallerProvider() {

        @Override
        public BaseMarshaller<?> getMarshaller(String typeName) {
            if (typeName.equals("test.X")) {
                return makeMarshaller();
            }
            return null;
        }

        @Override
        public BaseMarshaller<?> getMarshaller(Class<?> javaClass) {
            if (javaClass == X.class) {
                return makeMarshaller();
            }
            return null;
        }

        private BaseMarshaller<?> makeMarshaller() {
            return new ProtobufTagMarshaller<X>() {

                @Override
                public X read(ReadContext ctx) throws IOException {
                    Integer f = null;
                    TagReader in = ctx.getReader();
                    if (in.readTag() == WireType.makeTag(1, WireType.WIRETYPE_VARINT)) {
                        f = in.readInt32();
                    }
                    return new X(f);
                }

                @Override
                public void write(WriteContext ctx, X x) throws IOException {
                    ctx.getWriter().writeInt32(1, x.f);
                }

                @Override
                public Class<X> getJavaClass() {
                    return X.class;
                }

                @Override
                public String getTypeName() {
                    return "test.X";
                }
            };
        }
    });
    byte[] bytes = ProtobufUtil.toWrappedByteArray(ctx, new X(1234));
    Object out = ProtobufUtil.fromWrappedByteArray(ctx, bytes);
    assertTrue(out instanceof X);
    assertNotNull(((X) out).f);
    assertEquals(1234, ((X) out).f.intValue());
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) IOException(java.io.IOException) TagReader(org.infinispan.protostream.TagReader) BaseMarshaller(org.infinispan.protostream.BaseMarshaller) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) Test(org.junit.Test)

Example 7 with TagReader

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

the class TagWriterImplTest method doTest.

private void doTest(Factory factory) throws IOException {
    log.infof("SEED is %s", SEED);
    Random random = new Random(SEED);
    Data data = new Data(random);
    SerializationContext ctx = ProtobufUtil.newSerializationContext();
    TagWriter writer = factory.newWriter(ctx);
    int tag = 0;
    // bool
    writer.writeBool(++tag, data.b);
    // ints
    writer.writeInt32(++tag, data.i);
    writer.writeSInt32(++tag, data.i);
    writer.writeUInt32(++tag, Math.abs(data.i));
    // longs
    writer.writeInt64(++tag, data.l);
    writer.writeSInt64(++tag, data.l);
    writer.writeUInt64(++tag, Math.abs(data.l));
    // double
    writer.writeDouble(++tag, data.d);
    // float
    writer.writeFloat(++tag, data.f);
    // string
    writer.writeString(++tag, data.s);
    // byte(s)
    writer.writeBytes(++tag, data.bytes);
    writer.writeBytes(++tag, data.bytes, 1, 2);
    writer.flush();
    TagReader reader = factory.newReader(ctx);
    tag = 0;
    // bool
    checkFieldNumber(++tag, reader);
    assertEquals(data.b, reader.readBool());
    // ints
    checkFieldNumber(++tag, reader);
    assertEquals(data.i, reader.readInt32());
    checkFieldNumber(++tag, reader);
    assertEquals(data.i, reader.readSInt32());
    checkFieldNumber(++tag, reader);
    assertEquals(Math.abs(data.i), reader.readUInt32());
    // longs
    checkFieldNumber(++tag, reader);
    assertEquals(data.l, reader.readInt64());
    checkFieldNumber(++tag, reader);
    assertEquals(data.l, reader.readSInt64());
    checkFieldNumber(++tag, reader);
    assertEquals(Math.abs(data.l), reader.readUInt64());
    // double
    checkFieldNumber(++tag, reader);
    assertEquals(data.d, reader.readDouble(), 1);
    // float
    checkFieldNumber(++tag, reader);
    assertEquals(data.f, reader.readFloat(), 1);
    // string
    checkFieldNumber(++tag, reader);
    assertEquals(data.s, reader.readString());
    // byte(s)
    checkFieldNumber(++tag, reader);
    assertArrayEquals(data.bytes, reader.readByteArray());
    checkFieldNumber(++tag, reader);
    assertArrayEquals(new byte[] { data.bytes[1], data.bytes[2] }, reader.readByteArray());
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) Random(java.util.Random) TagWriter(org.infinispan.protostream.TagWriter) TagReader(org.infinispan.protostream.TagReader)

Example 8 with TagReader

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

the class TagWriterImplTest method testOutputStreamEncodeAndDecode.

@Test
public void testOutputStreamEncodeAndDecode() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(MAX_BYTE_ARRAY_SIZE);
    doTest(new Factory() {

        @Override
        public TagWriter newWriter(SerializationContext ctx) {
            return TagWriterImpl.newInstance(ctx, baos);
        }

        @Override
        public TagReader newReader(SerializationContext ctx) {
            return TagReaderImpl.newInstance(ctx, new ByteArrayInputStream(baos.toByteArray()));
        }
    });
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) ByteArrayInputStream(java.io.ByteArrayInputStream) TagWriter(org.infinispan.protostream.TagWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TagReader(org.infinispan.protostream.TagReader) Test(org.junit.Test)

Example 9 with TagReader

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

the class UnknownFieldSetImplTest method unmarshall.

private UnknownFieldSetImpl unmarshall(byte[] bytes) throws IOException {
    TagReader tagReader = TagReaderImpl.newInstance(null, new ByteArrayInputStream(bytes));
    UnknownFieldSetImpl unknownFieldSet = new UnknownFieldSetImpl();
    unknownFieldSet.readAllFields(tagReader);
    return unknownFieldSet;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) TagReader(org.infinispan.protostream.TagReader)

Example 10 with TagReader

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

the class FullBufferReadTest method testFullArrayMarshaller.

@Test
public void testFullArrayMarshaller() 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;

        public boolean isInputStream = false;

        @Override
        public X read(ReadContext rc) throws IOException {
            TagReader r = rc.getReader();
            isInputStream = r.isInputStream();
            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));
    ProtobufUtil.fromWrappedByteArray(ctx, fullMsgBytes);
    assertNotNull(mockMarshallerFuncs.actualBytes);
    assertEquals(6, mockMarshallerFuncs.actualBytes.length);
    assertFalse(mockMarshallerFuncs.isInputStream);
    byte[] expectedBytes = { 8, -46, 9, 16, -31, 33 };
    assertNotNull(expectedBytes);
    assertTrue(Arrays.equals(mockMarshallerFuncs.actualBytes, expectedBytes));
}
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)

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