UnsupportedOperationException from Buffers.newDirectByteBuffer()

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

UnsupportedOperationException from Buffers.newDirectByteBuffer()

MichaelMitchell
I am getting an error when ever I call array().

Code segment dealing with this:

                ByteBuffer _frame = Buffers.newDirectByteBuffer( 4 * Main.gui.glCanvas.getWidth() * Main.gui.glCanvas.getHeight() );
                System.out.println( "Num Pixels " + Main.width * Main.height );
                gl.glReadPixels(
                                0,
                                0,
                                Main.gui.glCanvas.getWidth(),
                                Main.gui.glCanvas.getHeight(),
                                GL3.GL_RGB,
                                GL3.GL_BYTE,
                                _frame
                                );

               
                try {
                        BufferedImage bi = ImageIO.read( new ByteArrayInputStream( _frame.array() ) ); // Error here!
                        ImageIO.write( bi, "png", new File( "Slices/slicu.png" ) );
                } catch ( Exception e ) {
                        System.out.println( e.getMessage() + " @ Filler.draw()" );
                        e.printStackTrace();
                }
Reply | Threaded
Open this post in threaded view
|

Re: UnsupportedOperationException from Buffers.newDirectByteBuffer()

Wade Walker
Administrator
This is because ByteBuffer doesn't support the array() method for direct buffers, only for buffers allocated on the heap. With heap buffers, you pass a byte array into the constructor that it just gives back to you for array(). But in a direct buffer, it stores only the memory address of a native memory area, which is not a Java array object.
Reply | Threaded
Open this post in threaded view
|

Re: UnsupportedOperationException from Buffers.newDirectByteBuffer()

gouessej
Administrator
In reply to this post by MichaelMitchell
Hi

If you really need an array, just follow the advice I gave you on Stackoverflow:
gouessej wrote
Then, create an array and copy the content of your buffer into it.
http://stackoverflow.com/a/22756351
Julien Gouesse | Personal blog | Website