Package com.netflix.astyanax.serializers

Source Code of com.netflix.astyanax.serializers.ObjectSerializer

package com.netflix.astyanax.serializers;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;

import com.netflix.astyanax.Serializer;
import com.netflix.astyanax.connectionpool.exceptions.SerializationException;

/**
* The ObjectSerializer is used to turn objects into their binary
* representations.
*
* @author Bozhidar Bozhanov
*
*/
public class ObjectSerializer extends AbstractSerializer<Object> implements
        Serializer<Object> {

    private static final ObjectSerializer INSTANCE = new ObjectSerializer();

    @Override
    public ByteBuffer toByteBuffer(Object obj) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);
            oos.close();

            return ByteBuffer.wrap(baos.toByteArray());
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }

    @Override
    public Object fromByteBuffer(ByteBuffer bytes) {
        if ((bytes == null) || !bytes.hasRemaining()) {
            return null;
        }
        try {
            ByteBuffer dup = bytes.duplicate();
            int l = dup.remaining();
            ByteArrayInputStream bais = new ByteArrayInputStream(dup.array(),
                    dup.arrayOffset() + dup.position(), l);
            ObjectInputStream ois = new ObjectInputStream(bais);
            Object obj = ois.readObject();
            dup.position(dup.position() + (l - ois.available()));
            ois.close();
            return obj;
        } catch (Exception ex) {
            throw new SerializationException(ex);
        }
    }

    public static ObjectSerializer get() {
        return INSTANCE;
    }
}
TOP

Related Classes of com.netflix.astyanax.serializers.ObjectSerializer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.