我在将 UTF8 字符串从 c 套接字发送到 java 套接字时遇到了一些问题。 以下方法工作正常:
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
main.title = in.readLine();
但是我需要一个 int java.io.InputStream.read(byte[] b, int offset, int length)
方法,它对于 BufferedReader 不存在。所以然后我尝试使用 DataInputStream
DataInputStream in2 = new DataInputStream(socket.getInputStream());
但是它读到的一切都是垃圾。
然后我尝试使用 DataInputStream
中的 readLine()
方法,但这并没有给我正确的 UTF8 字符串。
你看到了我的困境。我不能为一个 InputStream 使用两个阅读器吗?或者我可以转换 DataInputStream.readLine()
结果并将其转换为 UTF8 吗?
谢谢, 马丁
最佳答案
我们从design of the UTF-8 encoding知道值 0x0A
的唯一用法是换行 ('\n'
)。因此,你可以一直读下去,直到你击中它:
/** Reads UTF-8 character data; lines are terminated with '\n' */
public static String readLine(InputStream in) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
while (true) {
int b = in.read();
if (b < 0) {
throw new IOException("Data truncated");
}
if (b == 0x0A) {
break;
}
buffer.write(b);
}
return new String(buffer.toByteArray(), "UTF-8");
}
我假设您的协议(protocol)使用 \n
作为行终止符。如果没有 - 好吧,指出您要写入的约束通常很有用。
关于java - 使用 UTF8 的 DataInputStream 和 readLine(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6370808/