Search in sources :

Example 11 with TagWriter

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

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

the class ProtoStreamWriterImpl method writeFloats.

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

Example 13 with TagWriter

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

the class UnknownFieldSetImpl method writeExternal.

@Override
public void writeExternal(ObjectOutput out) throws IOException {
    ByteArrayOutputStreamEx baos = new ByteArrayOutputStreamEx();
    TagWriter output = TagWriterImpl.newInstanceNoBuffer(null, baos);
    writeTo(output);
    output.flush();
    ByteBuffer buffer = baos.getByteBuffer();
    int off = buffer.arrayOffset();
    int len = buffer.limit() - off;
    out.writeInt(len);
    out.write(buffer.array(), off, len);
}
Also used : TagWriter(org.infinispan.protostream.TagWriter) ByteBuffer(java.nio.ByteBuffer)

Example 14 with TagWriter

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

the class JsonUtils method fromCanonicalJSON.

public static byte[] fromCanonicalJSON(ImmutableSerializationContext ctx, Reader reader) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(ProtobufUtil.DEFAULT_ARRAY_BUFFER_SIZE);
    TagWriter writer = TagWriterImpl.newInstanceNoBuffer(ctx, baos);
    JsonParser parser = jsonFactory.createParser(reader);
    try {
        while (true) {
            JsonToken token = parser.nextToken();
            if (token == null) {
                break;
            }
            switch(token) {
                case START_OBJECT:
                    processDocument(ctx, parser, writer);
                    break;
                case VALUE_NULL:
                    // we got null input, we write nothing out
                    break;
                default:
                    throw new IllegalStateException("Invalid top level object! Found token: " + token);
            }
        }
        writer.flush();
        return baos.toByteArray();
    } catch (JsonProcessingException e) {
        throw new IllegalStateException("Invalid JSON", e);
    } finally {
        baos.close();
        reader.close();
    }
}
Also used : TagWriter(org.infinispan.protostream.TagWriter) JsonToken(com.fasterxml.jackson.core.JsonToken) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 15 with TagWriter

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

the class TagWriterImplTest method doTest.

private void doTest(Factory factory) throws IOException {
    log.infof("SEED is %s", SEED);
    Random random = new Random(SEED);
    Data data = new Data(random);
    SerializationContext ctx = ProtobufUtil.newSerializationContext();
    TagWriter writer = factory.newWriter(ctx);
    int tag = 0;
    // bool
    writer.writeBool(++tag, data.b);
    // ints
    writer.writeInt32(++tag, data.i);
    writer.writeSInt32(++tag, data.i);
    writer.writeUInt32(++tag, Math.abs(data.i));
    // longs
    writer.writeInt64(++tag, data.l);
    writer.writeSInt64(++tag, data.l);
    writer.writeUInt64(++tag, Math.abs(data.l));
    // double
    writer.writeDouble(++tag, data.d);
    // float
    writer.writeFloat(++tag, data.f);
    // string
    writer.writeString(++tag, data.s);
    // byte(s)
    writer.writeBytes(++tag, data.bytes);
    writer.writeBytes(++tag, data.bytes, 1, 2);
    writer.flush();
    TagReader reader = factory.newReader(ctx);
    tag = 0;
    // bool
    checkFieldNumber(++tag, reader);
    assertEquals(data.b, reader.readBool());
    // ints
    checkFieldNumber(++tag, reader);
    assertEquals(data.i, reader.readInt32());
    checkFieldNumber(++tag, reader);
    assertEquals(data.i, reader.readSInt32());
    checkFieldNumber(++tag, reader);
    assertEquals(Math.abs(data.i), reader.readUInt32());
    // longs
    checkFieldNumber(++tag, reader);
    assertEquals(data.l, reader.readInt64());
    checkFieldNumber(++tag, reader);
    assertEquals(data.l, reader.readSInt64());
    checkFieldNumber(++tag, reader);
    assertEquals(Math.abs(data.l), reader.readUInt64());
    // double
    checkFieldNumber(++tag, reader);
    assertEquals(data.d, reader.readDouble(), 1);
    // float
    checkFieldNumber(++tag, reader);
    assertEquals(data.f, reader.readFloat(), 1);
    // string
    checkFieldNumber(++tag, reader);
    assertEquals(data.s, reader.readString());
    // byte(s)
    checkFieldNumber(++tag, reader);
    assertArrayEquals(data.bytes, reader.readByteArray());
    checkFieldNumber(++tag, reader);
    assertArrayEquals(new byte[] { data.bytes[1], data.bytes[2] }, reader.readByteArray());
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) Random(java.util.Random) TagWriter(org.infinispan.protostream.TagWriter) TagReader(org.infinispan.protostream.TagReader)

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