Added PayloadSerializingTransformer and PayloadDeserializingTransformer (INT-513).

This commit is contained in:
Mark Fisher
2008-12-12 20:26:30 +00:00
parent 7c6ac19325
commit b1881f7b95
5 changed files with 274 additions and 1 deletions

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.transformer;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;
/**
* Transformer that deserializes the inbound byte array payload to an object.
* <p>The byte array payload must be a result of serialization.
*
* @author Mark Fisher
* @since 1.0.1
*/
public class PayloadDeserializingTransformer extends AbstractPayloadTransformer<byte[], Object> {
@Override
protected Object transformPayload(byte[] payload) throws Exception {
ByteArrayInputStream byteStream = new ByteArrayInputStream(payload);
ObjectInputStream objectStream = null;
try {
objectStream = new ObjectInputStream(byteStream);
return objectStream.readObject();
}
catch (ObjectStreamException e) {
throw new IllegalArgumentException(
"Failed to deserialize payload. Is the byte array a result of Object serialization?", e);
}
finally {
try {
objectStream.close();
}
catch (Exception e) {
// ignore
}
}
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.transformer;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import org.springframework.util.Assert;
/**
* Transformer that serializes the inbound payload into a byte array.
* <p>The payload instance must be Serializable.
*
* @author Mark Fisher
* @since 1.0.1
*/
public class PayloadSerializingTransformer extends AbstractPayloadTransformer<Object, byte[]> {
@Override
protected byte[] transformPayload(Object payload) throws Exception {
Assert.isTrue(payload instanceof Serializable, this.getClass().getName()
+ " requires a Serializable payload, but received [" + payload.getClass().getName() + "]");
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
objectStream.writeObject(payload);
objectStream.flush();
objectStream.close();
return byteStream.toByteArray();
}
}