I'm new in JNI so I'm not familiar in JNI and also English.
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.
My source code:
Java code is :
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 code :
#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;
}
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.
What is the reason?
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);
}
}
And I tried (some test.png and the output is):
Byte length : 381729
size : 8 , length : 8 ,contant : ?PNG
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.