I am getting following exception when using FileChannel.map
Exception in thread \"main\" java.lang.IllegalArgumentException: Size exceeds Integer.MAX_VALUE
ByteBuffer's capacity is limited to Integer.MAX_VALUE, so there is no way to map anything larger than that.
Look at: MappedByteBuffer map(MapMode mode, long position, long size)
position
has to be long for obvious reasons.
size
is not necessary to be long but in any calculation it has to be promoted - for example position+size has to be a positive long. OS mapping indeed may use long
to carry the mapping, map
function (mmap) may need to map more than Integer.MAX_VALUE in order to preserve page size but ByteBuffer just can't use that.
Overall int
lays very deep in java's design and there is no size_t
C alike type, mass utilizing long
instead of int will damper the performance. So in the end: if you need greater maps than 2GB, just use more than a single ByteBuffer.
It's not an implementation specific bug. The size is defined in the FileChannel.map as long, but...
size - The size of the region to be mapped; must be non-negative and no greater than Integer.MAX_VALUE
All compliant JVM implementations will be this way. I suspect the reason is a combination of history (who would need to access a file larger than 2GB? ;) and trying to push things forward in later versions of Java (it will be easier to allow values larger than Integer.MAX
than it will be to change the data type from int
to long
.)
A lot of people find this int-based thinking in the Java API regarding anything File very confounding and short sighted. But remember, Java start development in 1995! I'm sure 2GB seemed like a relatively safe value at the time.