mirror of
https://github.com/GeyserMC/Rainbow.git
synced 2025-12-19 14:59:16 +00:00
Fix: don't deadlock when writing NativeImage to byte array
This commit is contained in:
@@ -6,10 +6,7 @@ import org.lwjgl.stb.STBImage;
|
|||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.channels.Channels;
|
||||||
import java.nio.channels.Pipe;
|
|
||||||
import java.nio.channels.ReadableByteChannel;
|
|
||||||
import java.nio.channels.WritableByteChannel;
|
|
||||||
|
|
||||||
public class NativeImageUtil {
|
public class NativeImageUtil {
|
||||||
|
|
||||||
@@ -20,24 +17,11 @@ public class NativeImageUtil {
|
|||||||
throw new UnsupportedOperationException("Don't know how to write format " + image.format());
|
throw new UnsupportedOperationException("Don't know how to write format " + image.format());
|
||||||
} else {
|
} else {
|
||||||
((NativeImageAccessor) (Object) image).invokeCheckAllocated();
|
((NativeImageAccessor) (Object) image).invokeCheckAllocated();
|
||||||
Pipe pipe = Pipe.open();
|
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
|
||||||
try (WritableByteChannel outputChannel = pipe.sink()) {
|
if (!((NativeImageAccessor) (Object) image).invokeWriteToChannel(Channels.newChannel(output))) {
|
||||||
if (!((NativeImageAccessor) (Object) image).invokeWriteToChannel(outputChannel)) {
|
|
||||||
throw new IOException("Could not write image to pipe: " + STBImage.stbi_failure_reason());
|
throw new IOException("Could not write image to pipe: " + STBImage.stbi_failure_reason());
|
||||||
}
|
}
|
||||||
}
|
return output.toByteArray();
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user