我在字节数组中有一些数据,之前使用非阻塞 IO 从网络会话中检索到(以促进多个通道)。
数据的格式本质上是
varint: length of text
UTF-8: the text
我试图找出一种有效提取文本的方法,因为它的起始位置是不确定的(因为 varint 的长度是可变的)。我有一些非常接近的东西,但对于一个小问题,这里是:
import com.clearspring.analytics.util.Varint;
// Some fields for your info
private final byte replyBuffer[] = new byte[32768];
private static final Charset UTF8 = Charset.forName ("UTF-8");
// ...
// Code which extracts the text
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(replyBuffer);
DataInputStream inputStream = new DataInputStream(byteInputStream);
int textLengthBytes;
try {
textLengthBytes = Varint.readSignedVarInt (inputStream);
}
catch (IOException e) {
// I don't think we should ever get an IOException when using the
// ByteArrayInputStream class
throw new RuntimeException ("Unexpected IOException", e);
}
int offset = byteInputStream.pos(); // ** Here lies the problem **
String textReceived = new String (replyBuffer, offset, textLengthBytes, UTF8);
这个想法是缓冲区中的文本偏移量由 byteInputStream.pos() 指示。但是,该方法受到保护。
在我看来,在解码 varint 后获得文本“其余部分”的唯一方法是使用将其全部复制到另一个缓冲区的东西,但这对我来说似乎相当浪费。
直接从底层缓冲区构造字符串应该没问题,因为在此之后我不再关心 byteInputStream 或 inputStream 的状态。所以我试图找出一种计算偏移量的方法,或者换句话说,有多少字节 Varint.readSignedVarInt 消耗。也许有一种有效的方法可以将 Varint.readSignedVarInt 返回的整数值转换为编码中占用的字节数?