如何通过使用ByteBuffer查看标头偏移来创建字节数组?

时间:2014-10-21 00:56:32

标签: java c++ bytearray bytebuffer

我需要按照以下标题偏移格式为我的标题创建一个字节数组。

// 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,它是这样构成的 -

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);
}

我是否在上面定义的标题偏移上得到了正确的基础?

1 个答案:

答案 0 :(得分:1)

你很亲密。当records_partitionalready_replicated值使用int时,byteprivate 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); 

可替换地:

{{1}}

{{1}}