Buffer Underflow ERROR

60 views
Skip to first unread message

Yu wai Hlaing

unread,
Oct 21, 2015, 5:19:25 AM10/21/15
to kryo-users
I've been using kryo (2.21). I'm trying to serialize an object.  But I get a Buffer Underflow exception while getting the array.
 
Exception in thread "AWT-EventQueue-0" com.esotericsoftware.kryo.KryoException: Buffer underflow.
    at com.esotericsoftware.kryo.io.Input.require(Input.java:156)
    at com.esotericsoftware.kryo.io.Input.readInt(Input.java:337)
    at com.esotericsoftware.kryo.Kryo.readReferenceOrNull(Kryo.java:762)
    at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:624)


I save the object as...
               
               
Kryo kryo = new Kryo();
               
Output output = null;
               
try
               
{
                    kryo
= kryos.get();
                    kryo
.register(Codes.class,new CompatibleFieldSerializer(kryo,Codes.class));
                    kryo
.register(ArrayList.class,new CollectionSerializer());
                    kryo
.register(String.class);
                   
RandomAccessFile raf = new RandomAccessFile(f, "rw");
                    output
= new Output(new BufferedOutputStream(new FileOutputStream(raf.getFD()), bufferSize));
                    kryo
.writeObject(output, myObject);
                   
System.out.println("Writing in file with Kryo...............");
                 
} finally
                 
{
                   
if (output != null)
                   
{
                      output
.close();
                   
}
                 
}
                  output
.close();

And read it as...

try{
                            code_from_file
.clear();
                            kryo
= kryos.get();
                            kryo
.register(Codes.class,new CompatibleFieldSerializer(kryo,Codes.class));
                            kryo
.register(ArrayList.class,new CollectionSerializer());
                            kryo
.register(String.class);
                           
RandomAccessFile raf = new RandomAccessFile(f, "rw");
                            input
= new Input(new BufferedInputStream(new FileInputStream(raf.getFD()), bufferSize));
                           
Codes obj = new Codes();
                             
while( (obj = (Codes) kryo.readObject(input, Codes.class)) != null)
                             
{  
                                 
if(obj instanceof Codes)
                                 
{
                                     code_from_file
.add(obj);
                                 
}
                                 
System.out.println("obj  :  " + obj.toString());
                           
}
                       
}catch (EOFException ex)
                       
{
                       
} catch (FileNotFoundException ex) {
                            ex
.printStackTrace();
                       
} catch (IOException ex) {
                            ex
.printStackTrace();
                       
} finally
                         
{
                           
if (input != null)
                           
{
                              input
.close();
                           
}
                       
}
                        input
.close();

private static ThreadLocal<Kryo> kryos = new ThreadLocal<Kryo>() {
       
protected Kryo initialValue() {
           
Kryo kryo = new Kryo();
           
// configure kryo instance, customize settings
           
return kryo;
       
};
   
};






Reply all
Reply to author
Forward
0 new messages