Source Taste: Spring Data: Redis: JdkSerializationRedisSerializer

PART 1:
package org.springframework.data.redis.serializer;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;

/**
 * Java Serialization Redis serializer.
 * Delegates to the default (Java based) serializer in Spring 3.
 * 
 * @author Mark Pollack
 * @author Costin Leau
 */
public class JdkSerializationRedisSerializer implements RedisSerializer<Object> {

        //Derek: Note that SerializingConverter is a class in Spring Core
	private Converter<Object, byte[]> serializer = new SerializingConverter();
	private Converter<byte[], Object> deserializer = new DeserializingConverter();

	public Object deserialize(byte[] bytes) {
		if (SerializationUtils.isEmpty(bytes)) {
			return null;
		}

		try {
			return deserializer.convert(bytes);
		} catch (Exception ex) {
			throw new SerializationException("Cannot deserialize", ex);
		}
	}
	
	public byte[] serialize(Object object) {
		if (object == null) {
			return SerializationUtils.EMPTY_ARRAY;
		}
		try {
			return serializer.convert(object);
		} catch (Exception ex) {
			throw new SerializationException("Cannot serialize", ex);
		}
	}
}

PART 2:

package org.springframework.core.serializer.support;

import java.io.ByteArrayOutputStream;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.DefaultSerializer;
import org.springframework.core.serializer.Serializer;
import org.springframework.util.Assert;

/**
 * A {@link Converter} that delegates to a {@link org.springframework.core.serializer.Serializer}
 * to convert an object to a byte array.
 *
 * @author Gary Russell
 * @author Mark Fisher
 * @since 3.0.5
 */
public class SerializingConverter implements Converter<Object, byte[]> {

	private final Serializer<Object> serializer;


	/**
	 * Create a default SerializingConverter that uses standard Java serialization.
	 */
	public SerializingConverter() {
                //Derek: consider DefaultSerializer
                this.serializer = new DefaultSerializer();
	}

	/**
	 * Create a SerializingConverter that delegates to the provided {@link Serializer}
	 */
	public SerializingConverter(Serializer<Object> serializer) {
		Assert.notNull(serializer, "Serializer must not be null");
		this.serializer = serializer;
	}


	/**
	 * Serializes the source object and returns the byte array result.
	 */
	public byte[] convert(Object source) {
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream(128);
		try  {
                        //Derek: note that source is for input(object), byteStream is for output(byte stream) 
                        this.serializer.serialize(source, byteStream);
			return byteStream.toByteArray();
		}
		catch (Throwable ex) {
			throw new SerializationFailedException("Failed to serialize object using " +
					this.serializer.getClass().getSimpleName(), ex);
		}
	}

}
PART 3:
package org.springframework.core.serializer;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

/**
 * Serializer that writes an object to an output stream using Java Serialization.
 * 
 * @author Gary Russell
 * @author Mark Fisher
 * @since 3.0.5
 */
public class DefaultSerializer implements Serializer<Object> {

	/**
	 * Writes the source object to an output stream using Java Serialization.
	 * The source object must implement {@link Serializable}.
	 */
	public void serialize(Object object, OutputStream outputStream) throws IOException {
		if (!(object instanceof Serializable)) {
			throw new IllegalArgumentException(getClass().getSimpleName() + " requires a Serializable payload " +
					"but received an object of type [" + object.getClass().getName() + "]");
		}
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
		objectOutputStream.writeObject(object);
		objectOutputStream.flush();
	}

}

CONTEMPLATE:

(1) Go further, how to deal with the 'web of objects'? deep copying?

(2) Any limit for this Serializer?

(3) Can be applied in Networking Serialization?

你可能感兴趣的:(Source Taste: Spring Data: Redis: JdkSerializationRedisSerializer)