com.aliyun.datahub.client.impl.batch.header.BatchHeaderV1 Maven / Gradle / Ivy
The newest version!
package com.aliyun.datahub.client.impl.batch.header;
import com.aliyun.datahub.client.exception.DatahubClientException;
import java.io.ByteArrayInputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class BatchHeaderV1 extends BatchHeader {
private final static int BATCH_HEADER_SIZE = 36;
private static final ThreadLocal BYTE_BUFFER = ThreadLocal.withInitial(() -> ByteBuffer.allocate(BATCH_HEADER_SIZE).order(ByteOrder.LITTLE_ENDIAN));
public BatchHeaderV1() {
version = 1;
dataOffset = BATCH_HEADER_SIZE;
}
@Override
public byte[] serialize() {
ByteBuffer byteBuffer = BYTE_BUFFER.get();
byteBuffer.clear();
byteBuffer.put(MAGIC_NUMBER);
byteBuffer.putInt(version);
byteBuffer.putInt(length);
byteBuffer.putInt(rawDataSize);
byteBuffer.putInt(crc32);
byteBuffer.putShort(attributes);
byteBuffer.putShort(dataType);
byteBuffer.putInt(schemaVersion);
byteBuffer.putInt(dataOffset);
byteBuffer.putInt(recordCount);
return byteBuffer.array();
}
@Override
public void deserialize(byte[] bytes) {
ByteBuffer byteBuffer = BYTE_BUFFER.get();
ByteArrayInputStream input = new ByteArrayInputStream(bytes);
byte[] buffer = new byte[BATCH_HEADER_SIZE];
int len = input.read(buffer, 0, BATCH_HEADER_SIZE);
if (len < BATCH_HEADER_SIZE) {
throw new DatahubClientException("read batch header fail");
}
byteBuffer.clear();
byteBuffer.put(buffer);
byteBuffer.flip();
byteBuffer.getInt(); // magic
byteBuffer.getInt(); // version
length = byteBuffer.getInt();
rawDataSize = byteBuffer.getInt();
crc32 = byteBuffer.getInt();
attributes = byteBuffer.getShort();
dataType = byteBuffer.getShort();
schemaVersion = byteBuffer.getInt();
dataOffset = byteBuffer.getInt();
recordCount = byteBuffer.getInt();
}
@Override
public int getHeaderSize() {
return BATCH_HEADER_SIZE;
}
}