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());
}
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());
}
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()));
}
});
}
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;
}
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));
}
Aggregations