我从下面的代码中提取BufferUnderflowException。
int length = mBuf.remaining();
char[] charBuff = new char[length];
for (int i = 0; i < length; ++i) {
char[i] = mBuf.getChar();
}mBuf是ByteBuffer。行"chari =mBuf.getChar();“崩溃。
你对这个问题怎么看?
发布于 2014-11-20 16:10:42
您错误地认为一个字符的大小是一个字节。在Java中,char是两个字节,所以mBuf.getChar()消耗了两个字节。文档甚至声明该方法读取下两个字节。
如果使用由CharBuffer返回的mBuf.asCharBuffer(),则缓冲区的that ()方法将给出所需的数字。
更新:基于您的评论,我现在了解到缓冲区实际上包含一个字节字符。由于Java处理整个Unicode配置表(其中包含数十万个字符),所以必须告诉它您使用的是哪个字符集(从字符到字节编码):
// This is a pretty common one-byte charset.
Charset charset = StandardCharsets.ISO_8859_1;
// This is another common one-byte charset. You must use the same charset
// that was used to write the bytes in your ObjectiveC program.
//Charset charset = Charset.forName("windows-1252");
CharBuffer c = charset.newDecoder().decode(mBuf);
char[] charBuff = new char[c.remaining()];
c.get(charBuff);https://stackoverflow.com/questions/27044010
复制相似问题