Мне нужно сделать один байтовый массив для моих заголовков, следуя приведенному ниже формату смещений заголовков.
// 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;
А также CustomerAddress
является typedef для uint64_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);
}
Правильно ли я понял, основываясь на смещении заголовка, которое я определил выше?
Вы близки. Ваш records_partition
а также already_replicated
значения используют int
когда они должны использовать byte
вместо.
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);