Don't share CharsetDecoders across threads

Fixes #1803
This commit is contained in:
Jonathan Coates 2024-04-24 21:19:30 +01:00
parent 925092add3
commit 4980b7355d
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
1 changed files with 2 additions and 2 deletions

View File

@ -29,7 +29,7 @@
* @see dan200.computercraft.core.apis.HTTPAPI#websocket On how to open a websocket.
*/
public class WebsocketHandle {
private static final CharsetDecoder DECODER = StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPLACE);
private static final ThreadLocal<CharsetDecoder> DECODER = ThreadLocal.withInitial(() -> StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPLACE));
private final IAPIEnvironment environment;
private final String address;
@ -87,7 +87,7 @@ public final void send(Coerced<ByteBuffer> message, Optional<Boolean> binary) th
websocket.sendBinary(text);
} else {
try {
websocket.sendText(DECODER.decode(text).toString());
websocket.sendText(DECODER.get().decode(text).toString());
} catch (CharacterCodingException e) {
// This shouldn't happen, but worth mentioning.
throw new LuaException("Message is not valid UTF8");