Kryo在读取时导致缓冲区下溢异常

时间:2016-10-25 20:17:28

标签: java deserialization kryo

我正在序列化:

    private byte[] serialize()
{
    KryoPool pool = new KryoPool.Builder(factory).softReferences().build();
    Kryo kryo = pool.borrow();
    Output output = new Output(0, 1024);
    kryo.writeClassAndObject(output, readsSetNode);
    byte[] bytes = output.toBytes();
    output.close();
    pool.release(kryo);
    return bytes;
}

和反序列化:

KryoPool pool = new KryoPool.Builder(factory).softReferences().build();
        Kryo kryo = pool.borrow();

        Input input = new Input(bytes);
        HashMap<NodeStorage, NodeStorage> deserialized = (HashMap<NodeStorage, NodeStorage>) kryo.readClassAndObject(input);
        input.close();

        pool.release(kryo);

hashmap“deserialized”包含正确的对象,但不幸的是,日志被垃圾邮件带有缓冲区下溢异常: 这似乎只有在我尝试调试应用程序时才会发生。 这是否会导致任何问题,或者这种行为是否正常?

  

com.esotericsoftware.kryo.KryoException:缓冲区下溢。       在com.esotericsoftware.kryo.io.Input.require(Input.java:199)       在com.esotericsoftware.kryo.io.Input.readVarInt(Input.java:373)       at com.esotericsoftware.kryo.util.DefaultClassResolver.readClass(DefaultClassResolver.java:127)       在com.esotericsoftware.kryo.Kryo.readClass(Kryo.java:693)       在com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:804)       at main.java.com.bag.server.TestServer.appExecuteUnordered(TestServer.java:78)       at bftsmart.tom.server.defaultservices.DefaultRecoverable.executeUnordered(DefaultRecoverable.java:417)       在bftsmart.tom.ServiceReplica.receiveReadonlyMessage(ServiceReplica.java:214)       在bftsmart.tom.core.DeliveryThread.deliverUnordered(DeliveryThread.java:289)       在bftsmart.tom.core.TOMLayer.requestReceived(TOMLayer.java:290)       at bftsmart.communication.client.netty.NettyClientServerCommunicationSystemServerSide.channelRead0(NettyClientServerCommunicationSystemServerSide.java:184)       at bftsmart.communication.client.netty.NettyClientServerCommunicationSystemServerSide.channelRead0(NettyClientServerCommunicationSystemServerSide.java:61)       在io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)       at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:292)       at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:278)       at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:277)       at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:264)       at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:292)       at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:278)       at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:962)       at io.netty.channel.nio.AbstractNioByteChannel $ NioByteUnsafe.read(AbstractNioByteChannel.java:131)       在io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:528)       at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:485)       at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:399)       在io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:371)       at io.netty.util.concurrent.SingleThreadEventExecutor $ 2.run(SingleThreadEventExecutor.java:112)       at io.netty.util.concurrent.DefaultThreadFactory $ DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)       在java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,你应该修改你的序列化函数:

    private byte[] serialize()
{
    KryoPool pool = new KryoPool.Builder(factory).softReferences().build();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    Kryo kryo = pool.borrow();
    Output output = new Output(byteArrayOutputStream);
    kryo.writeClassAndObject(output, readsSetNode);
    output.close();
    pool.release(kryo);
    byte[] bytes = byteArrayOutputStream.toByteArray();;
    return bytes;
}