com.sample.netty.socket.utils.MessageDecoder.java Source code

Java tutorial

Introduction

Here is the source code for com.sample.netty.socket.utils.MessageDecoder.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sample.netty.socket.utils;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import java.util.List;

/**
 *
 * @author Vitor
 */
public class MessageDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
        if (in.readableBytes() < 4) {
            return;
        }
        StringBuilder sb = new StringBuilder();
        while (in.isReadable()) {
            sb.append((char) in.readByte());
        }
        out.add(sb.toString());
    }
}