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

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

package Alachisoft.NCache.Common.Caching;

import Alachisoft.NCache.Common.DataReader.TwoDimensionalArray;
import Alachisoft.NCache.Common.DataStructures.VirtualArray;
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;

public class SmallUserBinaryObject extends UserBinaryObject {
    private byte[] _data;
    private int _actualDataLength;

    public SmallUserBinaryObject() {
    }

    @Override
    public Object[] ClonePayload() {
        int length = getLength();
        if (length > 0) {
            byte[] clonedPayload = new byte[length];
            System.arraycopy(_data, 0, clonedPayload, 0, length);
            List ls = new ArrayList<>(1);
            ls.add(clonedPayload);
            return ls.toArray();
        }
        return null;
    }

    @Override
    public UserBinaryObject DeepClone() {
        byte[] clonedData = null;
        SmallUserBinaryObject clonedUserBinaryObject = new SmallUserBinaryObject();

        if (_data != null) {
            clonedData = new byte[_data.length];
            System.arraycopy(_data, 0, clonedData, 0, _actualDataLength);
        }

        clonedUserBinaryObject._data = clonedData;
        clonedUserBinaryObject._actualDataLength = _actualDataLength;
        return clonedUserBinaryObject;
    }

    @Override
    public VirtualArray Read(int offset, int length) {
        throw new UnsupportedOperationException("The method or operation is not implemented.");
    }

    @Override
    public void Write(VirtualArray vBuffer, int srcOffset, int dstOffset, int length) {
        throw new UnsupportedOperationException("The method or operation is not implemented.");
    }

    @Override
    public List getDataList() {
        byte[] bytes = _data;
        java.util.ArrayList byteList = new java.util.ArrayList(1);

        if (_data.length != getLength()) {
            bytes = new byte[getLength()];
            System.arraycopy(_data, 0, bytes, 0, getLength());
        }
        byteList.add(bytes);
        return byteList;
    }

    @Override
    public byte[] getFullObject() {
        byte[] fullByteArray = null;

        if (getLength() > 0) {
            fullByteArray = new byte[getLength()];
            System.arraycopy(_data, 0, fullByteArray, 0, getLength());
        }
        return fullByteArray;
    }

    @Override
    public byte[] getTwoDimensionalArray() {
        TwoDimensionalArray twoDimensionalArray = new TwoDimensionalArray(getLength());
        twoDimensionalArray.copyFromByteArray(_data);
        return twoDimensionalArray.toByteArray();
    }

    @Override
    public int getInMemorySize() {
        return getLength() + BYTE_ARRAY_MEMORY_OVERHEAD;
    }

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

    @Override
    public void initializeUserBinaryObject(Collection data) {
        if (data instanceof List) {
            if (((List)data).size() > 0) {
                initializeUserBinaryObject(((ByteString) (((List) data).get(0))).toByteArray());
            }
        } else {
            for (Object buffer : data) {
                initializeUserBinaryObject(((ByteString) buffer).toByteArray());
                break;
            }
        }
    }

    @Override
    public void initializeUserBinaryObject(byte[] data) {
        _data = data;
        _actualDataLength = _data.length;

        byte[] buffer = new byte[_actualDataLength];
        System.arraycopy(_data, 0, buffer, 0, _actualDataLength);
        _data = buffer;
    }

    @Override
    public void updateActualDataLength() {
        if (_actualDataLength == 0 && _data.length != 0) {
            _actualDataLength = _data.length;
        }
    }

    @Override
    public void deserialize(NCacheObjectInput reader) throws IOException, ClassNotFoundException
    {
        _data = (byte[])reader.readObject();
        _actualDataLength = reader.readInt();
    }

    @Override
    public void serialize(NCacheObjectOutput writer) throws IOException
    {
        writer.writeObject(_data);
        writer.write(_actualDataLength);
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy