- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
异常堆栈为
java.nio.BufferOverflowException
at java.nio.DirectByteBuffer.put(DirectByteBuffer.java:327)
at java.nio.ByteBuffer.put(ByteBuffer.java:813)
mappedByteBuffer.put(bytes);
代码:
randomAccessFile = new RandomAccessFile(file, "rw");
fileChannel = randomAccessFile.getChannel();
mappedByteBuffer = fileChannel.map(MapMode.READ_WRITE, 0, file.length());
并调用mappedByteBuffer.put(bytes);
mappedByteBuffer.put(bytes)
抛出 BufferOverflowException 的原因是什么
如何查找原因?
最佳答案
The mapped byte buffer returned by this method will have a position of zero and a limit and capacity of size;
换句话说,如果bytes.length > file.length()
,您应该收到一个BufferOverflowException
。
为了证明这一点,我测试了这段代码:
File f = new File("test.txt");
try (RandomAccessFile raf = new RandomAccessFile(f, "rw")) {
FileChannel ch = raf.getChannel();
MappedByteBuffer buf = ch.map(MapMode.READ_WRITE, 0, f.length());
final byte[] src = new byte[10];
System.out.println(src.length > f.length());
buf.put(src);
}
当且仅当打印 true
时,才会抛出此异常:
Exception in thread "main" java.nio.BufferOverflowException
at java.nio.DirectByteBuffer.put(DirectByteBuffer.java:357)
at java.nio.ByteBuffer.put(ByteBuffer.java:832)
关于java - BufferOverflowException 的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20785409/
我有一堆键和值,我想通过将它们打包在一个字节数组中来发送到我们的消息队列。我将创建一个包含所有键和值的字节数组,该数组应始终小于 50K,然后发送到我们的消息队列。我有标题,然后是数据。 数据包类别:
我正在尝试使用 OpenGL 画一个圆。我有以下代码: int numberOfVertices = 100; float[][] vertices = new float[numberOfVerti
异常堆栈为 java.nio.BufferOverflowException at java.nio.DirectByteBuffer.put(DirectByteBuffer.java:3
我们的应用程序使用 iMX6 SoM 芯片和 480x272 LVDS 显示屏。 我们还使用 Gluon 的 OpenJFX 和 Java 8u241 的嵌入式版本。 每当我们启动 jar 文件时,我
我有一个微服务应用程序,我需要使用 WebSockets 来实现其中一项功能。浏览器连接到 API 网关,然后需要将 WebSocket 请求转发到适当的微服务。为实现这一点,在 API 网关中使用了
我尝试为“audio/3gpp”创建编码器,但我的应用程序崩溃了。 我配置 MediaCodec... String mMime = "audio/3gpp"; mMediaCodec
每次我想运行我的 Android 应用程序时都会出错: [2013-11-02 13:05:36 - Dex Loader] Unable to execute dex: java.nio.Buffe
当编译一个特定的 Android 项目时,并且只在我的 Windows 机器上,我从 dex 中得到一个 java.nio.BufferOverflowException。使用 Eclipse 和使用
在编译特定的 Android 项目时,并且仅在我的 Windows 机器上,我在从 dex 中得到一个 java.nio.BufferOverflowException。使用 Eclipse 和使用
我尝试这段代码: byte arr[] = ByteBuffer.allocate(2).putInt(1).array() 但它失败并显示 BufferOverflowException 。1 是否
我正在尝试编写一个程序,该程序一次可以获取 1 位,然后在“收集”16 位后将 2 个字节写入文件。 这是基本代码: public void addBit(int bit) throws IOExce
背景 我正在尝试使用 Apache Commons 库中的 CircularFifoBuffer 类,该类包含通过 WebSocket 连接接收的最新消息的集合。但是,当我在 Linux 部署中达到
由于之前没有翻转缓冲区,我遇到了问题,但现在我无法让缓冲区使用 .put() 或 .putInt() 添加任何内容,它在第一次尝试时不断抛出 BufferOverflowException: buff
我需要将我的整数值转换为字节数组。为了不在每次调用我的 intToBytes 方法时一次又一次地创建 ByteBuffer,我定义了一个静态 ByteBuffer。 private static By
我只是为了好玩而尝试创建一个简单的程序,但我收到了 BufferOverflowException: ByteBuffer byteBuffer = ByteBuffer.allocateDirect
我正在构建一个字节数组来识别 M-Bus 主设备,并且我需要使用辅助地址来完成此操作。 To do it i need to build a byte[] with the identificatio
当我尝试使用 jobb 工具解压缩加密的 obb 文件时,出现以下错误: jobb -dump /temp/obb-output/ -o my-app-assets.obb -k secret-key
我正在学习java nio,我正在使用MappedByteBuffer和ExecutorService来异步复制文件。我的问题是 MappedByteBuffer.put() 方法抛出 java.ni
这是我的代码 Future.sequence((2 to firstPage.pages).map { count => getCommentPage(av, count) }).map(//
当我试图用 ADT 在 eclipse 中编译我的代码时,它向我显示了这个错误 [2013-12-10 17:55:51 - Android SDK] Warning when loading the
我是一名优秀的程序员,十分优秀!