@@ -275,11 +275,16 @@ public static boolean isMatched(String topicFilter, String topicName) throws Ill
275275 }
276276
277277 while (filterPos < filterLen && topicPos < topicLen ) {
278- if (topicFilter .charAt (filterPos ) == '#' )
279- topicPos = topicLen - 1 ; // skip until end of string
280-
278+ if (topicFilter .charAt (filterPos ) == '#' ) {
279+ /*
280+ * next 'if' will break when topicFilter = topic/# and topicName topic/A/,
281+ * but they are matched
282+ */
283+ topicPos = topicLen ;
284+ filterPos = filterLen ;
285+ break ;
286+ }
281287 if (topicName .charAt (topicPos ) == '/' && topicFilter .charAt (filterPos ) != '/' )
282-
283288 break ;
284289 if (topicFilter .charAt (filterPos ) != '+' && topicFilter .charAt (filterPos ) != '#'
285290 && topicFilter .charAt (filterPos ) != topicName .charAt (topicPos ))
@@ -301,11 +306,11 @@ public static boolean isMatched(String topicFilter, String topicName) throws Ill
301306 * https://github.com/eclipse/paho.mqtt.java/issues/418
302307 * Covers edge case to match sport/# to sport
303308 */
304- if ((topicFilter .length () - topicName . length ()) == 2 &&
305- topicFilter . substring ( topicFilter . length () - 2 , topicFilter .length ()). equals ( "/#" )) {
306- String filterSub = topicFilter . substring ( 0 , topicFilter . length () - 2 ) ;
307- if (filterSub . equals ( topicName )) {
308- System . err . println ( "filterSub equals topicName: " + filterSub + " == " + topicName );
309+ if ((topicFilter .length () - filterPos > 0 ) && ( topicPos == topicLen )) {
310+ if ( topicName . charAt ( topicPos - 1 ) == '/' && topicFilter .charAt ( filterPos ) == '#' )
311+ return true ;
312+ if (topicFilter . length () - filterPos > 1
313+ && topicFilter . substring ( filterPos , filterPos + 2 ). equals ( "/#" )) {
309314 return true ;
310315 }
311316 }
0 commit comments