Java 正在读取使用DataOutputStream writeUTF()方法发送的okio上的UTF字符串
我在当前的android项目中使用okhttp和okio来获取和读取我正在发送的一些二进制流,在流中,我使用该方法发送一些UTF值Java 正在读取使用DataOutputStream writeUTF()方法发送的okio上的UTF字符串,java,android,utf-8,okhttp,okio,Java,Android,Utf 8,Okhttp,Okio,我在当前的android项目中使用okhttp和okio来获取和读取我正在发送的一些二进制流,在流中,我使用该方法发送一些UTF值 DataOutputStream.writeUTF(). “okhttp”提供了两个选项:获取inputStream,因此可以直接使用方法DataInputStream.readUTF() 另一个选项是将流作为BufferedSource并使用其方法读取数据,我很惊讶读取UTF的方法需要您为其提供字节计数 String readUtf8(long byteCoun
DataOutputStream.writeUTF().
“okhttp”提供了两个选项:获取inputStream,因此可以直接使用方法DataInputStream.readUTF()
另一个选项是将流作为BufferedSource并使用其方法读取数据,我很惊讶读取UTF的方法需要您为其提供字节计数
String readUtf8(long byteCount)
为了读取来自服务器的UTF,我添加了以下方法
public static String readUtf(BufferedSource bufferedSource)throws IOException {
int length = readShortAsUnsignedInt(bufferedSource);
return bufferedSource.readUtf8(length);
}
public static int readShortAsUnsignedInt(BufferedSource bufferedSource)throws IOException {
return bufferedSource.readShort() & 0xffff;
}
这是正确的方法吗