Search in sources :

Example 6 with TagWriter

use of org.infinispan.protostream.TagWriter 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));
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TagReader(org.infinispan.protostream.TagReader) WriteContext(org.infinispan.protostream.ProtobufTagMarshaller.WriteContext) ByteArrayInputStream(java.io.ByteArrayInputStream) ReadContext(org.infinispan.protostream.ProtobufTagMarshaller.ReadContext) TagWriter(org.infinispan.protostream.TagWriter) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource) Test(org.junit.Test)

Example 7 with TagWriter

use of org.infinispan.protostream.TagWriter in project protostream by infinispan.

the class ProtoStreamWriterImpl method writeLongs.

@Override
public void writeLongs(String fieldName, long[] 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 INT64:
            for (long value : array) {
                out.writeInt64(fieldNumber, value);
            }
            break;
        case FIXED64:
            for (long value : array) {
                out.writeFixed64(fieldNumber, value);
            }
            break;
        case UINT64:
            for (long value : array) {
                out.writeUInt64(fieldNumber, value);
            }
            break;
        case SFIXED64:
            for (long value : array) {
                out.writeSFixed64(fieldNumber, value);
            }
            break;
        case SINT64:
            for (long value : array) {
                out.writeSInt64(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 8 with TagWriter

use of org.infinispan.protostream.TagWriter in project protostream by infinispan.

the class ProtoStreamWriterImpl method writeDoubles.

@Override
public void writeDoubles(String fieldName, double[] array) throws IOException {
    final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
    if (fd.getType() != Type.DOUBLE) {
        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 (double value : array) {
        out.writeDouble(fieldNumber, value);
    }
}
Also used : TagWriter(org.infinispan.protostream.TagWriter) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 9 with TagWriter

use of org.infinispan.protostream.TagWriter in project protostream by infinispan.

the class ProtoStreamWriterImpl method writeCollection.

@Override
public <E> void writeCollection(String fieldName, Collection<? super E> collection, Class<E> elementClass) throws IOException {
    final FieldDescriptor fd = messageContext.getFieldByName(fieldName);
    checkRepeatedFieldWrite(fd);
    if (collection == 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 : collection) {
                validateElement(t, elementClass);
                writeGroup(fd, t, elementClass);
            }
            break;
        case MESSAGE:
            for (Object t : collection) {
                validateElement(t, elementClass);
                writeMessage(fd, t, elementClass);
            }
            break;
        case ENUM:
            for (Object t : collection) {
                validateElement(t, elementClass);
                writeEnum(fd, (Enum) t);
            }
            break;
        case DOUBLE:
            validateElementClass(elementClass, Double.class);
            for (Object value : collection) {
                validateElement(value, elementClass);
                out.writeDouble(fieldNumber, (Double) value);
            }
            break;
        case FLOAT:
            validateElementClass(elementClass, Float.class);
            for (Object value : collection) {
                validateElement(value, elementClass);
                out.writeFloat(fieldNumber, (Float) value);
            }
            break;
        case BOOL:
            validateElementClass(elementClass, Boolean.class);
            for (Object value : collection) {
                validateElement(value, elementClass);
                out.writeBool(fieldNumber, (Boolean) value);
            }
            break;
        case STRING:
            validateElementClass(elementClass, String.class);
            for (Object value : collection) {
                validateElement(value, elementClass);
                out.writeString(fieldNumber, (String) value);
            }
            break;
        case BYTES:
            validateElementClass(elementClass, byte[].class);
            for (Object value : collection) {
                validateElement(value, elementClass);
                out.writeBytes(fieldNumber, (byte[]) value);
            }
            break;
        case INT64:
            validateElementClass(elementClass, Long.class);
            for (Object value : collection) {
                validateElement(value, elementClass);
                out.writeInt64(fieldNumber, (Long) value);
            }
            break;
        case UINT64:
            validateElementClass(elementClass, Long.class);
            for (Object value : collection) {
                validateElement(value, elementClass);
                out.writeUInt64(fieldNumber, (Long) value);
            }
            break;
        case FIXED64:
            validateElementClass(elementClass, Long.class);
            for (Object value : collection) {
                validateElement(value, elementClass);
                out.writeFixed64(fieldNumber, (Long) value);
            }
            break;
        case SFIXED64:
            validateElementClass(elementClass, Long.class);
            for (Object value : collection) {
                validateElement(value, elementClass);
                out.writeSFixed64(fieldNumber, (Long) value);
            }
            break;
        case SINT64:
            validateElementClass(elementClass, Long.class);
            for (Object value : collection) {
                validateElement(value, elementClass);
                out.writeSInt64(fieldNumber, (Long) value);
            }
            break;
        case INT32:
            validateElementClass(elementClass, Integer.class);
            for (Object value : collection) {
                validateElement(value, elementClass);
                out.writeInt32(fieldNumber, (Integer) value);
            }
            break;
        case FIXED32:
            validateElementClass(elementClass, Integer.class);
            for (Object value : collection) {
                validateElement(value, elementClass);
                out.writeFixed32(fieldNumber, (Integer) value);
            }
            break;
        case UINT32:
            validateElementClass(elementClass, Integer.class);
            for (Object value : collection) {
                validateElement(value, elementClass);
                out.writeUInt32(fieldNumber, (Integer) value);
            }
            break;
        case SFIXED32:
            validateElementClass(elementClass, Integer.class);
            for (Object value : collection) {
                validateElement(value, elementClass);
                out.writeSFixed32(fieldNumber, (Integer) value);
            }
            break;
        case SINT32:
            validateElementClass(elementClass, Integer.class);
            for (Object value : collection) {
                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 10 with TagWriter

use of org.infinispan.protostream.TagWriter 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)

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