blob: 7bd8509c1ebd75ea5bdb589ebc5c82ded41995a7 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 1998, 2012 Oracle and/or its affiliates. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
* which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Oracle - initial impl
******************************************************************************/
package example;
import model.Order;
import model.OrderLine;
import com.tangosol.io.ByteArrayReadBuffer;
import com.tangosol.io.ByteArrayWriteBuffer;
import com.tangosol.io.ReadBuffer.BufferInput;
import com.tangosol.io.WriteBuffer.BufferOutput;
import com.tangosol.io.pof.PofContext;
import com.tangosol.io.pof.PortableObjectSerializer;
import com.tangosol.io.pof.SimplePofContext;
/**
* Uses Coherence POF to serialize the object.
* @author James Sutherland
*/
public class POFSerializer implements Serializer {
PofContext context;
public POFSerializer() {
SimplePofContext simpleContext = new SimplePofContext();
simpleContext.registerUserType(1, Order.class, new PortableObjectSerializer(1));
simpleContext.registerUserType(2, OrderLine.class, new PortableObjectSerializer(2));
simpleContext.setReferenceEnabled(true);
this.context = simpleContext;
}
public POFSerializer(PofContext context) {
this.context = context;
}
public byte[] serialize(Object object) {
try {
BufferOutput output = new ByteArrayWriteBuffer(1000).getBufferOutput();
this.context.serialize(output, object);
output.close();
return output.getBuffer().toByteArray();
} catch (Exception exception) {
throw new RuntimeException(exception);
}
}
public Object deserialize(byte[] bytes) {
try {
BufferInput input = new ByteArrayReadBuffer(bytes).getBufferInput();
Object result = this.context.deserialize(input);
input.close();
return result;
} catch (Exception exception) {
throw new RuntimeException(exception);
}
}
public String toString() {
return getClass().getSimpleName();
}
}