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

The following examples show how to use io.protostuff.ProtostuffIOUtil#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 testInheritance() 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();
    ProtostuffIOUtil.writeTo(out, sys, schema, buf());
    byte[] listData = out.toByteArray();
    InputSystem deserSystem = new InputSystem();
    ByteArrayInputStream in = new ByteArrayInputStream(listData);
    ProtostuffIOUtil.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: ProtostuffSerializer.java    From tx-lcn with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] serialize(Object obj) throws SerializerException {
    Class cls = obj.getClass();
    LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        Schema schema = getSchema(cls);
        ProtostuffIOUtil.writeTo(outputStream, obj, schema, buffer);
        return outputStream.toByteArray();
    } catch (Exception e) {
        throw new SerializerException(e.getMessage(), e);
    } finally {
        buffer.clear();
    }
}
 
Example 4
Source File: ProtostuffSerializer.java    From tx-lcn with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(Object obj, OutputStream outputStream) throws SerializerException {
    Class cls = obj.getClass();
    LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
    try {
        Schema schema = getSchema(cls);
        ProtostuffIOUtil.writeTo(outputStream, obj, schema, buffer);
    } catch (Exception e) {
        throw new SerializerException(e.getMessage(), e);
    } finally {
        buffer.clear();
    }
}
 
Example 5
Source File: RuntimeSchemaEnumTagTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializeDeserializeNumericEnum() throws Exception
{
    RuntimeSchema<A> schema = RuntimeSchema.createFrom(A.class);
    A source = new A(TaggedEnum.TEN);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    LinkedBuffer buffer = LinkedBuffer.allocate();
    ProtostuffIOUtil.writeTo(outputStream, source, schema, buffer);
    byte[] bytes = outputStream.toByteArray();
    A newMessage = schema.newMessage();
    ProtostuffIOUtil.mergeFrom(bytes, newMessage, schema);
    Assert.assertEquals(source, newMessage);
}
 
Example 6
Source File: RuntimeSchemaTagTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
/**
 * Simple serialization/deserialization test
 *
 * @throws Exception
 */
@Test
public void testSerializeDeserialize() throws Exception
{
    RuntimeSchema<A6> schema = RuntimeSchema.createFrom(A6.class);
    A6 source = new A6(42);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    LinkedBuffer buffer = LinkedBuffer.allocate();
    ProtostuffIOUtil.writeTo(outputStream, source, schema, buffer);
    byte[] bytes = outputStream.toByteArray();
    A6 newMessage = schema.newMessage();
    ProtostuffIOUtil.mergeFrom(bytes, newMessage, schema);
    Assert.assertEquals(source, newMessage);
}
 
Example 7
Source File: AlwaysUseSunReflectionFactoryOptionTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Test
public void forceUseSunReflectionFactory() throws Exception {
	System.setProperty("protostuff.runtime.always_use_sun_reflection_factory", "true");
	Schema<MyClass> schema = RuntimeSchema.getSchema(MyClass.class);
	ByteArrayOutputStream output = new ByteArrayOutputStream();
	MyClass myClass = new MyClass(); // constructor initializes list with one element
	ProtostuffIOUtil.writeTo(output, myClass, schema, LinkedBuffer.allocate());
	byte[] bytes = output.toByteArray();
	Assert.assertEquals(1, myClass.getList().size());
	MyClass myClassNew = schema.newMessage(); // default constructor should not be used
	ProtostuffIOUtil.mergeFrom(bytes, myClassNew, schema);
	Assert.assertEquals(1, myClassNew.getList().size());
}
 
Example 8
Source File: EmptyMessageIT.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializeDeserialize_EmptyMessage() throws Exception
{
    EmptyMessage source = new EmptyMessage();
    LinkedBuffer buffer = LinkedBuffer.allocate();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ProtostuffIOUtil.writeTo(outputStream, source, EMPTY_MESSAGE_SCHEMA, buffer);
    byte[] bytes = outputStream.toByteArray();
    EmptyMessage newMessage = EMPTY_MESSAGE_SCHEMA.newMessage();
    ProtostuffIOUtil.mergeFrom(bytes, newMessage, EMPTY_MESSAGE_SCHEMA);
    Assert.assertEquals(0, bytes.length);
}
 
Example 9
Source File: EmptyMessageIT.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializeDeserialize_MessageWithEmptyMessage_unset() throws Exception
{
    MessageWithEmptyMessage source = new MessageWithEmptyMessage();
    LinkedBuffer buffer = LinkedBuffer.allocate();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ProtostuffIOUtil.writeTo(outputStream, source, MESSAGE_WITH_EMPTY_MESSAGE_SCHEMA, buffer);
    byte[] bytes = outputStream.toByteArray();
    MessageWithEmptyMessage newMessage = MESSAGE_WITH_EMPTY_MESSAGE_SCHEMA.newMessage();
    ProtostuffIOUtil.mergeFrom(bytes, newMessage, MESSAGE_WITH_EMPTY_MESSAGE_SCHEMA);
    Assert.assertEquals(0, bytes.length);
    Assert.assertNull(newMessage.getA());
}
 
Example 10
Source File: EmptyMessageIT.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Test
public void testSerializeDeserialize_MessageWithEmptyMessage_is_set() throws Exception
{
    MessageWithEmptyMessage source = new MessageWithEmptyMessage();
    source.setA(new EmptyMessage());
    LinkedBuffer buffer = LinkedBuffer.allocate();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ProtostuffIOUtil.writeTo(outputStream, source, MESSAGE_WITH_EMPTY_MESSAGE_SCHEMA, buffer);
    byte[] bytes = outputStream.toByteArray();
    MessageWithEmptyMessage newMessage = MESSAGE_WITH_EMPTY_MESSAGE_SCHEMA.newMessage();
    ProtostuffIOUtil.mergeFrom(bytes, newMessage, MESSAGE_WITH_EMPTY_MESSAGE_SCHEMA);
    Assert.assertNotNull(newMessage.getA());
}
 
Example 11
Source File: ProtostuffNullArrayElementTest.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
{
    ProtostuffIOUtil.writeTo(out, message, schema, buf());
}
 
Example 12
Source File: ProtostuffRuntimeMapTest.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
{
    ProtostuffIOUtil.writeTo(out, message, schema, buf());
}
 
Example 13
Source File: ProtostuffRuntimeObjectSchemaTest.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
{
    ProtostuffIOUtil.writeTo(out, message, schema, buf());
}
 
Example 14
Source File: ProtostuffNullArrayElementInObjectArrayTest.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
{
    ProtostuffIOUtil.writeTo(out, message, schema, buf());
}
 
Example 15
Source File: ProtostuffRuntimeCollectionSchemaTest.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
{
    ProtostuffIOUtil.writeTo(out, message, schema, buf());
}