Sunday 15 March 2015

java - How to make a byte arrary by looking at the header offsets using ByteBuffer? -



java - How to make a byte arrary by looking at the header offsets using ByteBuffer? -

i need create 1 byte array headers next below header offsets format.

// below header offsets // m_off_addressed_center must 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; // total size of header static constexpr uint32_t m_head_offset = m_off_already_replicated + 1;

and customeraddress typedef uint64_t , made -

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

below have started , not sure whether got right?

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

and below method client_data

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

does got right basis on header offset have defined above?

you close. records_partition , already_replicated values using int when should using byte instead.

private static long client_data(byte datacenter, short client_id, byte data_id, int data_counter) { homecoming (((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);

alternatively:

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 c++ bytearray bytebuffer

No comments:

Post a Comment