Search code examples
javastringbinarywebsocketfile-transfer

Binary vs. String transfer over a stream


I am making a little experiment with WebSockets and Java. Apperantly, according to the last draft of WebSocket, the message can be binary or a plain string. I use Webbit server and it has two functions:

public void onMessage(WebSocketConnection connection, String message) 

public void onMessage(WebSocketConnection connection, byte[] message)

I wonder what makes a difference. Is byte[] faster? Or why does it matter? I can write everything I write with the bytes because even a string is composed into bytes while transfer, so why do we have two multiple methods? Only Google Chrome 15 Beta and 16 Dev supports binary transfer, so I was thinking of using Base64 encoding/decoding on both client and server. Is this the only difference? What if I just read each byte, compose them into a string and send them? I think, only difference will be that not all bytes are String chars, so I wouuld just add an overhead when converting to a String?

tl;dr -> What is the difference between Binary transfer and String transfer?


Solution

  • The WebSocket protocol (HyBi) supports two different payload types: text, binary. The text payload is UTF-8 encoded string data. Any ASCII codes above 127 in the string that you send will be converted into a two-byte UTF-8 encoding. To successfully send/receive raw binary data you will probably want to encode the data in something like base64 (which is UTF-8 compatible).

    The binary payload type is sent directly. The bytes are sent as-is in the payload. This is more bandwidth efficient. It is means that you don't have to do an encode/decode step. The bytes you send get sent directly, and the bytes you receive can be accessed directly with no decoding.