简体   繁体   English

即使类已序列化,Blob对象仍无法正常工作

[英]Blob object not working properly even though the class is seralized

I have class which is seralized and does convert a very large amount of data object to blob to save it to database.In the same class there is decode method to convert blob to the actual object.Following is the code for encode and decode of the object. 我有一个序列化的类,并确实将大量数据对象转换为blob以将其保存到数据库中。在同一类中,有一个decode方法将blob转换为实际对象。以下是编码和解码的代码宾语。

private byte[] encode(ScheduledReport schedSTDReport)
{
    byte[] bytes = null;
    try
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(schedSTDReport);
        oos.flush(); 
        oos.close(); 
        bos.close();
        //byte [] data = bos.toByteArray();
        //ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //GZIPOutputStream out = new GZIPOutputStream(baos);
        //XMLEncoder encoder = new XMLEncoder(out);
        //encoder.writeObject(schedSTDReport);
        //encoder.close();
        bytes = bos.toByteArray();
        //GZIPOutputStream out = new GZIPOutputStream(bos);
        //out.write(bytes);
        //bytes = bos.toByteArray();

    }
    catch (Exception e)
    {
        _log.error("Exception caught while encoding/zipping Scheduled STDReport", e);
    }
    decode(bytes);
    return bytes;
}


/*
 * Decode the report definition blob back to the
 * ScheduledReport object.
 */
private ScheduledReport decode(byte[] bytes)
{
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ScheduledReport sSTDR = null;
    try
    {
        ObjectInputStream ois = new ObjectInputStream(bais);

        //GZIPInputStream in = new GZIPInputStream(bais);
        //XMLDecoder decoder = new XMLDecoder(in);
        sSTDR = (ScheduledReport)ois.readObject();//decoder.readObject();
        //decoder.close();
    }
    catch (Exception e)
    {
        _log.error("IOException caught while decoding/unzipping Scheduled STDReport", e);
    }
    return sSTDR;
}

The problem here is whenver I change something else in this class means any other method,a new class version is created and so the new version the class is unable to decode the originally encoded blob object. 这里的问题是,每当我在此类中进行其他更改时,意味着任何其他方法,都会创建新的类版本,因此该类的新版本无法解码原始编码的Blob对象。 The object which I am passing for encode is also seralized object but this problem exists. 我传递给编码的对象也是序列化对象,但是存在此问题。 Any ideas thanks 任何想法谢谢

Yup, Java binary serialization is pretty brittle :( 是的,Java二进制序列化非常脆弱:(

You can add a static serialVersionUID field to the class so that you can control the version numbers... this should prevent problems due to adding methods. 您可以将静态serialVersionUID字段添加到类中,以便可以控制版本号...这样可以避免由于添加方法而引起的问题。 You'll still run into potential issues when fields are added though. 不过,在添加字段时,您仍然会遇到潜在的问题。 See the JavaDocs for Serializable for some more details. 有关更多详细信息,请参见JavaDocs for Serializable

You might want to consider using another serialization format such as Protocol Buffers to give you more control though. 您可能需要考虑使用其他序列化格式( 例如协议缓冲区)来提供更多控制权。

您可以实现java.io.Externalizable以便能够控制序列化和反序列化中的预期内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ClassNotFoundException,即使包含该类的jar正确存在于类路径中 - ClassNotFoundException even though the jar containing the class is properly present in the classpath ClassNotFoundException即使该类已正确定义(我认为) - ClassNotFoundException even though the class is properly defined (I think) 即使在同一类中声明了对象也无法找到对象 - Unable to find object even though it is declared in same class 为什么这个类编译即使它没有正确实现其接口? - Why does this class compile even though it does not properly implement its interface? 即使存在.class文件也没有类错误 - No class error even though the .class files are present 对象输入流类强制转换异常-即使不应该发生 - Object Input Stream Class cast exception - even though it shouldn't happen 即使在上面的类中也找不到变量 - Variable not found even though it's in the class above 为什么没有调用A类中的toString? - Why is toString in class A evoked even though it is not called? 生成 equals/hashCode 实现但没有调用超类,即使此 class 不扩展 java.lang.Object - Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object KeyListener即使看似正确也不工作 - KeyListener Not Working even though it seems correct
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM