use of org.infinispan.protostream.TagWriter in project protostream by infinispan.
the class JsonUtils method processObject.
private static void processObject(ImmutableSerializationContext ctx, JsonParser parser, TagWriter writer, Descriptor messageDescriptor, Integer fieldNumber, boolean topLevel) throws IOException {
Set<String> requiredFields = messageDescriptor.getFields().stream().filter(FieldDescriptor::isRequired).map(FieldDescriptor::getName).collect(Collectors.toCollection(HashSet::new));
ByteArrayOutputStream baos = new ByteArrayOutputStream(ProtobufUtil.DEFAULT_ARRAY_BUFFER_SIZE);
TagWriter nestedWriter = TagWriterImpl.newInstanceNoBuffer(ctx, baos);
String currentField = null;
out: while (true) {
JsonToken token = parser.nextToken();
if (token == null) {
break;
}
switch(token) {
case END_OBJECT:
break out;
case START_ARRAY:
processArray(ctx, messageDescriptor.getFullName(), currentField, parser, nestedWriter);
break;
case START_OBJECT:
{
FieldDescriptor fd = messageDescriptor.findFieldByName(currentField);
Descriptor messageType = fd.getMessageType();
if (messageType == null) {
throw new IllegalStateException("Field '" + currentField + "' is not an object");
}
processObject(ctx, parser, nestedWriter, messageType, fd.getNumber(), false);
requiredFields.remove(currentField);
break;
}
case FIELD_NAME:
currentField = parser.getCurrentName();
break;
case VALUE_STRING:
case VALUE_NUMBER_INT:
case VALUE_NUMBER_FLOAT:
case VALUE_TRUE:
case VALUE_FALSE:
{
FieldDescriptor fd = messageDescriptor.findFieldByName(currentField);
if (fd == null) {
throw new IllegalStateException("The field '" + currentField + "' was not found in the Protobuf schema");
}
if (fd.getType() == Type.ENUM) {
writeEnumField(parser, nestedWriter, fd);
} else {
writeField(parser, nestedWriter, fd.getType(), fd.getNumber());
}
requiredFields.remove(currentField);
break;
}
case VALUE_NULL:
// we got null in, we write nothing out
break;
}
}
if (!requiredFields.isEmpty()) {
String missing = requiredFields.iterator().next();
throw new IllegalStateException("Required field '" + missing + "' missing");
}
if (topLevel) {
Integer topLevelTypeId = messageDescriptor.getTypeId();
if (topLevelTypeId == null) {
writer.writeString(WRAPPED_TYPE_NAME, messageDescriptor.getFullName());
} else {
writer.writeUInt32(WRAPPED_TYPE_ID, topLevelTypeId);
}
nestedWriter.flush();
writer.writeBytes(WRAPPED_MESSAGE, baos.toByteArray());
} else {
nestedWriter.flush();
writer.writeBytes(fieldNumber, baos.toByteArray());
}
writer.flush();
}
use of org.infinispan.protostream.TagWriter in project protostream by infinispan.
the class UnknownFieldSetImplTest method marshall.
private byte[] marshall(UnknownFieldSetImpl unknownFieldSet) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
TagWriter tagWriter = TagWriterImpl.newInstance(null, baos);
unknownFieldSet.writeTo(tagWriter);
tagWriter.flush();
return baos.toByteArray();
}
use of org.infinispan.protostream.TagWriter in project protostream by infinispan.
the class ProtoStreamWriterImpl method writeArray.
@Override
public <E> void writeArray(String fieldName, E[] array, Class<? extends E> elementClass) throws IOException {
final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
checkRepeatedFieldWrite(fd);
if (array == null) {
// a repeated field can never be flagged as required
return;
}
final TagWriter out = messageContext.out;
final int fieldNumber = fd.getNumber();
switch(fd.getType()) {
case GROUP:
for (Object t : array) {
validateElement(t, elementClass);
writeGroup(fd, t, elementClass);
}
break;
case MESSAGE:
for (Object t : array) {
validateElement(t, elementClass);
writeMessage(fd, t, elementClass);
}
break;
case ENUM:
for (Object t : array) {
validateElement(t, elementClass);
writeEnum(fd, (Enum) t);
}
break;
case DOUBLE:
validateElementClass(elementClass, Double.class);
for (Object value : array) {
validateElement(value, elementClass);
out.writeDouble(fieldNumber, (Double) value);
}
break;
case FLOAT:
validateElementClass(elementClass, Float.class);
for (Object value : array) {
validateElement(value, elementClass);
out.writeFloat(fieldNumber, (Float) value);
}
break;
case BOOL:
validateElementClass(elementClass, Boolean.class);
for (Object value : array) {
validateElement(value, elementClass);
out.writeBool(fieldNumber, (Boolean) value);
}
break;
case STRING:
validateElementClass(elementClass, String.class);
for (Object value : array) {
validateElement(value, elementClass);
out.writeString(fieldNumber, (String) value);
}
break;
case BYTES:
validateElementClass(elementClass, byte[].class);
for (Object value : array) {
validateElement(value, elementClass);
out.writeBytes(fieldNumber, (byte[]) value);
}
break;
case INT64:
validateElementClass(elementClass, Long.class);
for (Object value : array) {
validateElement(value, elementClass);
out.writeInt64(fieldNumber, (Long) value);
}
break;
case UINT64:
validateElementClass(elementClass, Long.class);
for (Object value : array) {
validateElement(value, elementClass);
out.writeUInt64(fieldNumber, (Long) value);
}
break;
case FIXED64:
validateElementClass(elementClass, Long.class);
for (Object value : array) {
validateElement(value, elementClass);
out.writeFixed64(fieldNumber, (Long) value);
}
break;
case SFIXED64:
validateElementClass(elementClass, Long.class);
for (Object value : array) {
validateElement(value, elementClass);
out.writeSFixed64(fieldNumber, (Long) value);
}
break;
case SINT64:
validateElementClass(elementClass, Long.class);
for (Object value : array) {
validateElement(value, elementClass);
out.writeSInt64(fieldNumber, (Long) value);
}
break;
case INT32:
validateElementClass(elementClass, Integer.class);
for (Object value : array) {
validateElement(value, elementClass);
out.writeInt32(fieldNumber, (Integer) value);
}
break;
case FIXED32:
validateElementClass(elementClass, Integer.class);
for (Object value : array) {
validateElement(value, elementClass);
out.writeFixed32(fieldNumber, (Integer) value);
}
break;
case UINT32:
validateElementClass(elementClass, Integer.class);
for (Object value : array) {
validateElement(value, elementClass);
out.writeUInt32(fieldNumber, (Integer) value);
}
break;
case SFIXED32:
validateElementClass(elementClass, Integer.class);
for (Object value : array) {
validateElement(value, elementClass);
out.writeSFixed32(fieldNumber, (Integer) value);
}
break;
case SINT32:
validateElementClass(elementClass, Integer.class);
for (Object value : array) {
validateElement(value, elementClass);
out.writeSInt32(fieldNumber, (Integer) value);
}
break;
default:
throw new IllegalArgumentException("The Protobuf declared field type is not compatible with the written type : " + fd.getFullName());
}
}
use of org.infinispan.protostream.TagWriter in project protostream by infinispan.
the class ProtoStreamWriterImpl method writeBooleans.
@Override
public void writeBooleans(String fieldName, boolean[] array) throws IOException {
final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
if (fd.getType() != Type.BOOL) {
throw new IllegalArgumentException("The Protobuf declared field type is not compatible with the written type : " + fd.getFullName());
}
checkRepeatedFieldWrite(fd);
if (array == null) {
// a repeated field can never be flagged as required
return;
}
final TagWriter out = messageContext.out;
final int fieldNumber = fd.getNumber();
for (boolean value : array) {
out.writeBool(fieldNumber, value);
}
}
use of org.infinispan.protostream.TagWriter 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);
}
}
Aggregations