use of org.infinispan.protostream.ProtobufTagMarshaller.WriteContext 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);
}
}
use of org.infinispan.protostream.ProtobufTagMarshaller.WriteContext 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));
}
use of org.infinispan.protostream.ProtobufTagMarshaller.WriteContext 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));
}
Aggregations