简体   繁体   English

JNA取回字节数组

[英]JNA getting back a byte array

I have a library developed in C that successfully decompresses an LZMA encoded file. 我有一个用C开发的库,可以成功解压缩LZMA编码的文件。 The signature for the file is a pointer to the data, the compressed, decompressed, and an 'out' field for the error code. 文件的签名是指向数据的指针,已压缩,已解压缩以及错误代码的'out'字段。 The return value is a pointer to the array (or null if it fails). 返回值是指向数组的指针(如果失败,则返回null)。

The function in C looks similar to this: C语言中的函数与此类似:

char* decompressLZMA(const char *lzmaData, int compressedSize, int uncompressedSize, t_status_codes *statusCode);

I've tried using pointers and memory from other examples but they are not working. 我试过使用其他示例中的指针和内存,但是它们不起作用。

How do I properly pass a byte array and pointer in to get data back? 如何正确传递字节数组和指针以获取数据?

This is my interface: 这是我的界面:

public interface LZMALibrary extends Library {
    Memory lzma_uncompress(Memory lzma_data, int compressed_size, int uncompressed_size, Pointer status_code);
}

It appears that I wanted to make a pointer of the 'Memory' class instead. 看来我想改用'Memory'类的指针。 The solution that is working for me right now is to create Pointer objects, and then the library will fill up the pointers, and I get them back and handle it appropriately. 现在对我有用的解决方案是创建Pointer对象,然后库将填充指针,然后我将它们找回来并适当地处理它。

My interface turned to: 我的界面变成:

public interface LZMALibrary extends Library {
    Pointer lzma_uncompress(Pointer lzma_data, int compressed_size, int uncompressed_size, Pointer status_code);
}

From there I am able to write in the data: 从那里我可以写数据:

Pointer ptr = new Memory(lzmaData.length);
ptr.write(0, lzmaData, 0, lzmaData.length);

I also need the pointer that will be written to: 我还需要将要写入的指针:

Pointer errorStatus = new Memory(4);

Then I can call the function to get a pointer back, and read that pointer if it's not null: 然后,我可以调用该函数以返回指针,并在该指针不为null时读取该指针:

Pointer p = lzmaLib.lzma_uncompress(ptr, lzmaData.length, DECOMPRESSED_LENGTH, errorStatus); // DECOMPRESSED_LENGTH is a constant.

if (p != null) {
    byte[] decompressedData = p.getByteArray(0, DECOMPRESSED_LENGTH);
    System.out.println(new String(decompressedData, "ASCII")); // My data is ASCII text.
}

if (errorStatus != null) {
    int errorStatusCode = errorStatus.getInt(0);
    System.out.println("Error code: " + errorStatusCode);
}

This appears to have solved my problem. 这似乎解决了我的问题。 I am very new to JNA so hopefully I am not missing anything. 我是JNA的新手,希望我不会错过任何东西。

If there's any possible errors I might run into, please feel free to post on how to correct it. 如果我可能遇到任何可能的错误,请随时发布如何纠正它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM