【发布时间】:2014-04-29 01:56:00
【问题描述】:
有一个遗留应用程序通过 TCP 处理 json。
我用的是netty服务器,我想通过Gson把json转成Pojo。
为了做到这一点,我转换创建了一个解码器,它将一个 ByteBuf 作为输入并创建了我的对象。
问题是 Bytebuf 的大小为 1024,而 json 比这大得多 (>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 个字节
如何将 ByteBuf 容量设置为其最大容量? (调试时我看到 ByteBuf 是一个 SimpleLeakAwareByteBuf,maxCapacity 为 2147483647)
我怎样才能摆脱这个限制?
【问题讨论】: