Alachisoft.NCache.Common.Caching.LargeUserBinaryObject Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nc-common Show documentation
Show all versions of nc-common Show documentation
Internal package of Alachisoft.
package Alachisoft.NCache.Common.Caching;
import Alachisoft.NCache.Common.DataReader.TwoDimensionalArray;
import Alachisoft.NCache.Common.DataStructures.VirtualArray;
import Alachisoft.NCache.Common.DataStructures.VirtualIndex;
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.Iterator;
import java.util.List;
public class LargeUserBinaryObject extends UserBinaryObject {
private int _index;
private int _noOfChunks;
private int _lastChunkSize;
private ArrayList _data = new ArrayList();
public LargeUserBinaryObject() {
}
public static UserBinaryObject createUserBinaryObject(byte[] byteArray) {
return null;
}
@Override
public Object[] ClonePayload() {
if (_data == null || getLength() <= 0)
return null;
int i = 0;
int numChunks = _data.size();
java.util.ArrayList clonedBuffers = new java.util.ArrayList(numChunks);
for (i = 0; i < numChunks - 1; i++) {
if (_data.get(i) instanceof byte[]) {
byte[] clonedBuffer = new byte[((byte[]) _data.get(i)).length];
System.arraycopy(_data.get(i), 0, clonedBuffer, 0, ((byte[]) _data.get(i)).length);
clonedBuffers.add(clonedBuffer);
}
}
if (_data.get(i) instanceof byte[]) {
byte[] clonedLastBuffer = new byte[_lastChunkSize];
System.arraycopy(_data.get(i), 0, clonedLastBuffer, 0, _lastChunkSize);
clonedBuffers.add(clonedLastBuffer);
}
return clonedBuffers.toArray();
}
@Override
public UserBinaryObject DeepClone() {
return null;
}
@Override
public VirtualArray Read(int offset, int length) {
VirtualArray vBuffer = null;
int streamLength = getLength();
if (offset >= streamLength)
return new VirtualArray(0);
if (offset + length > streamLength)
length -= (offset + length - streamLength);
VirtualArray vSrc = new VirtualArray(_data);
vBuffer = new VirtualArray(length);
VirtualIndex vSrcIndex = new VirtualIndex(offset);
VirtualIndex vDstIndex = new VirtualIndex();
VirtualArray.CopyData(vSrc, vSrcIndex, vBuffer, vDstIndex, length);
return vBuffer;
}
@Override
public void Write(VirtualArray vBuffer, int srcOffset, int dstOffset, int length) {
if (vBuffer == null)
return;
VirtualArray vDstArray = new VirtualArray(_data);
VirtualArray.CopyData(vBuffer, new VirtualIndex(srcOffset), vDstArray, new VirtualIndex(dstOffset), length, true);
_noOfChunks = _data.size();
_lastChunkSize += (length < 0 ? 0 : length) % LARGE_OBJECT_SIZE;
}
private void addDataChunk(byte[] dataChunk) {
if (_data != null && _index < _noOfChunks) {
_data.add(_index, dataChunk);
_index++;
}
}
@Override
public List getDataList() {
int i = 0;
java.util.ArrayList byteList = new java.util.ArrayList(_noOfChunks);
if (getLength() > 0) {
// _noOfChunks --> _data.Count
for (i = 0; i < _noOfChunks - 1; i++) {
byteList.add((byte[]) _data.get(i));
}
byte[] buffer = (byte[]) _data.get(i);
if (buffer.length != _lastChunkSize) {
buffer = new byte[_lastChunkSize];
System.arraycopy((byte[]) _data.get(i), 0, buffer, 0, _lastChunkSize);
}
byteList.add(buffer);
}
return byteList;
}
@Override
public byte[] getFullObject() {
byte[] fullByteArray = null;
if (getLength() > 0) {
fullByteArray = new byte[getLength()];
int nextIndex = 0;
byte[] binaryChunk = null;
for (int i = 0; i < _data.size() - 1; i++) {
binaryChunk = (byte[]) _data.get(i);
if (binaryChunk != null) {
System.arraycopy(binaryChunk, 0, fullByteArray, nextIndex, binaryChunk.length);
nextIndex += binaryChunk.length;
}
}
System.arraycopy((byte[]) _data.get(_noOfChunks - 1), 0, fullByteArray, nextIndex, _lastChunkSize);
}
return fullByteArray;
}
@Override
public byte[] getTwoDimensionalArray() {
TwoDimensionalArray twoDimensionalArray = new TwoDimensionalArray(getLength());
int nextIndex = 0;
byte[] binarChunk = null;
if (getLength() > 0)
{
for (int i = 0; i < _data.size() - 1; i++)
{
binarChunk = (byte[]) _data.get(i);
if (binarChunk != null)
{
twoDimensionalArray.copyFromByteArray(binarChunk);
nextIndex += binarChunk.length;
}
}
twoDimensionalArray.copyFromByteArray((byte[]) _data.get(_noOfChunks - 1));
}
return twoDimensionalArray.toByteArray();
}
@Override
public int getInMemorySize() {
return getLength() + (_noOfChunks * BYTE_ARRAY_MEMORY_OVERHEAD);
}
@Override
public int getLength() {
return _noOfChunks == 0 ? _lastChunkSize : ((_noOfChunks - 1) * LARGE_OBJECT_SIZE) + _lastChunkSize;
}
@Override
public void setLength(int value) {
}
@Override
public void initializeUserBinaryObject(Collection data) {
_noOfChunks = data.size();
// The following code contains duplication of certain code segments. This
// note serves the purpose of conveying the fact that the duplication is
// intended.
if (data instanceof java.util.List) {
List buffers = (List) data;
int i = 0;
ByteString bs;
for (; i < buffers.size() && i < _noOfChunks - 1; i++) {
bs= (ByteString) buffers.get(i);
// byte[] chunk = (byte[]) buffers.get(i);
byte[] buffer = bs.toByteArray();
_data.add(buffer);
}
bs = (ByteString) buffers.get(i);
byte[] lastBuffer = bs.toByteArray();
_lastChunkSize = bs.size();
_data.add(lastBuffer);
} else {
Iterator enumerator = data.iterator();
for (int i = 0; enumerator.hasNext() && i < _noOfChunks - 1; i++) {
byte[] chunk = (byte[]) enumerator.next();
byte[] buffer = chunk;
_data.add(buffer);
}
byte[] lastChunk = (byte[]) enumerator.next();
byte[] lastBuffer = lastChunk;
_lastChunkSize = lastChunk.length;
_data.add(lastBuffer);
}
}
@Override
public void initializeUserBinaryObject(byte[] data) {
int nextChunk = 0;
int nextChunkSize = 0;
int noOfChunks = data.length / LARGE_OBJECT_SIZE;
noOfChunks += (data.length - (noOfChunks * LARGE_OBJECT_SIZE)) != 0 ? 1 : 0;
_noOfChunks = noOfChunks;
for (int i = 1; i <= noOfChunks - 1; i++) {
nextChunkSize = data.length - nextChunk;
if (nextChunkSize > LARGE_OBJECT_SIZE) {
nextChunkSize = LARGE_OBJECT_SIZE;
}
byte[] binaryChunk = new byte[nextChunkSize];
System.arraycopy(data, nextChunk, binaryChunk, 0, nextChunkSize);
addDataChunk(binaryChunk);
nextChunk += nextChunkSize;
}
_lastChunkSize = data.length - nextChunk;
if (_lastChunkSize > LARGE_OBJECT_SIZE) {
_lastChunkSize = LARGE_OBJECT_SIZE;
}
byte[] lastBinaryChunk = new byte[_lastChunkSize];
System.arraycopy(data, nextChunk, lastBinaryChunk, 0, _lastChunkSize);
addDataChunk(lastBinaryChunk);
}
@Override
public void updateActualDataLength() {
}
@Override
public void deserialize(NCacheObjectInput reader) throws IOException, ClassNotFoundException
{
_lastChunkSize = reader.readInt();
_noOfChunks = reader.readInt();
_index = reader.readInt();
if (_noOfChunks > 0)
{
_data = new ArrayList(_noOfChunks);
for (int i = 0; i < _noOfChunks; i++)
{
_data.add(i, (byte[])reader.readObject());
}
}
}
@Override
public void serialize(NCacheObjectOutput writer) throws IOException
{
writer.write(_lastChunkSize);
writer.write(_noOfChunks);
writer.write(_index);
for (int i = 0; i < _noOfChunks; i++)
{
writer.writeObject(_data.get(i));
}
}
}