0

The server receives byte array as inputstream,and I wrapped the stream with DataInputStream.The first 2 bytes indicate the length of the byte array,and the second 2 bytes indicate a flag,and the next bytes consist of the content.My problem is the content contains unicode character which has 2 bytes.How can I read the unicode char ? My prev code is:

    DataInputStream dis = new DataInputStream(is);
    int length = dis.readUnsignedShort();
    int flag = dis.readUnsignedShort();

    String content = "";
    int c;
    for (int i = 0; i < length - 4; i++) {
        c = dis.read();
        content += (char) c;
    }

It only can read ascII.thxs for your helps!

Leo Xu
  • 174
  • 3
  • 11
  • See the answer by BalusC here: http://stackoverflow.com/questions/4505057/datainputstream-and-utf-8 – L.Butz Nov 04 '13 at 09:38
  • See also the [readChar](http://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html#readChar()) method if these code points are big-endian UTF-16. – McDowell Nov 04 '13 at 17:04

1 Answers1

0

This depends on encoding scheme of your input. If you do not want to do the heavy-lifting, you could use Apache IOUtils and convert the bytes to unicode string. Example : IOUtils.toString(bytes, "UTF-8")

Karthik
  • 1,005
  • 8
  • 7
  • I have solved my problem using:String s = new String(bytes, "UTF-8"); I think it is similar to your solution~thx as well – Leo Xu Nov 05 '13 at 03:07