get byte[] from NSData without newDirectBuffer or new byte array #1196

Closed
yujinfu opened this Issue Nov 20, 2015 · 2 comments

Projects

None yet

4 participants

@yujinfu
yujinfu commented Nov 20, 2015

Hi, dear robovm team, please add a method to NSData to get the byte[] without new a native direct buffer or new a byte array
I saw the NSData.getBytes(long,long) was protected, so I can just use java reflection to call the method brutally ,
now I call it like below:

private void getData(byte[] data,UIImage image){
        try {
            NSData nsData = image.getCGImage().getDataProvider().getData(); //all these invoke was directly to call object-c method
                        //@Method(selector = "getBytes:length:")
                       // protected native void getBytes(@Pointer long buffer, @MachineSizedUInt long length);
                       // this one also directly call object-c method
            Method method = nsData.getClass().getDeclaredMethod("getBytes", Long.TYPE,Long.TYPE); 
            method.setAccessible(true);
            method.invoke(nsData, VM.getArrayValuesAddress(data), nsData.getLength());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
@ntherning ntherning added this to the 1.11 milestone Nov 20, 2015
@ntherning
Contributor

I suggest we add methods:

int copy(ByteBuffer buffer, int dataOffset, int count);
int copy(byte[] buffer, int bufferOffset, int dataOffset, int count);

to NSData. These will copy at most count bytes to buffer starting at dataOffset. The number of copied bytes will be returned. -1 is returned if dataOffset >= data.length. This is similar to how InputStream.read(...) works.

@yujinfu
yujinfu commented Nov 20, 2015

good job

@badlogic badlogic modified the milestone: 1.13, 1.11 Nov 23, 2015
@BlueRiverInteractive BlueRiverInteractive closed this in f40a9dd Jan 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment