diff --git a/tcMenuJavaApi/src/main/java/com/thecoderscorner/menu/remote/protocol/TagValTextParser.java b/tcMenuJavaApi/src/main/java/com/thecoderscorner/menu/remote/protocol/TagValTextParser.java index effc0060..1712cf3e 100644 --- a/tcMenuJavaApi/src/main/java/com/thecoderscorner/menu/remote/protocol/TagValTextParser.java +++ b/tcMenuJavaApi/src/main/java/com/thecoderscorner/menu/remote/protocol/TagValTextParser.java @@ -19,6 +19,7 @@ */ public class TagValTextParser { public static final char FIELD_TERMINATOR = '|'; + private static final int MAX_FIELD_EXPECTED = 256; private final Map keyToValue = new HashMap<>(32); /** @@ -48,27 +49,27 @@ else if(key.charAt(0) == MenuCommandProtocol.PROTO_END_OF_MSG) { } private String readString(ByteBuffer buffer) { - StringBuilder sb = new StringBuilder(32); - while(buffer.hasRemaining()) { - char ch = (char) buffer.get(); - if(ch == MenuCommandProtocol.PROTO_END_OF_MSG) { + byte rawData[] = new byte[MAX_FIELD_EXPECTED]; + int position = 0; + while(buffer.hasRemaining() && position < MAX_FIELD_EXPECTED) { + var by = buffer.get(); + if(by == MenuCommandProtocol.PROTO_END_OF_MSG) { return "\u0002"; } - else if(ch == '\\') { + else if(by == '\\') { // special escape case allows anything to be sent - ch = (char) buffer.get(); - sb.append(ch); + rawData[position++] = buffer.get(); } - else if(ch == '=' || ch == TagValTextParser.FIELD_TERMINATOR) { + else if(by == '=' || by == TagValTextParser.FIELD_TERMINATOR) { // end of current token - return sb.toString(); + return new String(rawData, 0, position); } else { // within current token - sb.append(ch); + rawData[position++] = by; } } - return sb.toString(); + return new String(rawData, 0, position); } /** diff --git a/tcMenuJavaApi/src/test/java/com/thecoderscorner/menu/remote/protocol/TagValMenuCommandProtocolTest.java b/tcMenuJavaApi/src/test/java/com/thecoderscorner/menu/remote/protocol/TagValMenuCommandProtocolTest.java index 35d47462..57fb1f73 100644 --- a/tcMenuJavaApi/src/test/java/com/thecoderscorner/menu/remote/protocol/TagValMenuCommandProtocolTest.java +++ b/tcMenuJavaApi/src/test/java/com/thecoderscorner/menu/remote/protocol/TagValMenuCommandProtocolTest.java @@ -299,6 +299,14 @@ public void testReceiveActionMenuItem() throws IOException { assertEquals(0, actMenu.getSubMenuId()); } + @Test + public void testReceiveUTF8Value() throws IOException { + MenuCommand cmd = protocol.fromChannel(toBuffer(ACTION_BOOT_ITEM, "RO=0|PI=0|ID=1|NM=á č ň ť|\u0002")); + assertTrue(cmd instanceof MenuActionBootCommand); + MenuActionBootCommand actMenu = (MenuActionBootCommand) cmd; + assertEquals("á č ň ť", actMenu.getMenuItem().getName()); + } + @Test public void testReceiveBooleanMenuItem() throws IOException { MenuCommand cmd = protocol.fromChannel(toBuffer(BOOLEAN_BOOT_ITEM, "PI=0|RO=1|VI=1|ID=1|BN=1|NM=BoolItem|VC=1|\u0002"));