Search in sources :

Example 16 with TagWriter

use of org.infinispan.protostream.TagWriter 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 17 with TagWriter

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

TagWriter (org.infinispan.protostream.TagWriter)17 FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)9 SerializationContext (org.infinispan.protostream.SerializationContext)5 TagReader (org.infinispan.protostream.TagReader)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Test (org.junit.Test)4 FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)3 ReadContext (org.infinispan.protostream.ProtobufTagMarshaller.ReadContext)3 WriteContext (org.infinispan.protostream.ProtobufTagMarshaller.WriteContext)3 JsonToken (com.fasterxml.jackson.core.JsonToken)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 InputStream (java.io.InputStream)1 ByteBuffer (java.nio.ByteBuffer)1 LinkedList (java.util.LinkedList)1 Random (java.util.Random)1 AnnotatedDescriptor (org.infinispan.protostream.descriptors.AnnotatedDescriptor)1 Descriptor (org.infinispan.protostream.descriptors.Descriptor)1 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)1