Why does DataOutputStream.writeUTF() add additional 2 bytes at the beginning?

后端 未结 2 434
说谎
说谎 2020-12-05 07:52

When I was trying to parse xml using sax over sockets I came across a strange occurence. Upon analysing I noticed that DataOutputStream adds 2 bytes in front of my data.

2条回答
  •  遥遥无期
    2020-12-05 08:41

    Always use the same type of stream when reading and writing data. If you are feeding the stream directly into a sax parser, then you should not use a DataOutputStream.

    Just use

    BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
    bos.write(os.getBytes("UTF-8"));
    

提交回复
热议问题