package cc.blynk.common.handlers.common.encoders;

import cc.blynk.common.enums.Command;
import cc.blynk.common.model.messages.Message;
import cc.blynk.common.model.messages.MessageBase;
import cc.blynk.common.utils.Config;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * Encodes java message into a bytes array.
 *
 * The Blynk Project.
 * Created by Dmitriy Dumanskiy.
 * Created on 2/1/2015.
 */
public class MessageEncoder extends MessageToByteEncoder<MessageBase> {

    @Override
    protected void encode(ChannelHandlerContext ctx, MessageBase message, ByteBuf out) throws Exception {
        out.writeByte(message.command);
        out.writeShort(message.id);
        out.writeShort(message.length);

        if (message.command != Command.RESPONSE && message.length > 0) {
            out.writeBytes(((Message) message).body.getBytes(Config.DEFAULT_CHARSET));
        }
    }
}

@@ -755,48 +755,8 @@
 id);
-%0A        out.writeShort(message.length);
 %0A%0A  
@@ -785,9 +785,9 @@
 and 
-!
+=
 = Co
@@ -804,120 +804,285 @@
 ONSE
- && message.length %3E 0) %7B%0A            out.writeBytes(((Message) message).body.getBytes(Config.DEFAULT_CHARSET));
+) %7B%0A            out.writeShort(message.length);%0A        %7D else %7B%0A            byte%5B%5D body = ((Message) message).body.getBytes(Config.DEFAULT_CHARSET);%0A            out.writeShort(body.length);%0A            if (body.length %3E 0) %7B%0A                out.writeBytes(body);%0A            %7D
 %0A   

Correct message body length for unicode chars.