title: How to convert the contents of a Java byte array to C string in JNI?
body: <hr>
  
  <p><strong>Update:</strong> </p>
  
  <p>Error: <code>jbyte* elements = (*env)-&gt;GetByteArrayElements(env, array, NULL);</code> returns only 8 bytes. Provide any alternative to way to retrieve byte form jbytearray.</p>
  
  <hr>
  
  <p>I'm new in JNI so I'm not familiar in JNI and also English. </p>
  
  <p>Now I try the simple JNI program on File reading in Java and write it into file using C. </p>
  
  <p>File reading java code: </p>
  
  <pre><code>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); 
     }
  }
  </code></pre>
  
  <p>File write C code:</p>
  
  <pre><code>#include &lt;jni.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #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);
  }
  </code></pre>
  
  <p>I have tried many solutions (below link) but no luck in writing it to the file. Please provide the correct solution  and best JNI tutorial site.</p>
  
  <p>Already tried solution: (But not success)</p>
  
  <p><a href="https://stackoverflow.com/questions/16667029/a-correct-way-to-convert-byte-in-java-to-unsigned-char-in-c-and-vice-versa">A correct way to convert byte[] in java to unsigned char* in C++, and vice versa?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/17491752/converting-jbytearray-to-a-character-array-and-then-printing-to-console">Converting jbyteArray to a character array, and then printing to console</a></p>
  
  <p><a href="https://stackoverflow.com/questions/8439233/how-to-convert-jbytearray-to-native-char-in-jni">How to convert jbyteArray to native char* in jni?</a></p>
  
  <pre><code>int len = (*env)-&gt;GetArrayLength (env , array );
  printf(" Length of the bytearray %d\n", len );
  unsigned char * string ;
  string = (char *)malloc((len + 1) * sizeof(char)) ;
  
  jbyte* b = (*env)-&gt;GetByteArrayElements(env, array, &amp;isCopy);
  </code></pre>
  
  <p>After <code>GetByteArrayElements</code> jbyte length should be 8 but the <code>GetArrayLength</code> returns bytearray length is 50,335.</p>
  
  <p>What i try :</p>
  
  <pre><code>JNIEXPORT void JNICALL Java_HelloJNI_sayHello (JNIEnv *env, jobject job, jbyteArray array ){
  
      jsize num_bytes = (*env)-&gt;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)-&gt;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)-&gt;ReleaseByteArrayElements(env, array, elements, JNI_ABORT);
  
      FILE *fp;
      fp = fopen( "file.txt" , "w" );
      fwrite(buffer , 1 , sizeof(buffer) , fp );
      fclose(fp);
      return;
  }
  </code></pre>
  
  <p>AND the Output:</p>
  
  <pre><code>Done
  Number of jByte element   : 50335
  Number of Byte elements   : 8
  Number of buffer elements : 8
  </code></pre>