Search in sources :

Example 1 with TagWriter

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();
}
Also used : TagWriter(org.infinispan.protostream.TagWriter) AnnotatedDescriptor(org.infinispan.protostream.descriptors.AnnotatedDescriptor) EnumValueDescriptor(org.infinispan.protostream.descriptors.EnumValueDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) Descriptor(org.infinispan.protostream.descriptors.Descriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) JsonToken(com.fasterxml.jackson.core.JsonToken) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 2 with TagWriter

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();
}
Also used : TagWriter(org.infinispan.protostream.TagWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 3 with TagWriter

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());
    }
}
Also used : TagWriter(org.infinispan.protostream.TagWriter) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 4 with TagWriter

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);
    }
}
Also used : TagWriter(org.infinispan.protostream.TagWriter) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 5 with TagWriter

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

TagWriter (org.infinispan.protostream.TagWriter)17 FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)9 SerializationContext (org.infinispan.protostream.SerializationContext)5 TagReader (org.infinispan.protostream.TagReader)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 Test (org.junit.Test)4 FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)3 ReadContext (org.infinispan.protostream.ProtobufTagMarshaller.ReadContext)3 WriteContext (org.infinispan.protostream.ProtobufTagMarshaller.WriteContext)3 JsonToken (com.fasterxml.jackson.core.JsonToken)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 InputStream (java.io.InputStream)1 ByteBuffer (java.nio.ByteBuffer)1 LinkedList (java.util.LinkedList)1 Random (java.util.Random)1 AnnotatedDescriptor (org.infinispan.protostream.descriptors.AnnotatedDescriptor)1 Descriptor (org.infinispan.protostream.descriptors.Descriptor)1 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)1