开发者

Is there a simple way to append a byte to a StringBuffer and specify the encoding?

开发者 https://www.devze.com 2023-02-27 05:38 出处:网络
Question What is the simplest way to append a byte to a StringBuffer (i.e. cast a byte to a char) and specify the character encoding used (ASCII, UTF-8, etc)?

Question

What is the simplest way to append a byte to a StringBuffer (i.e. cast a byte to a char) and specify the character encoding used (ASCII, UTF-8, etc)?

Context

I want to append a byte to a stringbuffer. Doing so requires casting the byte to a char:

myStringBuffer.append((char)nextByte);

However, the code above uses the default character encoding for my machine (which is MacRoman). Meanwhile, other components in the system/network require UTF-8. So I need to so something like:

try {
    myStringBuffer.append(new String(new Byte[]{nextByte}, "UTF-8"));
} catch (UnsupportedEncodingException e) {
    //handle error
}

Which, frankly, is pretty ugly.

Surely, there's a better way (other than b开发者_JAVA百科reaking the same code into multiple lines)???????


The simple answer is 'no'. What if the byte is the first byte of a multi-byte sequence? Nothing would maintain the state.

If you have all the bytes of a logical character in hand, you can do:

sb.append(new String(bytes, charset));

But if you have one byte of UTF-8, you can't do this at all with stock classes.

It would not be terribly difficult to build a juiced-up StringBuffer that uses java.nio.charset classes to implement byte appending, but it would not be one or two lines of code.

Comments indicate that there's some basic Unicode knowledge needed here.

In UTF-8, 'a' is one byte, 'á' is two bytes, '丧' is three bytes, and '

0

精彩评论

暂无评论...
验证码 换一张
取 消