为什么FileChannel.map会占用Integer.MAX_VALUE数据? [英] Why does FileChannel.map take up to Integer.MAX_VALUE of data?

查看:998
本文介绍了为什么FileChannel.map会占用Integer.MAX_VALUE数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用FileChannel.map时出现以下异常

I am getting following exception when using FileChannel.map

Exception in thread "main" java.lang.IllegalArgumentException: Size exceeds Integer.MAX_VALUE
    at sun.nio.ch.FileChannelImpl.map(Unknown Source)
    at niotest.NioTest.readUsingNio(NioTest.java:38)
    at niotest.NioTest.main(NioTest.java:64)

快速查看OpenJdk实现,显示方法图(..)in FileChannelImpl size 类型 long 作为输入。但是在体内,它将它与 Integer.MAX_VALUE 进行比较,如果大于此值则抛出错误。为什么将 long 大小作为输入,但将其限制为最大整数长度?

Quickly looking into OpenJdk implementation shows that the method map(..) in FileChannelImpl takes size of type long as input. But inside the body, it compares it with Integer.MAX_VALUE and throws error if its greater than that. Why take long size as input but limit it to max integer length?

有人知道这个实现背后的具体原因吗?
或者它是某种bug?

Anyone knows specific reason behind this implementation? or is it some kind of bug?

源URL - http://grepcode.com/file/repository.grepcode.com/java/root/jdk /openjdk/6-b14/sun/nio/ch/FileChannelImpl.java

我在64位Windows-2k8上使用64位JRE运行此程序

I am running this program using 64bit JRE on 64bit Windows-2k8

推荐答案

这不是特定于实现的错误。大小在 FileChannel.map ,但是......

It's not an implementation specific bug. The size is defined in the FileChannel.map as long, but...


size - 要映射的区域的大小;必须是非负数且不大于Integer.MAX_VALUE

size - The size of the region to be mapped; must be non-negative and no greater than Integer.MAX_VALUE

所有符合规范的JVM实现都是这样的。我怀疑原因是历史的组合(谁需要访问大于2GB的文件?;)并试图在更高版本的Java中推进事情(允许大于 Integer.MAX ,而不是将数据类型从 int 更改为 long 。 )

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.)

很多人在Java API中发现这种基于int的思想,关于任何文件非常混乱和短视。但请记住,Java在1995年开始开发!我相信当时2GB似乎是一个相对安全的价值。

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.

这篇关于为什么FileChannel.map会占用Integer.MAX_VALUE数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆