4

I want to allocate a direct IntBuffer in Java with, say, a billion elements (64-bit system). The only way I know of is creating a direct ByteBuffer and viewing it as a direct IntBuffer. However, 4*1,000,000,000 exceeds Integer.MAX_VALUE, so my question is: how can I achieve my goal?

int numInts = 1_000_000_000;
IntBuffer i = IntBuffer.allocate(numInts); // works!

ByteBuffer bb = ByteBuffer.allocateDirect(4*numInts); // does NOT work: integer overflow
IntBuffer ib = bb.asIntBuffer();
System.out.println("This will never be printed");

Thanks a lot in advance,

Markus

Flowi
  • 45
  • 4
  • 1
    how about creating a two dimensional array of buffers ? – nafas Oct 07 '15 at 11:18
  • 2
    Possible duplicate of [java creating byte array whose size is represented by a long](http://stackoverflow.com/questions/1071858/java-creating-byte-array-whose-size-is-represented-by-a-long) – Johnny Willer Oct 07 '15 at 11:49
  • 1
    It's not a limitation with IntBuffer, it's a java limititaon. Arrays length in java cannot be bigger than 2^31. – Johnny Willer Oct 07 '15 at 11:56
  • You're right, but my buffer's size need not exceed 2^31 integers, the problem arises because of the (necessary?) detour via ByteBuffer. – Flowi Oct 08 '15 at 09:37

1 Answers1

1

Maybe you could try to create a Buffer based on a BigInteger instead. Some people created a 'BigIntbuffer' class on Github that could inspire you : See here

Chryor
  • 115
  • 2
  • 15