gpt4 book ai didi

java - 如何使用 ByteBuffer 查看 header 偏移量来制作字节数组?

转载 作者:行者123 更新时间:2023-11-28 06:37:58 30 4
gpt4 key购买 nike

我需要按照以下 header 偏移格式为我的 header 制作一个字节数组。

// below is the header offsets

// m_off_addressed_center must be the first byte
static constexpr uint32_t m_off_addressed_center = 0;
static constexpr uint32_t m_off_record_version = m_off_addressed_center + 1;
static constexpr uint32_t m_off_num_records = m_off_record_version + 1;
static constexpr uint32_t m_off_buffer_used = m_off_num_records + sizeof(uint32_t);
static constexpr uint32_t m_off_address = m_off_buffer_used + sizeof(uint32_t);
static constexpr uint32_t m_off_address_from = m_off_address + sizeof(CustomerAddress);
static constexpr uint32_t m_off_records_partition = m_off_address_from + sizeof(CustomerAddress);
static constexpr uint32_t m_off_already_replicated = m_off_records_partition + 1;

// this is the full size of the header
static constexpr uint32_t m_head_offset = m_off_already_replicated + 1;

CustomerAddressuint64_t 的类型定义,它是这样组成的 -

typedef uint64_t   CustomerAddress;

void client_data(uint8_t datacenter,
uint16_t client_id,
uint8_t data_id,
uint32_t data_counter,
CustomerAddress& customer_address)
{
customer_address = (uint64_t(datacenter) << 56)
+ (uint64_t(client_id) << 40)
+ (uint64_t(data_id) << 32)
+ data_counter;
}

下面是我已经开始的,我不确定我是否做对了一切?

ByteBuffer b = ByteBuffer.allocate(256 * 256); // allocating 64k buffer
b.order(ByteOrder.BIG_ENDIAN);

// header layout
int m_off_addressed_center = 1;
int m_off_record_version = 2;
int m_off_num_records = 1;
int m_off_buffer_used = 100;

long m_off_address = client_data((byte) 10, (short) 12, (byte) 30, 200);
long m_off_address_from = client_data((byte) 20, (short) 22, (byte) 40, 150);

int m_off_records_partition = 10;
int m_off_already_replicated = 20;

b.putInt(m_off_addressed_center);
b.putInt(m_off_record_version);
b.putInt(m_off_num_records);
b.putInt(m_off_buffer_used);

b.putLong(m_off_address);
b.putLong(m_off_address_from);

b.putInt(m_off_records_partition);
b.putInt(m_off_already_replicated);

byte[] result = b.array();
System.out.println(result);

下面是我的方法client_data

private static long client_data(byte datacenter, short client_id, byte data_id, int data_counter) {
return ((long) (datacenter) << 56) | ((long) client_id << 40) | ((long) data_id << 32) | ((long) data_counter);
}

根据上面定义的 header 偏移量,我是否得到了正确的一切?

最佳答案

你很接近。您的 records_partitionalready_replicated 值在应该使用 byte 时使用了 int

private static long client_data(byte datacenter, short client_id, byte data_id, int data_counter)
{
return (((long) datacenter) << 56) |
(((long) client_id) << 40) |
(((long) data_id) << 32) |
((long) data_counter);
}

ByteBuffer b = ByteBuffer.allocate(28);
b.order(ByteOrder.BIG_ENDIAN);

// header layout
byte addressed_center = 1;
byte record_version = 2;
int num_records = 1;
int buffer_used = 100;
long address = client_data((byte) 10, (short) 12, (byte) 30, (in) 200);
long address_from = client_data((byte) 20, (short) 22, (byte) 40, (int) 150);
byte records_partition = 10;
byte already_replicated = 20;

b.put( addressed_center);
b.put( record_version);
b.putInt( num_records);
b.putInt( buffer_used);
b.putLong( address);
b.putLong( address_from);
b.put( records_partition);
b.put( already_replicated);

byte[] result = b.array();
System.out.println(result);

或者:

private final static int m_off_addressed_center           = 0;
private final static int m_off_record_version = m_off_addressed_center + 1;
private final static int m_off_num_records = m_off_record_version + 1;
private final static int m_off_buffer_used = m_off_num_records + 4;
private final static int m_off_address = m_off_buffer_used + 4;
private final static int m_off_address_from = m_off_address + 8;
private final static int m_off_records_partition = m_off_address_from + 8;
private final static int m_off_already_replicated = m_off_records_partition + 1;

private final static int m_head_offset = m_off_already_replicated + 1;

ByteBuffer b = ByteBuffer.allocate(m_head_offset);
b.order(ByteOrder.BIG_ENDIAN);

// header layout
byte addressed_center = 1;
byte record_version = 2;
int num_records = 1;
int buffer_used = 100;
long address = client_data((byte) 10, (short) 12, (byte) 30, (in) 200);
long address_from = client_data((byte) 20, (short) 22, (byte) 40, (int) 150);
byte records_partition = 10;
byte already_replicated = 20;

b.put( m_off_addressed_center, addressed_center);
b.put( m_off_record_version, record_version);
b.putInt( m_off_num_records, num_records);
b.putInt( m_off_buffer_used, buffer_used);
b.putLong( m_off_address, address);
b.putLong( m_off_address_from, address_from);
b.put( m_off_records_partition, records_partition);
b.put( m_off_already_replicated, already_replicated);

byte[] result = b.array();
System.out.println(result);

关于java - 如何使用 ByteBuffer 查看 header 偏移量来制作字节数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26477340/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com