All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.astyanax.serializers.BytesArraySerializer Maven / Gradle / Ivy

There is a newer version: 3.10.2
Show newest version
package com.netflix.astyanax.serializers;

import java.nio.ByteBuffer;

import org.apache.cassandra.db.marshal.BytesType;

import com.netflix.astyanax.Serializer;

/**
 * A BytesArraySerializer translates the byte[] to and from ByteBuffer.
 * 
 * @author Patricio Echague
 * 
 */
public final class BytesArraySerializer extends AbstractSerializer implements Serializer {

    private static final BytesArraySerializer instance = new BytesArraySerializer();

    public static BytesArraySerializer get() {
        return instance;
    }

    @Override
    public ByteBuffer toByteBuffer(byte[] obj) {
        if (obj == null) {
            return null;
        }
        return ByteBuffer.wrap(obj);
    }

    @Override
    public byte[] fromByteBuffer(ByteBuffer byteBuffer) {
            if (byteBuffer == null) {
                return null;
            }
            ByteBuffer dup = byteBuffer.duplicate();
            byte[] bytes = new byte[dup.remaining()];
            byteBuffer.get(bytes, 0, bytes.length);
            return bytes;
    }

    @Override
    public ByteBuffer fromString(String str) {
        return BytesType.instance.fromString(str);
    }

    @Override
    public String getString(ByteBuffer byteBuffer) {
	    if (byteBuffer == null) return null;
            return BytesType.instance.getString(byteBuffer.duplicate());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy