TooTallNate/Java-WebSocket — Issue #57: buf.rewind() in WebSocket.channelWrite breaks partially written buffers
Author: jribble
Created: 2012-01-11 23:57:57+00:00
State: open

I'm using Java-WebSocket to write a [websockify](https://github.com/kanaka/websockify) implementation in Java. I'm using it to run VNC data to [noVNC](http://kanaka.github.com/noVNC/). It was pretty quick and easy to implement, but my data would get corrupted after a little while and the VNC client would shut down with data errors.

I tracked this down to a problem with partially written buffers. WebSocket.sendFrame will attempt to write a buffer with channelWrite. If the buffer is only partially written it will be placed on a queue and the next time sendFrame is called the rest of the buffer will be sent. The problem is that channelWrite rewinds the buffer, so it resends the portion of the buffer that was already sent.

In my opinion, channelWrite should not change the state of the buffer. Buffers should be prepared for writing out to the socket before getting to channelWrite. Partially written buffers will then be continued from the correct location.