开发者

How to cache *any* object type to memory/disk in java?

Is there a generic way to cache any type of object (be in a java class, or a wor开发者_如何学God document etc.) to memory or disk?

Is simply serializing the object, and retaining the file extension (if it has one) enough to rebuild the object?


You seems to be using the word Object to describe 2 different things.

If your object is a Java object then having that object implement the Serializable is enough if you then use the java methods to serialize/de-serialize the object.

If you want to cache arbitrary data from the filesystem, the best way is to read it in an byte array(Or ArrayList). Then you can just write the array back to the disk or where you want it.


If you're talking about the inbuilt Java serialization, then you wouldn't even need to retain the file extension. The serialized form has enough information such that the deserialization process will produce an identical object without any additional help. I suppose that depending on how your code is structured, though, you might need to store some metadata for your own benefit so that you know what to cast the resulting Object as.

Note that Java serialization doesn't seem to fit your requirements, though - it cannot serialize any type of object, only those that implement Serializable. Perhaps you need to think a little more about what you mean by "simply serializing the object", since that's the rub.


No.

There is a class of objects which cannot be deserialized in a meaningful way. Think of an open network connection which is in the middle of transferring a file. You can not store that to disk, close your app, open your app, deserialize that connection and expect that it "just continues".

Java has an interface Serializable which indicates that an object can be serialized. It's up to you to ensure that is indeed possible. Typically an object is Serializable if all the data it holds is Serializable, or that data which is not Serializable is marked transient.

This is not to say that you could not, theoretically, dump the memory contents to a file as a byte stream, and read it back again later. You could build something like that I suppose. But to expect that it works is a different thing altogether.

In short, it is not possible to serialize any type. However, there is a generic way to serialize Java objects which are marked to be Serializable.


Not sure what you mean by "or a word document". Serialization can be used for disk caching, not sure what the purpose of using it in memory would be since it would probably be far faster to simply keep the original object.

A more robust solution might be ehcache it can manage the size of the cache as well as moving it between memory and disk.


If you're wondering about the cross platform (disk or memory) persistence part of the question, look at Java's Preferences class.


My, what a lot of answers!

Any object can make itself serializable by implementing java.io.Serializable.

But:

A default serialiser is implemented in ObjectOutputStream, which simply walks the object tree. This is fine for simple javabean type objects, but it can have undesirable effects such as system objects being serialised (I once inspected a serialised java object file and found that it was including all of the system timezone objects). And, of course, if your object has objects inside it that are not serializable (and not transient), then ObjectOutputStream will throw an exception.

(actually, even for JavaBean objects the default serializer it awful - the default serializer emits the classname of java.lang.String for every string field.)

So if your object is complicated, then you really should implement Externalizable and write a serialiser and deserializer with some smarts.

http://download.oracle.com/javase/6/docs/platform/serialization/spec/serial-arch.html#7185

So basically - no, you can't serialise any old object. You have to design object that are intended to be serialised and, ideally, that have some smarts about how they get themselves to and from a stream.


You cannot serialize any object in Java. Moreover, Java uses shallow copying(or is it called something else) for serialization, so if you want to seialize something like a HashMap, it might not save your data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜