有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将字节流转换为UTF8字符?

我需要将字节流转换为一行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();
    }   
  }
}

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


共 (2) 个答案

  1. # 1 楼答案

    您可以使用一个简单的类来跟踪字符,并且只有在获得完整的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);
            }
        }
    }
    
  2. # 2 楼答案

    将字节获取代码包装在InputStream中,并将其传递给InputStreamReader

        InputStreamReader isr = new InputStreamReader(new InputStream() {
            @Override
            public int read() throws IOException {
                return xx();// wherever you get your data from.
            }
        }, "UTF-8");
        while(true) {
            try {
                if(isr.read() == THE_CHAR_WE_ARE_WAITING_FOR)
                    System.out.println("hurray!");
            } catch(IOException e) {
                e.printStackTrace(); 
            }
        }