RocketMQ 源码阅读—— DefaultMQProducerImpl 中 Topic 校验
在 DefaultMQProducerImpl 的启动过程中,会校验 Topic,RocketMQ 用了了一个简易的位图来维护合法值范围,蛮小巧别致的。
public static boolean isTopicOrGroupIllegal(String str) {
int strLen = str.length();
int len = VALID_CHAR_BIT_MAP.length;
boolean[] bitMap = VALID_CHAR_BIT_MAP;
for (int i = 0; i < strLen; i++) {
char ch = str.charAt(i);
if (ch >= len || !bitMap[ch]) {
return true;
}
}
return false;
}
public static final boolean[] VALID_CHAR_BIT_MAP = new boolean[128];
VALID_CHAR_BIT_MAP['%'] = true;
// -
VALID_CHAR_BIT_MAP['-'] = true;
// _
VALID_CHAR_BIT_MAP['_'] = true;
// |
VALID_CHAR_BIT_MAP['|'] = true;
for (int i = 0; i < VALID_CHAR_BIT_MAP.length; i++) {
if (i >= '0' && i <= '9') {
// 0-9
VALID_CHAR_BIT_MAP[i] = true;
} else if (i >= 'A' && i <= 'Z') {
// A-Z
VALID_CHAR_BIT_MAP[i] = true;
} else if (i >= 'a' && i <= 'z') {
// a-z
VALID_CHAR_BIT_MAP[i] = true;
}
}