use of org.infinispan.protostream.TagReader in project protostream by infinispan.
the class EnumMarshallerDelegate method unmarshall.
@Override
public T unmarshall(ProtobufTagMarshaller.ReadContext ctx, FieldDescriptor fd) throws IOException {
final int expectedTag = fd.getWireTag();
int enumValue;
ProtoStreamReaderImpl reader = ((TagReaderImpl) ctx).getProtoStreamReader();
UnknownFieldSet unknownFieldSet = reader.getUnknownFieldSet();
Object value = unknownFieldSet.consumeTag(expectedTag);
if (value != null) {
enumValue = ((Long) value).intValue();
} else {
TagReader in = ctx.getReader();
while (true) {
int tag = in.readTag();
if (tag == 0) {
return null;
}
if (tag == expectedTag) {
enumValue = in.readEnum();
break;
}
unknownFieldSet.readSingleField(tag, in);
}
}
return decode(expectedTag, enumValue, unknownFieldSet);
}
use of org.infinispan.protostream.TagReader in project protostream by infinispan.
the class ProtoStreamReaderImpl method readNestedObject.
/**
* Read an Object or an Enum.
*
* @param length the actual length of the nested object or -1 if the length should be read from the stream
*/
private <A> A readNestedObject(FieldDescriptor fd, Class<A> clazz, ProtobufTagMarshaller.ReadContext ctx, int length) throws IOException {
BaseMarshallerDelegate<A> marshallerDelegate = serCtx.getMarshallerDelegate(clazz);
TagReader in = ctx.getReader();
A a;
if (fd.getType() == Type.GROUP) {
a = marshallerDelegate.unmarshall(ctx, fd);
in.checkLastTagWas(WireType.makeTag(fd.getNumber(), WireType.WIRETYPE_END_GROUP));
} else if (fd.getType() == Type.MESSAGE) {
if (length < 0) {
length = in.readUInt32();
}
int oldLimit = in.pushLimit(length);
a = marshallerDelegate.unmarshall(ctx, fd);
in.checkLastTagWas(0);
in.popLimit(oldLimit);
} else {
throw new IllegalArgumentException("Declared field type is not a message or an enum : " + fd.getFullName());
}
return a;
}
use of org.infinispan.protostream.TagReader 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.TagReader 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.TagReader in project protostream by infinispan.
the class ProtoStreamReaderImpl method readPrimitive.
private Object readPrimitive(String fieldName, JavaType javaType) throws IOException {
final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
final Type type = fd.getType();
if (type == Type.ENUM || type == Type.GROUP || type == Type.MESSAGE) {
throw new IllegalArgumentException("Declared field type is not a primitive : " + fd.getFullName());
}
if (fd.getJavaType() != javaType) {
throw new IllegalArgumentException("Declared field type is not of the expected type : " + fd.getFullName());
}
checkFieldRead(fd, false);
final int expectedTag = fd.getWireTag();
Object o = messageContext.unknownFieldSet.consumeTag(expectedTag);
if (o != null) {
return convertWireTypeToJavaType(type, o);
}
TagReader in = messageContext.in;
while (true) {
int tag = in.readTag();
if (tag == 0) {
break;
}
if (tag == expectedTag) {
switch(type) {
case DOUBLE:
return in.readDouble();
case FLOAT:
return in.readFloat();
case BOOL:
return in.readBool();
case STRING:
return in.readString();
case BYTES:
return in.readByteArray();
case INT32:
return in.readInt32();
case SFIXED32:
return in.readSFixed32();
case FIXED32:
return in.readFixed32();
case UINT32:
return in.readUInt32();
case SINT32:
return in.readSInt32();
case INT64:
return in.readInt64();
case UINT64:
return in.readUInt64();
case FIXED64:
return in.readFixed64();
case SFIXED64:
return in.readSFixed64();
case SINT64:
return in.readSInt64();
default:
throw new IOException("Unexpected field type : " + type);
}
}
messageContext.unknownFieldSet.readSingleField(tag, in);
}
if (fd.hasDefaultValue()) {
return fd.getDefaultValue();
}
if (fd.isRequired()) {
throw new IOException("Field " + fd.getFullName() + " is required but is not present in the stream");
}
return null;
}
Aggregations