Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从ByteBuffer读取特殊UTF-8字符_Java_Nio - Fatal编程技术网

Java 从ByteBuffer读取特殊UTF-8字符

Java 从ByteBuffer读取特殊UTF-8字符,java,nio,Java,Nio,我怎样才能从ByteBuffer中读取特殊字符ByteBuffer.wrap(“\u0001.getBytes()).asCharBuffer().get()引发BufferUnderflow异常。据我所知,ByteBuffer.getChar()总是前进两个字节,而这个字符串中只有一个字节。你是说你想在ByteBuffer中迭代UTF-8编码字符而不通过字符串吗?如果你确定ByteBuffer包含UTF-8编码字符,使用StandardCharsets.UTF_8.decode(buffer)

我怎样才能从ByteBuffer中读取特殊字符
ByteBuffer.wrap(“\u0001.getBytes()).asCharBuffer().get()
引发
BufferUnderflow
异常。据我所知,
ByteBuffer.getChar()
总是前进两个字节,而这个字符串中只有一个字节。

你是说你想在ByteBuffer中迭代UTF-8编码字符而不通过字符串吗?如果你确定ByteBuffer包含UTF-8编码字符,使用
StandardCharsets.UTF_8.decode(buffer).toString()
@that otherguy是的,我希望尽可能避免堆分配。faik没有内置的功能从字节缓冲区读取单个UTF-8编码的代码点,所以您必须编写自己的。为什么不使用
CharBuffer.wrap()
?取消所有往返行程?