这段代码有什么问题?:
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
FileChannel channel = cacheFile.getChannel();
int bytesCount = channel.read(byteBuffer, offset);
int value = byteBuffer.getInt();最后一行总是抛出BufferUnderflowException。变量bytesCount包含4。
我在这里错过了什么?
发布于 2013-06-22 04:29:24
在读取之前使用绝对get或倒带缓冲区:
// option 1
int value = byteBuffer.getInt(0);
// option 2
buffer.rewind();
int value = byteBuffer.getInt();尽管文档不是很明显(您必须单击链接直到转到ReadableByteChannel.read()),但读入缓冲区会更改缓冲区的位置。
发布于 2013-06-22 07:37:16
在使用get()或write()从缓冲区中获取数据之前,必须翻转缓冲区。
https://stackoverflow.com/questions/17243652
复制相似问题