Search in sources :

Example 41 with FieldDescriptor

use of org.infinispan.protostream.descriptors.FieldDescriptor in project protostream by infinispan.

the class ProtoStreamWriterImpl method writeBytes.

@Override
public void writeBytes(String fieldName, InputStream input) throws IOException {
    final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
    checkFieldWrite(fd);
    if (fd.getType() != Type.BYTES) {
        throw new IllegalArgumentException("Declared field type is not of type bytes : " + fd.getFullName());
    }
    if (input == null) {
        throw new IllegalArgumentException("The input stream cannot be null");
    }
    int len = 0;
    List<byte[]> chunks = new LinkedList<>();
    int bufLen;
    byte[] buffer = new byte[CHUNK_SIZE];
    while ((bufLen = input.read(buffer)) != -1) {
        chunks.add(buffer);
        len += bufLen;
        buffer = new byte[CHUNK_SIZE];
    }
    input.close();
    TagWriter out = messageContext.out;
    out.writeTag(fd.getNumber(), WireType.WIRETYPE_LENGTH_DELIMITED);
    out.writeVarint32(len);
    for (byte[] chunk : chunks) {
        out.writeRawBytes(buffer, 0, chunk == buffer ? bufLen : CHUNK_SIZE);
    }
}
Also used : TagWriter(org.infinispan.protostream.TagWriter) LinkedList(java.util.LinkedList) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 42 with FieldDescriptor

use of org.infinispan.protostream.descriptors.FieldDescriptor in project protostream by infinispan.

the class ProtoStreamWriterImpl method writeInts.

@Override
public void writeInts(String fieldName, int[] array) 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 INT32:
            for (int value : array) {
                out.writeInt32(fieldNumber, value);
            }
            break;
        case FIXED32:
            for (int value : array) {
                out.writeFixed32(fieldNumber, value);
            }
            break;
        case UINT32:
            for (int value : array) {
                out.writeUInt32(fieldNumber, value);
            }
            break;
        case SFIXED32:
            for (int value : array) {
                out.writeSFixed32(fieldNumber, value);
            }
            break;
        case SINT32:
            for (int value : array) {
                out.writeSInt32(fieldNumber, 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 43 with FieldDescriptor

use of org.infinispan.protostream.descriptors.FieldDescriptor in project protostream by infinispan.

the class ProtoStreamWriterImpl method writeEnum.

@Override
public <E extends Enum<E>> void writeEnum(String fieldName, E value) throws IOException {
    final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
    if (fd.getType() != Type.ENUM) {
        throw new IllegalArgumentException("Declared field type is not an enum : " + fd.getFullName());
    }
    checkFieldWrite(fd);
    if (value == null) {
        if (fd.isRequired()) {
            throw new IllegalArgumentException("A required field cannot be null : " + fd.getFullName());
        }
        return;
    }
    writeEnum(fd, value);
}
Also used : FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 44 with FieldDescriptor

use of org.infinispan.protostream.descriptors.FieldDescriptor in project protostream by infinispan.

the class ProtoStreamWriterImpl method writeString.

@Override
public void writeString(String fieldName, String value) throws IOException {
    final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
    checkFieldWrite(fd);
    if (fd.getType() != Type.STRING) {
        throw new IllegalArgumentException("Declared field type is not of type string : " + fd.getFullName());
    }
    if (value == null) {
        if (fd.isRequired()) {
            throw new IllegalArgumentException("A required field cannot be null : " + fd.getFullName());
        }
        return;
    }
    messageContext.out.writeString(fd.getNumber(), value);
}
Also used : FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 45 with FieldDescriptor

use of org.infinispan.protostream.descriptors.FieldDescriptor in project protostream by infinispan.

the class ProtoStreamWriterImpl method writeDouble.

@Override
public void writeDouble(String fieldName, double value) throws IOException {
    final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
    checkFieldWrite(fd);
    if (fd.getType() != Type.DOUBLE) {
        throw new IllegalArgumentException("The Protobuf declared field type is not compatible with the written type : " + fd.getFullName());
    }
    messageContext.out.writeDouble(fd.getNumber(), value);
}
Also used : FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Aggregations

FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)51 Descriptor (org.infinispan.protostream.descriptors.Descriptor)16 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)12 TagWriter (org.infinispan.protostream.TagWriter)9 IOException (java.io.IOException)7 GenericDescriptor (org.infinispan.protostream.descriptors.GenericDescriptor)7 EnumValueDescriptor (org.infinispan.protostream.descriptors.EnumValueDescriptor)6 FileDescriptor (org.infinispan.protostream.descriptors.FileDescriptor)6 Map (java.util.Map)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)4 Configuration (org.infinispan.protostream.config.Configuration)4 ExtendDescriptor (org.infinispan.protostream.descriptors.ExtendDescriptor)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 Objects (java.util.Objects)3 AnnotationElement (org.infinispan.protostream.descriptors.AnnotationElement)3 Set (java.util.Set)2 Function (java.util.function.Function)2