如何将字节流转换为UTF-8字符?

yeg*_*256 5 java utf-8 character-encoding

我需要将字节流转换为UTF-8字符行.在这一行中对我来说唯一重要的角色是最后一个.这种转换应该在一个周期内进行,因此性能非常重要.一种简单而低效的方法是:

public class Foo {
  private ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  void next(byte input) {
    this.buffer.write(input);
    String text = this.buffer.toString("UTF-8"); // this is time consuming
    if (text.charAt(text.length() - 1) == THE_CHAR_WE_ARE_WAITING_FOR) {
      System.out.println("hurray!");
      this.buffer.reset();
    }   
  }
}
Run Code Online (Sandbox Code Playgroud)

字节数组到字符串的转换发生在每个输入字节上,据我所知,这是非常无效的.是否有可能以其他方式保留前一周期的字节到文本转换结果?

Joa*_*son 6

您可以使用一个简单的类来跟踪字符,并且仅在获得完整的UTF8序列时才进行转换。这是一个示例(不添加任何错误检查)

class UTF8Processor {
    private byte[] buffer = new byte[6];
    private int count = 0;

    public String processByte(byte nextByte) throws UnsupportedEncodingException {
        buffer[count++] = nextByte;
        if(count == expectedBytes())
        {
            String result = new String(buffer, 0, count, "UTF-8");
            count = 0;
            return result;
        }
        return null;
    }

    private int expectedBytes() {
        int num = buffer[0] & 255;
        if(num < 0x80) return 1;
        if(num < 0xe0) return 2;
        if(num < 0xf0) return 3;
        if(num < 0xf8) return 4;
        return 5;
    }
}

class Bop
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // Create test data.
        String str = "Hejsan åäö/?ya";
        byte[] bytes = str.getBytes("UTF-8");

        String ch;

        // Processes byte by byte, returns a valid UTF8 char when 
        //there is a complete one to get.

        UTF8Processor processor = new UTF8Processor();

        for(int i=0; i<bytes.length; i++)
        {
            if((ch = processor.processByte(bytes[i])) != null)
                System.out.println(ch);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Aur*_*and 2

根据评论:

它是换行符(0x0A)

你的next方法可以只检查:

if ((char)input == THE_CHAR_WE_ARE_WAITING_FOR) {
    //whatever your logic is.
}
Run Code Online (Sandbox Code Playgroud)

您不必对 < 128 个字符进行任何转换。