INT-1280 (partial) - Moved Converter implementations back to root package

This commit is contained in:
Gary Russell
2010-07-29 17:10:26 +00:00
parent 7dda54bb79
commit cb28603b42
8 changed files with 49 additions and 21 deletions

View File

@@ -14,16 +14,17 @@
* limitations under the License.
*/
package org.springframework.commons.serializer.java;
package org.springframework.commons.serializer;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.springframework.commons.serializer.DeserializationFailureException;
import org.springframework.commons.serializer.java.JavaStreamingConverter;
import org.springframework.core.convert.converter.Converter;
/**
* Delegates to a {@link JavaStreamingConverter} to deserialize data
* Delegates to a {@link InputStreamingConverter} (default is
* {@link JavaStreamingConverter}} to deserialize data
* in a byte[] to an object.
*
* @author Gary Russell
@@ -32,12 +33,13 @@ import org.springframework.core.convert.converter.Converter;
*/
public class DeserializingConverter implements Converter<byte[], Object> {
private JavaStreamingConverter converter = new JavaStreamingConverter();
private InputStreamingConverter<Object> streamingConverter
= new JavaStreamingConverter();
public Object convert(byte[] source) {
ByteArrayInputStream byteStream = new ByteArrayInputStream(source);
try {
return converter.convert(byteStream);
return streamingConverter.convert(byteStream);
}
catch (Exception e) {
try {
@@ -48,5 +50,13 @@ public class DeserializingConverter implements Converter<byte[], Object> {
}
}
/**
* Override the default {@link JavaStreamingConverter}
* @param streamingConverter the streamingConverter to set
*/
public void setStreamingConverter(InputStreamingConverter<Object> streamingConverter) {
this.streamingConverter = streamingConverter;
}
}

View File

@@ -14,18 +14,19 @@
* limitations under the License.
*/
package org.springframework.commons.serializer.java;
package org.springframework.commons.serializer;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import org.springframework.commons.serializer.SerializationFailureException;
import org.springframework.commons.serializer.java.JavaStreamingConverter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.Assert;
/**
* Delegates to a {@link JavaStreamingConverter} to serialize an object
* to a byte[]. Source object must implement {@link Serializable}.
* Delegates to a {@link OutputStreamingConverter} (default is
* {@link JavaStreamingConverter}) to serialize an object
* to a byte[].
*
* @author Gary Russell
* @since 2.0
@@ -33,18 +34,26 @@ import org.springframework.util.Assert;
*/
public class SerializingConverter implements Converter<Object, byte[]> {
private JavaStreamingConverter converter = new JavaStreamingConverter();
private OutputStreamingConverter<Object> streamingConverter
= new JavaStreamingConverter();
public byte[] convert(Object source) {
Assert.isTrue(source instanceof Serializable, this.getClass().getName()
+ " requires a Serializable payload, but received [" + source.getClass().getName() + "]");
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
try {
this.converter.convert(source, byteStream);
this.streamingConverter.convert(source, byteStream);
return byteStream.toByteArray();
} catch (Exception e) {
throw new SerializationFailureException("Failed to serialize", e);
}
}
/**
* Override the default {@link JavaStreamingConverter}
* @param streamingConverter the streamingConverter to set
*/
public void setStreamingConverter(
OutputStreamingConverter<Object> streamingConverter) {
this.streamingConverter = streamingConverter;
}
}

View File

@@ -21,9 +21,11 @@ import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import org.springframework.commons.serializer.InputStreamingConverter;
import org.springframework.commons.serializer.OutputStreamingConverter;
import org.springframework.util.Assert;
/**
@@ -51,8 +53,14 @@ public class JavaStreamingConverter
}
}
/**
* Source object must implement {@link Serializable}.
*/
public void convert(Object object, OutputStream outputStream)
throws IOException {
Assert.isTrue(object instanceof Serializable, this.getClass().getName()
+ " requires a Serializable payload, but received [" +
object.getClass().getName() + "]");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(object);
objectOutputStream.flush();

View File

@@ -1,6 +1,5 @@
/**
* Implementations of In/Out JavaStreamingConverter and
* Converter using standard Java Serialization.
* Implementation of In/Out JavaStreamingConverter.
*/
package org.springframework.commons.serializer.java;

View File

@@ -3,6 +3,7 @@
* Root package for Spring commons' serializer interfaces and implementations.
* Provides an abstraction over various serialization techniques.
* Includes exceptions for serialization and deserialization failures.
* Actual (de)serializers are in subpackages.
*/
package org.springframework.commons.serializer;

View File

@@ -16,13 +16,15 @@
package org.springframework.integration.transformer;
import org.springframework.commons.serializer.java.DeserializingConverter;
import org.springframework.commons.serializer.DeserializingConverter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.Assert;
/**
* Transformer that deserializes the inbound byte array payload to an object by delegating to a
* Converter&lt;byte[], Object&gt;. Default delegate is a {@link DeserializingConverter}.
* Converter&lt;byte[], Object&gt;. Default delegate is a {@link DeserializingConverter} using
* Java serialization.
*
* <p>The byte array payload must be a result of equivalent serialization.
*
* @author Mark Fisher

View File

@@ -16,13 +16,14 @@
package org.springframework.integration.transformer;
import org.springframework.commons.serializer.java.SerializingConverter;
import org.springframework.commons.serializer.SerializingConverter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.Assert;
/**
* Transformer that serializes the inbound payload into a byte array by delegating to a
* Converter&lt;Object, byte[]&gt;. Default delegate is a {@link SerializingConverter}.
* Converter&lt;Object, byte[]&gt;. Default delegate is a {@link SerializingConverter} using
* Java serialization.
*
* <p>The payload instance must be Serializable if the default converter is used.
*

View File

@@ -22,8 +22,6 @@ import static org.junit.Assert.fail;
import java.io.Serializable;
import org.junit.Test;
import org.springframework.commons.serializer.java.DeserializingConverter;
import org.springframework.commons.serializer.java.SerializingConverter;
/**