我尝试使用java库ByteBuffer,并编写了以下代码示例:
ByteBuffer buf = ByteBuffer.allocate(32);
buf.putInt(4);
buf.putInt(8);
buf.putInt(12);
buf.putInt(16);
buf.putInt(20);
buf.putInt(24);
buf.putInt(28);
buf.putInt(32);
buf.order(ByteOrder.LITTLE_ENDIAN);
byte[] temp = new byte[32];
buf.get(temp);由于某种原因,它在最后一行抛出了一个BufferUnderflowException。
我不知道为什么,谁能给我解释一下我做错了什么?
发布于 2014-05-15 20:58:26
如java文档中所述。
相对get方法。..。
如果缓冲区的当前位置不小于其限制,则抛出: BufferUnderflowException
查找更多here
发布于 2014-05-15 21:11:07
看一看http://mindprod.com/jgloss/bytebuffer.html
必须调用ByteBuffer.flip才能从通过物理I/O填充缓冲区转换为通过ByteBuffer.get清空缓冲区
https://stackoverflow.com/questions/23678826
复制相似问题