@Test public void testDecodeProperties() { MessageExt messageExt = new MessageExt(); messageExt.setMsgId("645100FA00002A9F000000489A3AA09E"); messageExt.setTopic("abc"); messageExt.setBody("hello!q!".getBytes()); try { messageExt.setBornHost(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 0)); } catch (UnknownHostException e) { e.printStackTrace(); assertThat(Boolean.FALSE).isTrue(); } messageExt.setBornTimestamp(System.currentTimeMillis()); messageExt.setCommitLogOffset(123456); messageExt.setPreparedTransactionOffset(0); messageExt.setQueueId(0); messageExt.setQueueOffset(123); messageExt.setReconsumeTimes(0); try { messageExt.setStoreHost(new InetSocketAddress(InetAddress.getLocalHost(), 0)); } catch (UnknownHostException e) { e.printStackTrace(); assertThat(Boolean.FALSE).isTrue(); } messageExt.putUserProperty("a", "123"); messageExt.putUserProperty("b", "hello"); messageExt.putUserProperty("c", "3.14"); byte[] msgBytes = new byte[0]; try { msgBytes = MessageDecoder.encode(messageExt, false); } catch (Exception e) { e.printStackTrace(); assertThat(Boolean.FALSE).isTrue(); } ByteBuffer byteBuffer = ByteBuffer.allocate(msgBytes.length); byteBuffer.put(msgBytes); Map<String, String> properties = MessageDecoder.decodeProperties(byteBuffer); assertThat(properties).isNotNull(); assertThat("123").isEqualTo(properties.get("a")); assertThat("hello").isEqualTo(properties.get("b")); assertThat("3.14").isEqualTo(properties.get("c")); }
public static Map<String, String> decodeProperties(java.nio.ByteBuffer byteBuffer) { int sysFlag = byteBuffer.getInt(SYSFLAG_POSITION); int bornhostLength = (sysFlag & MessageSysFlag.BORNHOST_V6_FLAG) == 0 ? 8 : 20; int storehostAddressLength = (sysFlag & MessageSysFlag.STOREHOSTADDRESS_V6_FLAG) == 0 ? 8 : 20; int bodySizePosition = 4 + 4 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + bornhostLength + 8 + storehostAddressLength + 4 + 8; int topicLengthPosition = bodySizePosition + 4 + byteBuffer.getInt(bodySizePosition); byte topicLength = byteBuffer.get(topicLengthPosition); short propertiesLength = byteBuffer.getShort(topicLengthPosition + 1 + topicLength); byteBuffer.position(topicLengthPosition + 1 + topicLength + 2); if (propertiesLength > 0) { byte[] properties = new byte[propertiesLength]; byteBuffer.get(properties); String propertiesString = new String(properties, CHARSET_UTF8); Map<String, String> map = string2messageProperties(propertiesString); return map; } return null; }
MessageDecoder { public static Map<String, String> decodeProperties(java.nio.ByteBuffer byteBuffer) { int sysFlag = byteBuffer.getInt(SYSFLAG_POSITION); int bornhostLength = (sysFlag & MessageSysFlag.BORNHOST_V6_FLAG) == 0 ? 8 : 20; int storehostAddressLength = (sysFlag & MessageSysFlag.STOREHOSTADDRESS_V6_FLAG) == 0 ? 8 : 20; int bodySizePosition = 4 + 4 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + bornhostLength + 8 + storehostAddressLength + 4 + 8; int topicLengthPosition = bodySizePosition + 4 + byteBuffer.getInt(bodySizePosition); byte topicLength = byteBuffer.get(topicLengthPosition); short propertiesLength = byteBuffer.getShort(topicLengthPosition + 1 + topicLength); byteBuffer.position(topicLengthPosition + 1 + topicLength + 2); if (propertiesLength > 0) { byte[] properties = new byte[propertiesLength]; byteBuffer.get(properties); String propertiesString = new String(properties, CHARSET_UTF8); Map<String, String> map = string2messageProperties(propertiesString); return map; } return null; } }
MessageDecoder { public static Map<String, String> decodeProperties(java.nio.ByteBuffer byteBuffer) { int sysFlag = byteBuffer.getInt(SYSFLAG_POSITION); int bornhostLength = (sysFlag & MessageSysFlag.BORNHOST_V6_FLAG) == 0 ? 8 : 20; int storehostAddressLength = (sysFlag & MessageSysFlag.STOREHOSTADDRESS_V6_FLAG) == 0 ? 8 : 20; int bodySizePosition = 4 + 4 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + bornhostLength + 8 + storehostAddressLength + 4 + 8; int topicLengthPosition = bodySizePosition + 4 + byteBuffer.getInt(bodySizePosition); byte topicLength = byteBuffer.get(topicLengthPosition); short propertiesLength = byteBuffer.getShort(topicLengthPosition + 1 + topicLength); byteBuffer.position(topicLengthPosition + 1 + topicLength + 2); if (propertiesLength > 0) { byte[] properties = new byte[propertiesLength]; byteBuffer.get(properties); String propertiesString = new String(properties, CHARSET_UTF8); Map<String, String> map = string2messageProperties(propertiesString); return map; } return null; }  }
MessageDecoder { public static Map<String, String> decodeProperties(java.nio.ByteBuffer byteBuffer) { int sysFlag = byteBuffer.getInt(SYSFLAG_POSITION); int bornhostLength = (sysFlag & MessageSysFlag.BORNHOST_V6_FLAG) == 0 ? 8 : 20; int storehostAddressLength = (sysFlag & MessageSysFlag.STOREHOSTADDRESS_V6_FLAG) == 0 ? 8 : 20; int bodySizePosition = 4 + 4 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + bornhostLength + 8 + storehostAddressLength + 4 + 8; int topicLengthPosition = bodySizePosition + 4 + byteBuffer.getInt(bodySizePosition); byte topicLength = byteBuffer.get(topicLengthPosition); short propertiesLength = byteBuffer.getShort(topicLengthPosition + 1 + topicLength); byteBuffer.position(topicLengthPosition + 1 + topicLength + 2); if (propertiesLength > 0) { byte[] properties = new byte[propertiesLength]; byteBuffer.get(properties); String propertiesString = new String(properties, CHARSET_UTF8); Map<String, String> map = string2messageProperties(propertiesString); return map; } return null; }  static String createMessageId(final ByteBuffer input, final ByteBuffer addr, final long offset); static String createMessageId(SocketAddress socketAddress, long transactionIdhashCode); static MessageId decodeMessageId(final String msgId); static Map<String, String> decodeProperties(java.nio.ByteBuffer byteBuffer); static MessageExt decode(java.nio.ByteBuffer byteBuffer); static MessageExt clientDecode(java.nio.ByteBuffer byteBuffer, final boolean readBody); static MessageExt decode(java.nio.ByteBuffer byteBuffer, final boolean readBody); static byte[] encode(MessageExt messageExt, boolean needCompress); static MessageExt decode(
        java.nio.ByteBuffer byteBuffer, final boolean readBody, final boolean deCompressBody); static MessageExt decode(
        java.nio.ByteBuffer byteBuffer, final boolean readBody, final boolean deCompressBody, final boolean isClient); static List<MessageExt> decodes(java.nio.ByteBuffer byteBuffer); static List<MessageExt> decodes(java.nio.ByteBuffer byteBuffer, final boolean readBody); static String messageProperties2String(Map<String, String> properties); static Map<String, String> string2messageProperties(final String properties); static byte[] encodeMessage(Message message); static Message decodeMessage(ByteBuffer byteBuffer); static byte[] encodeMessages(List<Message> messages); static List<Message> decodeMessages(ByteBuffer byteBuffer); }
MessageDecoder { public static Map<String, String> decodeProperties(java.nio.ByteBuffer byteBuffer) { int sysFlag = byteBuffer.getInt(SYSFLAG_POSITION); int bornhostLength = (sysFlag & MessageSysFlag.BORNHOST_V6_FLAG) == 0 ? 8 : 20; int storehostAddressLength = (sysFlag & MessageSysFlag.STOREHOSTADDRESS_V6_FLAG) == 0 ? 8 : 20; int bodySizePosition = 4 + 4 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + bornhostLength + 8 + storehostAddressLength + 4 + 8; int topicLengthPosition = bodySizePosition + 4 + byteBuffer.getInt(bodySizePosition); byte topicLength = byteBuffer.get(topicLengthPosition); short propertiesLength = byteBuffer.getShort(topicLengthPosition + 1 + topicLength); byteBuffer.position(topicLengthPosition + 1 + topicLength + 2); if (propertiesLength > 0) { byte[] properties = new byte[propertiesLength]; byteBuffer.get(properties); String propertiesString = new String(properties, CHARSET_UTF8); Map<String, String> map = string2messageProperties(propertiesString); return map; } return null; }  static String createMessageId(final ByteBuffer input, final ByteBuffer addr, final long offset); static String createMessageId(SocketAddress socketAddress, long transactionIdhashCode); static MessageId decodeMessageId(final String msgId); static Map<String, String> decodeProperties(java.nio.ByteBuffer byteBuffer); static MessageExt decode(java.nio.ByteBuffer byteBuffer); static MessageExt clientDecode(java.nio.ByteBuffer byteBuffer, final boolean readBody); static MessageExt decode(java.nio.ByteBuffer byteBuffer, final boolean readBody); static byte[] encode(MessageExt messageExt, boolean needCompress); static MessageExt decode(
        java.nio.ByteBuffer byteBuffer, final boolean readBody, final boolean deCompressBody); static MessageExt decode(
        java.nio.ByteBuffer byteBuffer, final boolean readBody, final boolean deCompressBody, final boolean isClient); static List<MessageExt> decodes(java.nio.ByteBuffer byteBuffer); static List<MessageExt> decodes(java.nio.ByteBuffer byteBuffer, final boolean readBody); static String messageProperties2String(Map<String, String> properties); static Map<String, String> string2messageProperties(final String properties); static byte[] encodeMessage(Message message); static Message decodeMessage(ByteBuffer byteBuffer); static byte[] encodeMessages(List<Message> messages); static List<Message> decodeMessages(ByteBuffer byteBuffer); final static Charset CHARSET_UTF8; final static int MESSAGE_MAGIC_CODE_POSTION; final static int MESSAGE_FLAG_POSTION; final static int MESSAGE_PHYSIC_OFFSET_POSTION; final static int MESSAGE_MAGIC_CODE; static final char NAME_VALUE_SEPARATOR; static final char PROPERTY_SEPARATOR; static final int PHY_POS_POSITION; static final int SYSFLAG_POSITION; }