如何在JNI中将Java字节数组的内容转换为C字符串?

Mut*_* GM 5 c java java-native-interface


更新:

错误: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); 
   }
}
Run Code Online (Sandbox Code Playgroud)

文件写入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);
}
Run Code Online (Sandbox Code Playgroud)

我尝试了很多解决方案(下面的链接),但没有成功将其写入文件。请提供正确的解决方案和最好的JNI教程站点。

已经尝试过解决方案:(但没有成功)

将 java 中的 byte[] 转换为 C++ 中的 unsigned 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);
Run Code Online (Sandbox Code Playgroud)

jbyte 长度之后GetByteArrayElements应该是 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;
}
Run Code Online (Sandbox Code Playgroud)

和输出:

Done
Number of jByte element   : 50335
Number of Byte elements   : 8
Number of buffer elements : 8
Run Code Online (Sandbox Code Playgroud)

Joh*_*ger 6

正如@Olaf 观察到的,指针和数组是完全不同类型的对象。没有任何有意义的方法可以将一种方法转换为另一种方法。因此,第一步是更好地描述您的实际含义。

由于您的最终目标似乎是通过 将 Java 数组的字节写入文件fputs(),因此您似乎想要做的是创建一个内容与 Java 字节数组相同的 C 字符串。这正是奥拉夫提出的欺骗, 将 jbyteArray 转换为字符数组,然后打印到控制台、请求以及接受的答案声称提供的内容。然而,您声称您已经尝试过该解决方案,但它不起作用。您没有描述失败的性质,但我可以相信它确实失败了,因为接受的答案中的解决方案有点错误。在下文中,我将把这个答案称为“历史性答案”。

历史性的答案在几个方面是相当正确的。特别是,C 字符串必须以 null 终止,而 Java 字节数组的内容不会终止,除非意外。另外,jbytearray指针不是直接指向数据的指针,因此您需要使用适当的 JNI 函数来获取数据本身。您需要创建一个足够大的新缓冲区来容纳字节数组的内容终止符,将字节复制到其中,然后添加终止符。

例如:

// 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);
Run Code Online (Sandbox Code Playgroud)

之后,您将在buffer字节数组内容的空终止副本所指向的空间中,例如,您可以将其传递给fputs()

int result = fputs(buffer, file);
Run Code Online (Sandbox Code Playgroud)

此外,一旦成功分配缓冲区,您必须确保在从函数返回之前释放它,包括通过任何错误处理返回路径:

free(buffer);
Run Code Online (Sandbox Code Playgroud)

我看到历史答案的主要问题是,它建议使用strlen()计算数组数据的长度,以便能够分配足够大的临时缓冲区。但只有当字节数组已经以 null 终止时,这才有效,在这种情况下,首先就没有必要。

更新

上面回答了所提出的问题——如何将数据转换为 C 字符串。但请注意,问题本身的前提是假设将数据转换为 C 字符串并通过其输出fputs()首先是一种合适的机制。正如@Michael 观察到的,如果数据包含空字节,则情况并非如此,因为可能很难排除它们是否最初来自二进制文件。

如果总体目标只是将字节写入文件,那么首先将它们转换为 C 字符串是没有意义的。有一些替代机制可以在不首先执行此类转换的情况下输出数据。如果可以确信数据不包含内部空字节,那么您可以使用以下方法fprintf()写入它们:

fprintf(file, "%*s", (int) num_bytes, (char *) elements);
Run Code Online (Sandbox Code Playgroud)

另一方面,如果数据可能包含空值,那么您应该使用适当的低级输出函数。这可能看起来像这样:

#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);
}
Run Code Online (Sandbox Code Playgroud)