Netty ByteBuf解码时无法获得大量输入的完整消息 [英] Netty ByteBuf dont get the full message of a large input when decoding

查看:642
本文介绍了Netty ByteBuf解码时无法获得大量输入的完整消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个旧版应用程序可以通过TCP执行json.

There is a legacy application which does json over TCP.

我使用一台Netty服务器,我想通过Gson将json转换为Pojo.

I use a netty server, and I want to convert the json into a Pojo via Gson.

为此,我转换创建了一个解码器,该解码器将ByteBuf作为输入并创建我的对象".

In order to do this, I convert created a Decoder which take as input a ByteBuf and create My Object.

问题在于Bytebuf的大小为1024,而json的大小要大得多(> 20mo)

The problem is the Bytebuf has a size of 1024, and the json is much much more bigger than that (>20mo)

@Sharable
public class RequestDecoder extends MessageToMessageDecoder<ByteBuf> {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
        InputStream json = new ByteBufInputStream(msg);
        InputStreamReader inputStreamReader = new InputStreamReader(json, Charsets.UTF_8);
        JsonReader reader;
        reader = new JsonReader(inputStreamReader);
        reader.setLenient(true);
        Request object = new Gson().fromJson(reader, Request.class);
        try {
            reader.close();
        } catch (IOException e) {
            throw new RuntimeException("uncloseable reader", e);
        }
        out.add(object);
    }
}

我总是得到一个:

com.google.gson.stream.MalformedJsonException: Unterminated string at line 1 column 1020

当我调试时,我仅在"ByteBuf msg"中获得1024个第一个字节

and when I debug I only get the 1024 first byte in the "ByteBuf msg"

如何将ByteBuf容量设置为其最大容量? (调试时,我看到ByteBuf是一个SimpleLeakAwareByteBuf,最大容量为2147483647)

How can I set a ByteBuf capacity to its max capacity? (when debugging I see that ByteBuf is a SimpleLeakAwareByteBuf with a maxCapacity of 2147483647)

如何摆脱这种局限性?

推荐答案

我在管道的开头添加了DelimiterBasedFrameDecoder:

I added a DelimiterBasedFrameDecoder at the beginning of the pipeline :

pipeline.addLast("framer", new DelimiterBasedFrameDecoder(Integer.MAX_VALUE, Unpooled.wrappedBuffer(new byte[]{'E', 'O', 'F', '\n'})));

因为我发现所有json均以"EOF \ n"终止(由于遗留问题)

Because I discovered that all json were terminated with "EOF\n" (due to legacy issues)

现在对我来说还可以,但是当这个遗留问题解决后会发生什么?

That's ok for me now, but what will happen when this legacy issue is resolved?

这篇关于Netty ByteBuf解码时无法获得大量输入的完整消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆