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

com.alachisoft.ncache.client.internal.util.VirtualArray Maven / Gradle / Ivy

There is a newer version: 5.3.0
Show newest version
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.alachisoft.ncache.client.internal.util;

import com.alachisoft.ncache.runtime.util.HelperFxn;
import com.google.protobuf.ByteString;

import java.util.List;

/**
 * @author Asad Hasnain
 */
public class VirtualArray {

    private List _baseArray;
    private long _size;

    public VirtualArray(List array) {
        _baseArray = array;
        for (int i = 0; i < array.size(); i++) {
            byte[] tmp = array.get(i).toByteArray();
            if (tmp != null) _size += tmp.length;
        }
    }


    public int CopyData(byte[] buffer, int offset, int length)
            throws IllegalArgumentException {
        if (offset + length > buffer.length)
            throw new IllegalArgumentException("Length plus offset is greater than buffer size");

        int dataToCopy = (int) (length >= _size ? _size : length);
        int dataCopied = dataToCopy;
        int i = 0;

        while (dataToCopy > 0) {
            byte[] binarChunk = (byte[]) _baseArray.get(i).toByteArray();
            if (binarChunk != null) {
                int copyCount = Math.min(binarChunk.length, dataToCopy);

                HelperFxn.BlockCopy(binarChunk, 0, buffer, offset, copyCount);
                offset += copyCount;
                dataToCopy -= copyCount;
            }
            i++;
        }
        return dataCopied;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy