66
77import java .io .ByteArrayOutputStream ;
88import 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
1411public 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