Muthu GM Muthu GM - 4 months ago 115
C Question

JNI GetByteArrayElements () Error

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.

Answer

First of all you shouldn't allocate extra byte for destination buffer:

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

it is unnecessary. Also you may omit sizeof(jbyte) since it is always 1:

lib = (jbyte *) malloc(num_bytes);

Then:

printf("size : %d , length : %d  ,contant : %s\n" ,(int)sizeof(buffer), (int)strlen(buffer) ,  buffer );

(int)sizeof(buffer) is a size of a pointer variable, not a size of addressed data, and program fairly tells you that pointer is 8-byte length object (you are on 64-bit host). Next, (int)strlen(buffer) doesn't return actual size of pointed data, instead it returns the number of bytes that precede the first zero ('\0') byte in your data.

And because of this strlen() result matches with length of data that were read from text files - they almost always don't contain zero bytes. And binary files, like PNG, MP3, so on contain zero bytes frequently, and actual length doesn't match with strlen() result.

To top it off: the only true data length - is the result of GetArrayLength().

EDIT

Actual arguments for fwrite() are not OK:

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

sizeof(buffer)/sizeof(buffer[0] is the same as 8/1, since buffer is a 8-byte length pointer and buffer[0] is unsigned char i.e. one byte length entity. You should rewrite it like this:

fwrite(buffer, 1, num_bytes, fp);

And there is no standard function that can help you to print all data including zeros. I guess you should manually loop over each byte, format and print.