JNI Error GetByteArrayElements ()

I am new to JNI, so I am not familiar with JNI, as well as English.

My JNI project is just reading and writing files. Reading a file in Java and passing a byte array to the C API writes it to a file using C.

My source code:

Java Code:

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 have a problem with the transmission ( .zip, .mp3, .mp4, .jpgand .png) an array of bytes of the file. I'm trying to use some text formats such as .txt, .java, .cfiles to create what I would expect.

What is the reason?

I tried (reading my java file (FileIO.java) in Java and exiting):

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 output):

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

GetByteArrayElements() 8 . . , "png" . .

+4
3
    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);
+2

:

strlen() ,         s, ('\ 0').

strlen()         s.

, , , , - null terminator. , 0.

, , , null terminator .

(int)sizeof(buffer) , pointer unsigned char. (64 ..) 8 .

: , size_t, "%zu".

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

.

, .

. , png . PNG

+1

:

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

. sizeof(jbyte), 1:

lib = (jbyte *) malloc(num_bytes);

:

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

(int)sizeof(buffer) - , , , 8- ( 64- ). (int)strlen(buffer) , , ('\0') .

- strlen() , - . , PNG, MP3 .. , strlen().

: - GetArrayLength().

fwrite() :

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

sizeof(buffer)/sizeof(buffer[0] 8/1, buffer 8- buffer[0] unsigned char . :

fwrite(buffer, 1, num_bytes, fp);

, , . , , .

+1
source

Source: https://habr.com/ru/post/1650283/


All Articles