Skip to content

Commit 020b416

Browse files
committed
修复 MsgCodecImpl 解析以空白字符开头的数据时,未能解析出 Message 的问题
1 parent 43368ef commit 020b416

1 file changed

Lines changed: 25 additions & 5 deletions

File tree

okhttps-stomp/src/main/java/com/ejlchina/stomp/MsgCodecImpl.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,7 @@ public synchronized void decode(String input, OnCallback<Message> out) {
8686
* @param start 开始解析的问题
8787
*/
8888
protected void decode(OnCallback<Message> out, int start) {
89-
if (start > 0) {
90-
// 清空 start 之前的数据
91-
pending.delete(0, start);
92-
}
89+
cleanPendingStartData(start);
9390
// Body 结尾符下标
9491
int bEndIdx = pending.indexOf(bodyEnd);
9592
if (bEndIdx < 0) {
@@ -122,13 +119,32 @@ protected void decode(OnCallback<Message> out, int start) {
122119
decode(out, bEndIdx + bodyEnd.length());
123120
}
124121

122+
protected void cleanPendingStartData(int start) {
123+
if (start > 0) {
124+
// 清空 start 之前的数据
125+
pending.delete(0, start);
126+
}
127+
// Command 开始符下标
128+
int index = 0;
129+
for (int i = 0; i < pending.length(); i++) {
130+
char c = pending.charAt(i);
131+
if (isCommandChar(c)) {
132+
index = i;
133+
}
134+
}
135+
if (index > 0) {
136+
// 清空 Command 之前的数据
137+
pending.delete(0, index);
138+
}
139+
}
140+
125141
/**
126142
* 解析 Headers
127143
* @param cEndIdx Command 结尾符下标
128144
* @param hEndIdx Headers 结尾符下标
129145
* @return Headers
130146
*/
131-
private List<Header> decodeHeaders(int cEndIdx, int hEndIdx) {
147+
protected List<Header> decodeHeaders(int cEndIdx, int hEndIdx) {
132148
String[] strHeaders = pending.substring(cEndIdx + commandEnd.length(), hEndIdx)
133149
.split(headerDelimiter);
134150
List<Header> headers = new ArrayList<>(strHeaders.length);
@@ -145,6 +161,10 @@ protected boolean isCommand(String command) {
145161
return !command.isEmpty() && command.matches("[A-Z]+");
146162
}
147163

164+
protected boolean isCommandChar(char c) {
165+
return c >= 'A' && c <= 'Z';
166+
}
167+
148168
protected Message createMessage(String command, List<Header> headers, String payload) {
149169
return new Message(command, headers, payload);
150170
}

0 commit comments

Comments
 (0)