Example usage for org.springframework.core.serializer Serializer serialize

List of usage examples for org.springframework.core.serializer Serializer serialize

Introduction

In this page you can find the example usage for org.springframework.core.serializer Serializer serialize.

Prototype

void serialize(T object, OutputStream outputStream) throws IOException;

Source Link

Document

Write an object of type T to the given OutputStream.

Usage

From source file:org.springdata.ehcache.convert.EhcacheMappingConverter.java

@Override
public void write(Object source, ByteArray sink) {

    if (source == null) {
        sink.setBytes(new byte[0]);
        return;//from   w  w  w .j a  v  a 2  s  .c  o  m
    }

    Class<?> beanClassLoaderClass = transformClassToBeanClassLoaderClass(source.getClass());
    EhcachePersistentEntity<?> entity = mappingContext.getPersistentEntity(beanClassLoaderClass);

    if (entity == null) {
        throw new MappingException("No mapping metadata found for " + source.getClass());
    }

    Serializer<Object> serializer = (Serializer<Object>) entity.getSerializer();

    ByteArrayOutputStream bytesStream = new ByteArrayOutputStream();
    try {
        serializer.serialize(source, bytesStream);
        sink.setBytes(bytesStream.toByteArray());
    } catch (IOException e) {
        throw new MappingException("serialization fail for " + beanClassLoaderClass, e);
    }

}