Java Code Examples for io.protostuff.ProtobufIOUtil#writeTo()

The following examples show how to use io.protostuff.ProtobufIOUtil#writeTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InheritanceTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testInheritanceProtobuf() throws IOException
{
    Schema<InputSystem> schema = getSchema(InputSystem.class);
    InputSystem sys = new InputSystem();
    KeyBoard kb = new KeyBoard();
    Mouse ms = new Mouse();
    kb.setName("Test");
    kb.setNumberOfKeys(10);
    ms.setName("Test1");
    ms.setNumberOfButtons(2);
    List<InputDevice> devices = new ArrayList<InputDevice>();
    devices.add(ms);
    devices.add(kb);
    sys.setInputDevices(devices);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ProtobufIOUtil.writeTo(out, sys, schema, buf());
    byte[] listData = out.toByteArray();
    InputSystem deserSystem = new InputSystem();
    ByteArrayInputStream in = new ByteArrayInputStream(listData);
    ProtobufIOUtil.mergeFrom(in, deserSystem, schema, buf());

    assertEquals(sys, deserSystem);
}
 
Example 2
Source File: ProtostuffMessageBodyWriter.java    From datawave with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void writeTo(Object message, Class<?> clazz, Type type, Annotation[] annotations, MediaType media, MultivaluedMap<String,Object> httpHeaders,
                OutputStream out) throws IOException, WebApplicationException {
    
    // TODO: Figure out a method to add the proto file location in the response headers.
    // This map must be mofified before any data is written to out,
    // since at that time the response headers will be flushed.
    
    Schema<Object> schema = null;
    if (message instanceof Message) {
        Message<Object> msg = (Message<Object>) message;
        schema = msg.cachedSchema();
    } else {
        schema = (Schema<Object>) RuntimeSchema.getSchema(clazz);
    }
    
    try {
        if (MediaType.APPLICATION_XML_TYPE.equals(media) || MediaType.TEXT_XML_TYPE.equals(media)) {
            XmlIOUtil.writeTo(out, message, schema);
        } else if ("text/yaml".equals(media.toString()) || "text/x-yaml".equals(media.toString()) || "application/x-yaml".equals(media.toString())) {
            YamlIOUtil.writeTo(out, message, schema, buffer);
        } else if ("application/x-protobuf".equals(media.toString())) {
            ProtobufIOUtil.writeTo(out, message, schema, buffer);
        } else if ("application/x-protostuff".equals(media.toString())) {
            ProtostuffIOUtil.writeTo(out, message, schema, buffer);
        } else if (MediaType.APPLICATION_JSON_TYPE.equals(media)) {
            IOContext ctx = new IOContext(JsonIOUtil.DEFAULT_JSON_FACTORY._getBufferRecycler(), out, false);
            UTF8JsonGenerator generator = new UTF8JsonGenerator(ctx, JsonIOUtil.DEFAULT_JSON_FACTORY.getGeneratorFeatures(),
                            JsonIOUtil.DEFAULT_JSON_FACTORY.getCodec(), out);
            try {
                JsonIOUtil.writeTo(generator, message, schema, false);
            } finally {
                generator.close();
            }
        }
    } finally {
        buffer.clear();
    }
}
 
Example 3
Source File: RuntimeSchemaBenchmark.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Benchmark
public void runtime_serialize_1_int_field() throws Exception
{
    try
    {
        ProtobufIOUtil.writeTo(buffer, int1, int1RuntimeSchema);
    }
    finally
    {
        buffer.clear();
    }
}
 
Example 4
Source File: RuntimeSchemaBenchmark.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Benchmark
public void runtime_serialize_10_int_fields() throws Exception
{
    try
    {
        ProtobufIOUtil.writeTo(buffer, int10, int10RuntimeSchema);
    }
    finally
    {
        buffer.clear();
    }
}
 
Example 5
Source File: RuntimeSchemaBenchmark.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Benchmark
public void runtime_sparse_serialize_1_int_field() throws Exception
{
    try
    {
        ProtobufIOUtil.writeTo(buffer, sparseInt1, sparseInt1RuntimeSchema);
    }
    finally
    {
        buffer.clear();
    }
}
 
Example 6
Source File: RuntimeSchemaBenchmark.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Benchmark
public void runtime_sparse_serialize_10_int_fields() throws Exception
{
    try
    {
        ProtobufIOUtil.writeTo(buffer, sparseInt10, sparseInt10RuntimeSchema);
    }
    finally
    {
        buffer.clear();
    }
}
 
Example 7
Source File: RuntimeSchemaBenchmark.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Benchmark
public void generated_serialize_1_int_field() throws Exception
{
    try
    {
        ProtobufIOUtil.writeTo(buffer, generatedInt1, generatedInt1Schema);
    }
    finally
    {
        buffer.clear();
    }
}
 
Example 8
Source File: RuntimeSchemaBenchmark.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Benchmark
public void generated_serialize_10_int_field() throws Exception
{
    try
    {
        ProtobufIOUtil.writeTo(buffer, generatedInt10, generatedInt10Schema);
    }
    finally
    {
        buffer.clear();
    }
}
 
Example 9
Source File: ProtobufSerializer.java    From voyage with Apache License 2.0 4 votes vote down vote up
protected <T> int writeObject(LinkedBuffer buffer, T object,
		Schema<T> schema) {
	return ProtobufIOUtil.writeTo(buffer, object, schema);
}
 
Example 10
Source File: ProtobufRuntimeObjectSchemaTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> void writeTo(OutputStream out, T message, Schema<T> schema)
        throws IOException
{
    ProtobufIOUtil.writeTo(out, message, schema, buf());
}
 
Example 11
Source File: ProtobufNullArrayElementTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> void writeTo(OutputStream out, T message, Schema<T> schema)
        throws IOException
{
    ProtobufIOUtil.writeTo(out, message, schema, buf());
}
 
Example 12
Source File: ProtobufRuntimeCollectionSchemaTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> void writeTo(OutputStream out, T message, Schema<T> schema)
        throws IOException
{
    ProtobufIOUtil.writeTo(out, message, schema, buf());
}
 
Example 13
Source File: ProtobufNullArrayElementInObjectArrayTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> void writeTo(OutputStream out, T message, Schema<T> schema)
        throws IOException
{
    ProtobufIOUtil.writeTo(out, message, schema, buf());
}
 
Example 14
Source File: ProtobufRuntimeMapTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> void writeTo(OutputStream out, T message, Schema<T> schema)
        throws IOException
{
    ProtobufIOUtil.writeTo(out, message, schema, buf());
}
 
Example 15
Source File: RuntimeSchemaBenchmark.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Setup
public void prepare() throws IOException
{
    int1RuntimeSchema = RuntimeSchema.createFrom(Int1.class);
    int10RuntimeSchema = RuntimeSchema.createFrom(Int10.class);
    sparseInt1RuntimeSchema = RuntimeSchema.createFrom(SparseInt1.class);
    sparseInt10RuntimeSchema = RuntimeSchema.createFrom(SparseInt10.class);
    generatedInt1Schema = GeneratedInt1.getSchema();
    generatedInt10Schema = GeneratedInt10.getSchema();
    int1 = new Int1();
    int1.a0 = 1;
    int10 = new Int10();
    int10.a0 = 1;
    int10.a1 = 2;
    int10.a2 = 3;
    int10.a3 = 4;
    int10.a4 = 5;
    int10.a5 = 6;
    int10.a6 = 7;
    int10.a7 = 8;
    int10.a8 = 9;
    int10.a9 = 10;
    sparseInt1 = new SparseInt1();
    sparseInt1.a0 = 1;
    sparseInt10 = new SparseInt10();
    sparseInt10.a0 = 1;
    sparseInt10.a1 = 2;
    sparseInt10.a2 = 3;
    sparseInt10.a3 = 4;
    sparseInt10.a4 = 5;
    sparseInt10.a5 = 6;
    sparseInt10.a6 = 7;
    sparseInt10.a7 = 8;
    sparseInt10.a8 = 9;
    sparseInt10.a9 = 10;
    generatedInt1 = new GeneratedInt1();
    generatedInt1.setA0(1);
    generatedInt10 = new GeneratedInt10();
    generatedInt10.setA0(1);
    generatedInt10.setA1(2);
    generatedInt10.setA2(3);
    generatedInt10.setA3(4);
    generatedInt10.setA4(5);
    generatedInt10.setA5(6);
    generatedInt10.setA6(7);
    generatedInt10.setA7(8);
    generatedInt10.setA8(9);
    generatedInt10.setA9(10);
    buffer = LinkedBuffer.allocate();

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ProtobufIOUtil.writeTo(outputStream, int1, int1RuntimeSchema, buffer);
    data_1_int = outputStream.toByteArray();
    outputStream.reset();
    buffer.clear();

    ProtobufIOUtil.writeTo(outputStream, int10, int10RuntimeSchema, buffer);
    data_10_int = outputStream.toByteArray();
    outputStream.reset();
    buffer.clear();
}