com.alachisoft.ncache.client.internal.util.VirtualArray Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ncache-professional-client Show documentation
Show all versions of ncache-professional-client Show documentation
NCache Professional client for java.
/*
* 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;
}
}