0

Is this correct way to create native jshortArray from C char array:

size_t s;
FILE *f = fopen(argv[4], "rb");
void *b;
fseek(f, 0, SEEK_END);
s = ftell(f);
fseek(f, 0, SEEK_SET);

b = (char *)malloc(s);
fread(b, s, 1, f);
fclose(f);


jshortArray js = env->NewShortArray(s*2); 
env->SetShortArrayRegion(js, 0 , s*2, (short const *)b);

Will js contain the same content as char array?

I am working with audio library and it takes input as bytes and output will be short array (jshortArray). But I need to pass it and I assume being the right size.

Working on proprietary implementation of libspx (libspeex)

I assume that is what it takes/works:

public static native boolean nDecodeBuffer(long j12, byte[] bArr, long j13, short[] sArr);

I am also confused .... how do JNI/Java works with codecs?

I see in Java we pass by value i.e I will pass byte array as input and get my short array hopefully with output in Java Implementation (correct me if I am wrong)

In C/C++ it would be done with pointers, I would pass output to be a pointer to my short array

Can somebody explain it also

dev
  • 1,119
  • 1
  • 11
  • 34
  • 1
    You read `s` bytes from file and use that to construct a `short[]` of byte `s*2`. That means the buffer actually would contain s * 2 shorts, or s * 4 bytes. It does not. Both the `NewShortArray` and `SetShortArrayRegion` must pass `s/2` instead. – Botje Dec 21 '22 at 15:33
  • 1
    As for your other question, Java is pass by _reference_. – Botje Dec 21 '22 at 15:39
  • Yes that works now correctly – dev Dec 21 '22 at 16:12

0 Answers0