简体   繁体   中英

Java native big jshortarray with more than int elements

How can I create a jshortarray with 545460846912 elements

 retval = env->NewShortArray((long) 545460846912);  // allocate
                 ~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~
..test.cpp:296:42: warning: implicit conversion from 'long' to 'jsize' (aka 'int') changes value from 545460846912 to 320 [-Wconstant-conversion]
    env->SetShortArrayRegion(retval, 0 , (long) 545460846912, (short const *)b); 
         ~~~~~~~~~~~~~~~~~~~             ^~

Try to mimic this code

int nGetEncoderFrameSize = (int) LibSpeex.nGetEncoderFrameSize(nInitSpxEncoder);

and it returns 545460846912 for my file

I need to make an short array with that size

 short[] sArr = new short[nGetEncoderFrameSize];

and then call it:

long nEncodeBuffer = LibSpeex.nEncodeBuffer(nInitSpxEncoder, sArr, bArr, 65536);

Is it possible to create that big jshortArray?

How can I solve it?

How can I create a jshortarray with 545460846912 elements

You can't .

Per the Java 9 Language Specification (bolding mine):

Arrays must be indexed by int values ; short , byte , or char values may also be used as index values because they are subjected to unary numeric promotion (§5.6.1) and become int values.

An attempt to access an array component with a long index value results in a compile-time error.

All array accesses are checked at run time; an attempt to use an index that is less than zero or greater than or equal to the length of the array causes an ArrayIndexOutOfBoundsException to be thrown.

The wording for Java 17 is identical .

Java int values are limited in range to (again, bolding mine):

4.2.1. Integral Types and Values

The values of the integral types are integers in the following ranges:

  • For byte , from -128 to 127, inclusive
  • For short , from -32768 to 32767, inclusive
  • For int , from -2147483648 to 2147483647, inclusive
  • For long , from -9223372036854775808 to 9223372036854775807, inclusive
  • For char , from ' ' to '�' inclusive, that is, from 0 to 65535

2147483647 is smaller than 545460846912.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM