Skip to content

Commit 6876cd2

Browse files
committed
0 terminator for byte array
1 parent 362b613 commit 6876cd2

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/main/java/JavaBWAPIBackend/Client.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
import java.nio.ByteBuffer;
66
import java.nio.ByteOrder;
7+
import java.nio.CharBuffer;
8+
import java.nio.charset.Charset;
9+
import java.nio.charset.CharsetEncoder;
710
import java.nio.charset.StandardCharsets;
811

912
public class Client {
@@ -438,12 +441,18 @@ public class Event {
438441
public int eventStringCount() { return sharedMemory.getInt(StringOffset); }
439442
public String eventString(int s) { return parseString(StringOffset + 4 + 256 * s, 256); }
440443

444+
445+
private final CharsetEncoder enc = Charset.forName("ISO-8859-1").newEncoder();
446+
441447
public int addString(String s) {
442-
if(s.length() >= 1024)
448+
int len = s.length() + 1;
449+
if(len >= 1024)
443450
throw new StringIndexOutOfBoundsException();
444451
int at = sharedMemory.getInt(StringOffset + 256004);
452+
byte b[] = new byte[len];
453+
enc.encode(CharBuffer.wrap(s), ByteBuffer.wrap(b), true);
445454
sharedMemory.position(StringOffset + 256008 + at * 1024);
446-
sharedMemory.put(s.getBytes(StandardCharsets.ISO_8859_1), 0, s.length());
455+
sharedMemory.put(b, 0, len);
447456
sharedMemory.position(0);
448457
sharedMemory.putInt(StringOffset + 256004, at + 1);
449458
return at;

0 commit comments

Comments
 (0)