From ad30d7224df8d4d368f87658e10ae90cf76f58f5 Mon Sep 17 00:00:00 2001 From: Eclipse Date: Wed, 22 Oct 2025 08:12:26 +0000 Subject: [PATCH] Fix: don't deadlock when writing NativeImage to byte array --- .../rainbow/image/NativeImageUtil.java | 24 ++++--------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/rainbow/src/main/java/org/geysermc/rainbow/image/NativeImageUtil.java b/rainbow/src/main/java/org/geysermc/rainbow/image/NativeImageUtil.java index e79fbd8..453e2b5 100644 --- a/rainbow/src/main/java/org/geysermc/rainbow/image/NativeImageUtil.java +++ b/rainbow/src/main/java/org/geysermc/rainbow/image/NativeImageUtil.java @@ -6,10 +6,7 @@ import org.lwjgl.stb.STBImage; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.channels.Pipe; -import java.nio.channels.ReadableByteChannel; -import java.nio.channels.WritableByteChannel; +import java.nio.channels.Channels; public class NativeImageUtil { @@ -20,24 +17,11 @@ public class NativeImageUtil { throw new UnsupportedOperationException("Don't know how to write format " + image.format()); } else { ((NativeImageAccessor) (Object) image).invokeCheckAllocated(); - Pipe pipe = Pipe.open(); - try (WritableByteChannel outputChannel = pipe.sink()) { - if (!((NativeImageAccessor) (Object) image).invokeWriteToChannel(outputChannel)) { + try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { + if (!((NativeImageAccessor) (Object) image).invokeWriteToChannel(Channels.newChannel(output))) { throw new IOException("Could not write image to pipe: " + STBImage.stbi_failure_reason()); } - } - - try (ReadableByteChannel inputChannel = pipe.source()) { - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - ByteBuffer buffer = ByteBuffer.allocate(4096); - while (inputChannel.read(buffer) != -1) { - buffer.flip(); - while (buffer.hasRemaining()) { - bytes.write(buffer.get()); - } - buffer.clear(); - } - return bytes.toByteArray(); + return output.toByteArray(); } } }