Exemplo n.º 1
0
  /** 客户端使用,SLAVE也会使用 */
  public static MessageExt decode(java.nio.ByteBuffer byteBuffer, final boolean readBody) {
    try {
      MessageExt msgExt = new MessageExt();

      // 1 TOTALSIZE
      int storeSize = byteBuffer.getInt();
      msgExt.setStoreSize(storeSize);

      // 2 MAGICCODE
      byteBuffer.getInt();

      // 3 BODYCRC
      int bodyCRC = byteBuffer.getInt();
      msgExt.setBodyCRC(bodyCRC);

      // 4 QUEUEID
      int queueId = byteBuffer.getInt();
      msgExt.setQueueId(queueId);

      // 5 FLAG
      int flag = byteBuffer.getInt();
      msgExt.setFlag(flag);

      // 6 QUEUEOFFSET
      long queueOffset = byteBuffer.getLong();
      msgExt.setQueueOffset(queueOffset);

      // 7 PHYSICALOFFSET
      long physicOffset = byteBuffer.getLong();
      msgExt.setCommitLogOffset(physicOffset);

      // 8 SYSFLAG
      int sysFlag = byteBuffer.getInt();
      msgExt.setSysFlag(sysFlag);

      // 9 BORNTIMESTAMP
      long bornTimeStamp = byteBuffer.getLong();
      msgExt.setBornTimestamp(bornTimeStamp);

      // 10 BORNHOST
      byte[] bornHost = new byte[4];
      byteBuffer.get(bornHost, 0, 4);
      int port = byteBuffer.getInt();
      msgExt.setBornHost(new InetSocketAddress(InetAddress.getByAddress(bornHost), port));

      // 11 STORETIMESTAMP
      long storeTimestamp = byteBuffer.getLong();
      msgExt.setStoreTimestamp(storeTimestamp);

      // 12 STOREHOST
      byte[] storeHost = new byte[4];
      byteBuffer.get(storeHost, 0, 4);
      port = byteBuffer.getInt();
      msgExt.setStoreHost(new InetSocketAddress(InetAddress.getByAddress(storeHost), port));

      // 13 RECONSUMETIMES
      int reconsumeTimes = byteBuffer.getInt();
      msgExt.setReconsumeTimes(reconsumeTimes);

      // 14 Prepared Transaction Offset
      long preparedTransactionOffset = byteBuffer.getLong();
      msgExt.setPreparedTransactionOffset(preparedTransactionOffset);

      // 15 BODY
      int bodyLen = byteBuffer.getInt();
      if (bodyLen > 0) {
        if (readBody) {
          byte[] body = new byte[bodyLen];
          byteBuffer.get(body);

          // uncompress body
          if ((sysFlag & MessageSysFlag.CompressedFlag) == MessageSysFlag.CompressedFlag) {
            body = UtilAll.uncompress(body);
          }

          msgExt.setBody(body);
        } else {
          byteBuffer.position(byteBuffer.position() + bodyLen);
        }
      }

      // 16 TOPIC
      byte topicLen = byteBuffer.get();
      byte[] topic = new byte[(int) topicLen];
      byteBuffer.get(topic);
      msgExt.setTopic(new String(topic));

      // 17 properties
      short propertiesLength = byteBuffer.getShort();
      if (propertiesLength > 0) {
        byte[] properties = new byte[propertiesLength];
        byteBuffer.get(properties);
        String propertiesString = new String(properties);
        Map<String, String> map = string2messageProperties(propertiesString);
        msgExt.setProperties(map);
      }

      // 消息ID
      ByteBuffer byteBufferMsgId = ByteBuffer.allocate(MSG_ID_LENGTH);
      String msgId =
          createMessageId(byteBufferMsgId, msgExt.getStoreHostBytes(), msgExt.getCommitLogOffset());
      msgExt.setMsgId(msgId);

      return msgExt;
    } catch (UnknownHostException e) {
      byteBuffer.position(byteBuffer.limit());
    } catch (BufferUnderflowException e) {
      byteBuffer.position(byteBuffer.limit());
    } catch (Exception e) {
      byteBuffer.position(byteBuffer.limit());
    }

    return null;
  }