Search in sources :

Example 1 with ReadContext

use of org.infinispan.protostream.ProtobufTagMarshaller.ReadContext 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 2 with ReadContext

use of org.infinispan.protostream.ProtobufTagMarshaller.ReadContext 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 3 with ReadContext

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

FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)3 ReadContext (org.infinispan.protostream.ProtobufTagMarshaller.ReadContext)3 WriteContext (org.infinispan.protostream.ProtobufTagMarshaller.WriteContext)3 SerializationContext (org.infinispan.protostream.SerializationContext)3 TagReader (org.infinispan.protostream.TagReader)3 TagWriter (org.infinispan.protostream.TagWriter)3 Test (org.junit.Test)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1