JNI GetByteArrayElements()错误 [英] JNI GetByteArrayElements () Error

查看:62
本文介绍了JNI GetByteArrayElements()错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JNI的新手,所以我对JNI和英语都不熟悉.

I'm new in JNI so I'm not familiar in JNI and also English.

我的JNI项目是一个简单的File读写.用Java读取文件并将字节数组传递给C API,然后使用C将其写入文件.

My JNI project is a simple File reading and writing. File reading in Java and passing the byte array to C API the write it into file using C.

我的源代码:

Java代码是:

public class FileIO {
   static {
      System.loadLibrary("FileIO");         
   }

   private native void writeFile(byte[] msg);

   public static void main(String[] args) throws IOException { 

      byte[] array = Files.readAllBytes(new File("PtFBWCTn.mp3").toPath());

      // System.out.println(array.length);
      new FileIO(). writeFile(array); 
   }
}

C代码:

#include <jni.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "HelloJNI.h"

JNIEXPORT void JNICALL Java_FileIO_ writeFile (JNIEnv *env, jobject job, jbyteArray array ){

    jsize num_bytes = (*env)->GetArrayLength(env, array);
    printf("Byte length : %d\n" , num_bytes);

    unsigned char * buffer;
    jbyte  *lib ;
    lib =(jbyte *) malloc( ( num_bytes +1 ) * sizeof(jbyte));

    (*env)->GetByteArrayRegion(env , array, 0 , num_bytes , lib);
    // lib = (*env)->GetByteArrayElements(env , array, 0);
    buffer =(char *) lib ;
    FILE *fp;
    fp=fopen("test.mp3", "wb");
    printf("size : %d , length : %d  ,contant : %s\n" ,(int)sizeof(buffer), (int)strlen(buffer) ,  buffer );

    fwrite(buffer, sizeof(buffer[0]), sizeof(buffer)/sizeof(buffer[0]), fp);
    return;
}

我传递(.zip.mp3.mp4.jpg.png)文件字节数组时遇到问题.我尝试创建某些文本文件格式,如.txt.java.c文件.

I got problem while I am passing (.zip , .mp3 , .mp4 , .jpg and .png) file byte array. I try some text file formats like .txt, .java , .c files are create what I expect.

原因是什么?

我尝试过(用Java读取我的Java文件(FileIO.java),输出为):

I tried (reading my java file(FileIO.java) in Java and the output is):

Byte length : 238653
size : 8 , length : 4  ,contant : import java.nio.file.Files;
import java.io.File;

public class FileIO {
   static {
      System.loadLibrary("FileIO");         
   }

   private native void writeFile(byte[] msg);

   public static void main(String[] args) throws IOException { 

      byte[] array = Files.readAllBytes(new File("PtFBWCTn.mp3").toPath());

      // System.out.println(array.length);
      new FileIO(). writeFile(array); 
   }
}

然后我尝试了一下(输出了一些test.png):

And I tried (some test.png and the output is):

Byte length : 381729
size : 8 , length : 8  ,contant : ?PNG

GetByteArrayElements()在读取媒体和其他某种格式时仅返回8个字节.但是,如果是文本文件,它将返回正确的字节数. 请提供"png"的长度和其他某种格式的长度不等于Byte长度的原因.并提供解决此问题的方法.

GetByteArrayElements () returns only 8 bytes while reading media and other some format. But in case of text file it returns correct amount of bytes. Please provide the reason for why the length of the 'png' and other some format length is not equal to Byte length. And provide way to how to solve this problem.

推荐答案

    jsize num_bytes = (*env)->GetArrayLength(env, array);
    char * buffer;

    buffer = (char *) malloc (num_bytes);

    jbyte  *lib = (*env)->GetByteArrayElements(env , array, 0);

    memcpy ( buffer , lib , num_bytes ) ;

    FILE *fp;
    fp=fopen("test.png", "wb");
    fwrite(buffer, sizeof(char) , num_bytes , fp);

这篇关于JNI GetByteArrayElements()错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆