/** * 消息位置序列化为: * * <ul> * <li>8个字节的offset * <li>4个字节的长度 * <li>4个字节checksum:这是指整个消息存储数据的checksum,跟message的checksum不同 * <li>4个字节长度,存储的分区名的长度 * <li>存储的分区名 * </ul> * * @return */ public ByteBuffer encode() { if (this.buf == null) { final byte[] storeDescBytes = ByteUtils.getBytes(this.storeDesc); final ByteBuffer buf = ByteBuffer.allocate(4 + 4 + 8 + 4 + this.storeDesc.length()); buf.putLong(this.getOffset()); buf.putInt(this.getLength()); buf.putInt(this.checksum); buf.putInt(storeDescBytes.length); buf.put(storeDescBytes); buf.flip(); this.buf = buf; } return this.buf; }
/** * 将消息属性和消息payload打包,结构如下:</br></br> 0或者1个定长attribute + payload * * @param message * @return */ public static final byte[] encodePayload(final Message message) { final byte[] payload = message.getData(); final String attribute = message.getAttribute(); byte[] attrData = null; if (attribute != null) { attrData = ByteUtils.getBytes(attribute); } else { return payload; } final int attrLen = attrData == null ? 0 : attrData.length; final ByteBuffer buffer = ByteBuffer.allocate(4 + attrLen + payload.length); if (attribute != null) { buffer.putInt(attrLen); if (attrData != null) { buffer.put(attrData); } } buffer.put(payload); return buffer.array(); }