org.apache.pulsar.shade.com.yahoo.sketches.ArrayOfLongsSerDe Maven / Gradle / Ivy
/*
* Copyright 2015-16, Yahoo! Inc.
* Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
*/
package org.apache.pulsar.shade.com.yahoo.sketches;
import org.apache.pulsar.shade.com.yahoo.memory.Memory;
import org.apache.pulsar.shade.com.yahoo.memory.NativeMemory;
import org.apache.pulsar.shade.com.yahoo.memory.UnsafeUtil;
/**
* Methods of serializing and deserializing arrays of Long.
*
* @author Alexander Saydakov
*/
public class ArrayOfLongsSerDe extends ArrayOfItemsSerDe {
@Override
public byte[] serializeToByteArray(final Long[] items) {
final byte[] bytes = new byte[Long.BYTES * items.length];
final Memory mem = new NativeMemory(bytes);
long offsetBytes = 0;
for (int i = 0; i < items.length; i++) {
mem.putLong(offsetBytes, items[i]);
offsetBytes += Long.BYTES;
}
return bytes;
}
@Override
public Long[] deserializeFromMemory(final Memory mem, final int length) {
UnsafeUtil.checkBounds(0, (long)length * Long.BYTES, mem.getCapacity());
final Long[] array = new Long[length];
long offsetBytes = 0;
for (int i = 0; i < length; i++) {
array[i] = mem.getLong(offsetBytes);
offsetBytes += Long.BYTES;
}
return array;
}
}