Skip to content

Commit ad30d72

Browse files
Fix: don't deadlock when writing NativeImage to byte array
1 parent fa38449 commit ad30d72

File tree

1 file changed

+4
-20
lines changed

1 file changed

+4
-20
lines changed

rainbow/src/main/java/org/geysermc/rainbow/image/NativeImageUtil.java

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66

77
import java.io.ByteArrayOutputStream;
88
import java.io.IOException;
9-
import java.nio.ByteBuffer;
10-
import java.nio.channels.Pipe;
11-
import java.nio.channels.ReadableByteChannel;
12-
import java.nio.channels.WritableByteChannel;
9+
import java.nio.channels.Channels;
1310

1411
public class NativeImageUtil {
1512

@@ -20,24 +17,11 @@ public static byte[] writeToByteArray(NativeImage image) throws IOException {
2017
throw new UnsupportedOperationException("Don't know how to write format " + image.format());
2118
} else {
2219
((NativeImageAccessor) (Object) image).invokeCheckAllocated();
23-
Pipe pipe = Pipe.open();
24-
try (WritableByteChannel outputChannel = pipe.sink()) {
25-
if (!((NativeImageAccessor) (Object) image).invokeWriteToChannel(outputChannel)) {
20+
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
21+
if (!((NativeImageAccessor) (Object) image).invokeWriteToChannel(Channels.newChannel(output))) {
2622
throw new IOException("Could not write image to pipe: " + STBImage.stbi_failure_reason());
2723
}
28-
}
29-
30-
try (ReadableByteChannel inputChannel = pipe.source()) {
31-
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
32-
ByteBuffer buffer = ByteBuffer.allocate(4096);
33-
while (inputChannel.read(buffer) != -1) {
34-
buffer.flip();
35-
while (buffer.hasRemaining()) {
36-
bytes.write(buffer.get());
37-
}
38-
buffer.clear();
39-
}
40-
return bytes.toByteArray();
24+
return output.toByteArray();
4125
}
4226
}
4327
}

0 commit comments

Comments
 (0)