[英]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.