java - 使用 UTF8 的 DataInputStream 和 readLine()

标签 java utf-8

我在将 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/

相关文章:

Java:干净地将异常重定向到字符串

java - 如何将 utf-8 字符串附加到属性文件

java - 使用java代码通过传递URL下载文件

java - 如何向http请求添加未知数量的 header 参数?

java - 从 fragment 更新 UI

SQL Server 函数返回问号而不是实际结果

sql - 使用 dplyr 和 SQLite 进行 UTF-8 编码

c - 将 int Unicode 代码点保存到 UTF-8 文件

Java UTF-16 到 UTF-8 的转换

java - Java中null是如何实现的?