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

Alachisoft.NCache.Common.Caching.UserBinaryObject Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Alachisoft.NCache.Common.Caching;

import Alachisoft.NCache.Common.DataReader.TwoDimensionalArray;
import Alachisoft.NCache.Common.DataStructures.IStreamItem;
import Alachisoft.NCache.Common.ISizable;
import com.alachisoft.ncache.serialization.core.io.ICompactSerializable;
import com.alachisoft.ncache.serialization.core.io.NCacheObjectInput;
import com.alachisoft.ncache.serialization.core.io.NCacheObjectOutput;
import com.google.protobuf.ByteString;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author Administrator
 */
public abstract class UserBinaryObject implements IStreamItem, ISizable,java.io.Serializable, ICompactSerializable {

    public static final int LARGE_OBJECT_SIZE = 79 * 1024;
    public static final int BYTE_ARRAY_MEMORY_OVERHEAD = 24;

    private int noOfChunks;
    private int index;
    private List data;

    public static UserBinaryObject createUserBinaryObject(Collection data) {
        if(data == null || data.isEmpty())
            return null;
        UserBinaryObject binaryObject = null;
        if(data.size() > 1)
            binaryObject = new LargeUserBinaryObject();
        else
            binaryObject = new SmallUserBinaryObject();

        if(binaryObject != null)
            binaryObject.initializeUserBinaryObject(data);

        return binaryObject;
    }

    public abstract void updateActualDataLength();

    public static UserBinaryObject createUserBinaryObject(byte[] byteArray) {
        if(byteArray == null)
            return null;

        float noOfChunks = (float) byteArray.length / LARGE_OBJECT_SIZE;

        UserBinaryObject binaryObject = null;
        if(noOfChunks > 1)
            binaryObject = new LargeUserBinaryObject();
        else
            binaryObject = new SmallUserBinaryObject();

        if(binaryObject != null)
            binaryObject.initializeUserBinaryObject(byteArray);

        return binaryObject;
    }

    public abstract void initializeUserBinaryObject(Collection data);

    public abstract void initializeUserBinaryObject(byte[] data);

    public Object[] getData() {
        return getDataList().toArray();
    }

    public abstract List getDataList();

    public abstract byte[] getFullObject();

    public abstract byte[] getTwoDimensionalArray();

    @Override
    public int getSize() {
        return getLength();
    }

    /**
     This method always creates a new instance of user's payload and copies the data
     to it. Keep in mind that the instance created is not from pool.

     @return The cloned payload.
     */
    public abstract Object[] ClonePayload();

    public abstract UserBinaryObject DeepClone();

    @Override
    public void setLength(int value) {

    }

    //region ICompactSerializable Members

    public abstract void deserialize(NCacheObjectInput reader) throws IOException, ClassNotFoundException;

    public abstract void serialize(NCacheObjectOutput writer) throws IOException;
    //.endregion
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy