2023-01-20 328
更新:
错误:jbyte* elements = (*env)->GetByteArrayElements(env, array, NULL);仅返回8个字节.提供任何替代方法来检索字节形式jbytearray.
我是JNI的新手,所以我对JNI和英语不熟悉.
现在,我尝试使用Java中的简单JNI程序在文件读取上,然后使用C将其写入文件.
文件读取Java代码:
public class FileIO {
static {
System.loadLibrary("io");
}
private native void writeToFile(byte[] msg);
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream=null;
File file = new File("version1-1.png");
byte[] bFile = new byte[(int) file.length()];
try {
//convert file into array of bytes
fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
System.out.println("Done");
} catch(Exception e){
e.printStackTrace();
}
new FileIO().writeToFile(bFile);
}
}
文件写C代码:
#include <jni.h>
#include <stdio.h>
#include <string.h>
#include "FileIO.h"
JNIEXPORT void JNICALL Java_FileIO_writeToFile (JNIEnv *env, jobject job, jbyteArray array ){
char * buf ;
// here what i do ??? :(
FILE *file = fopen("test123.png", "w");
int results = fputs(buf, file);
if (results == EOF) {
//Failed to write do error code here.
}
fclose(file);
}
我尝试了许多解决方案(下面的链接),但是将其写入文件时没有运气.请提供正确的解决方案和最佳JNI教程.
已经尝试了解决方案:(但不是成功)
在Java中转换字节[]在C ++中的未签名char*的正确方法,反之亦然?
将jbytearray转换为角色阵列,然后打印到控制台
如何在Jni中转换jbytearray转换为本机char*?
int len = (*env)->GetArrayLength (env , array );
printf(" Length of the bytearray %d\n", len );
unsigned char * string ;
string = (char *)malloc((len + 1) * sizeof(char)) ;
jbyte* b = (*env)->GetByteArrayElements(env, array, &isCopy);
GetByteArrayElements jbyte长度应为8,但GetArrayLength返回bytearray长度为50,335.
我尝试的是:
JNIEXPORT void JNICALL Java_HelloJNI_sayHello (JNIEnv *env, jobject job, jbyteArray array ){
jsize num_bytes = (*env)->GetArrayLength(env, array);
char *buffer = malloc(num_bytes + 1);
printf("Number of jByte element : %d\n", (int) num_bytes);
if (!buffer)
printf("Buff Fail\n");
jbyte* elements = (*env)->GetByteArrayElements(env, array, NULL);
if (!elements)
printf("Element Fail\n");
printf ("Number of Byte elements : %d\n", (int) strlen (elements));
memcpy(buffer, elements, num_bytes);
buffer[num_bytes] = 0;
printf("Number of buffer elements : %d\n", (int) strlen(elements));
(*env)->ReleaseByteArrayElements(env, array, elements, JNI_ABORT);
FILE *fp;
fp = fopen( "file.txt" , "w" );
fwrite(buffer , 1 , sizeof(buffer) , fp );
fclose(fp);
return;
}
和输出:
Done
Number of jByte element : 50335
Number of Byte elements : 8
Number of buffer elements : 8
正如@olaf观察到的,指针和数组完全是不同种类的对象.没有有意义的方法将一个转换为另一个.因此,第一步是更好地描述您的实际含义.
由于您的最终目标似乎是通过fputs()将Java数组的字节写入文件,因此您要做的是创建一个与Java Byte Array相同的C字符串.这正是Olaf提议的dupe, creti到一个字符数组,然后打印到控制台,请求以及接受的答案声称提供的内容.但是,您声称您已经尝试了该解决方案,并且它不起作用.您没有描述失败的性质,但是我可以相信它确实失败了,因为接受的答案中的解决方案有点障碍.在接下来的内容中,我将该答案称为”历史答案”.
历史性的答案在几个方面都很正确.特别是,C字符串必须为null终止,除非偶然,否则不会终止Java字节阵列的内容.另外,jbytearray指针不是直接指向数据的指针,因此您需要使用适当的JNI功能来获取数据本身.您需要为字节数组的内容 plus 创建一个足够大的新缓冲区,终结器,将字节复制到其中,然后添加终结器.
例如:
// determine the needed length and allocate a buffer for it
jsize num_bytes = GetArrayLength(env, array);
char *buffer = malloc(num_bytes + 1);
if (!buffer) {
// handle allocation failure ...
}
// obtain the array elements
jbyte* elements = GetByteArrayElements(env, array, NULL);
if (!elements) {
// handle JNI error ...
}
// copy the array elements into the buffer, and append a terminator
memcpy(buffer, elements, num_bytes);
buffer[num_bytes] = 0;
// Do not forget to release the element array provided by JNI:
ReleaseByteArrayElements(env, array, elements, JNI_ABORT);
之后,您可以在buffer指向的空间中,是字节数组内容的无效终止副本,例如,您可以将其传递给fputs():
int result = fputs(buffer, file);
另外,一旦成功分配缓冲区,就必须确定在返回功能之前释放它,包括通过任何错误处理返回路径:
free(buffer);
我在历史答案中看到的主要问题是,它建议使用strlen()计算数组数据的长度,以便能够分配足够大的临时缓冲区.但这只有在字节数组已经被终止终止时才能起作用,在这种情况下,首先不需要.
上面回答了提出的问题 – 如何将数据转换为C字符串.但是,请注意,问题本身的前提是将数据转换为C字符串并通过fputs()输出它们是一个适当的机制.正如@michael所观察到的,如果数据包含null字节,则不是这样,因为如果它们最初来自二进制文件,可能很难排除它们.
如果总体目标仅仅是将字节写入文件,则首先将它们转换为C字符串是毫无意义的.有其他机制可以输出数据,而无需先执行这种转换.如果可以依靠数据不包含内部null字节,则可以使用fprintf()编写:
fprintf(file, "%*s", (int) num_bytes, (char *) elements);
另一方面,如果数据可能包含空,则应使用适当的低级输出功能.看起来像这样:
#include <stdio.h>
#include <jni.h>
#include "FileIO.h"
JNIEXPORT void JNICALL Java_FileIO_writeToFile(JNIEnv *env, jobject job,
jbyteArray array) {
FILE *fp = fopen( "file.txt" , "w" );
if (!fp) {
// handle failure to open the file ...
}
// determine the needed length and allocate a buffer for it
jsize num_bytes = GetArrayLength(env, array);
// obtain the array elements
jbyte* elements = GetByteArrayElements(env, array, NULL);
if (!elements) {
// handle JNI error ...
}
// output the data
if (fwrite(elements, 1, num_bytes, fp) != num_bytes) {
// handle I/O error ...
}
// Do not forget to release the element array provided by JNI:
ReleaseByteArrayElements(env, array, elements, JNI_ABORT);
fclose(fp);
}
以上所述是小编给大家介绍的如何将Java字节数组的内容转换为JNI中的C字符串?,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!
原文链接:https://77isp.com/post/26060.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态