Results 1 to 17 of 17
- 09-07-2010, 09:15 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 22
- Rep Power
- 0
Enum singleton serialization and deserialization
I've found a nice Singleton implementation in the book "Effective Java - 2nd edition (Joshua Bloch)". I understand how to implement singleton via enum but I can't find the way how to serialize and deserialize it.
Here is the code of my enum:
All I need is to know how to write save and load function.Java Code:public enum Parameters implements Serializable{ INSTANCE; public String playerString; private short Collumns; private short Lines; private short Interval; private Parameters() { File f = new File("config.cfg"); if (f.exists()){ if(!load()){ SetDefaultParameters(); } } else{ SetDefaultParameters(); } } private void SSetDefaultParameters(){ //fill the parameters with default values save(); } private void save(){ // save to file } private boolean load(){ try{ //load from file }catch (Exception ex){ return false; } return true; } }
thanks...Last edited by bassfero; 09-07-2010 at 10:15 AM.
- 09-07-2010, 11:01 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Why do you need to write save and load methods?
ETA: Scratch that...since it's serialisable you should be able to simply create an ObjectStream (Output for save, Input for load) to the file, and stream this.
ETA2: On load, since you will already have INSTANCE, I'm not too sure what'll happen when you read a new Parameters object in via serilisation, but assuming it doesn't throw a wobbly you'll then need to assign the sttributes from the read in one to the "real" enum one.Last edited by Tolls; 09-07-2010 at 11:06 AM.
- 09-07-2010, 11:05 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 22
- Rep Power
- 0
this singleton supose to be a configuration, so i want to be able to save it in file
- 09-07-2010, 11:12 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
See my edits...
- 09-07-2010, 11:41 AM #5
Member
- Join Date
- Jul 2010
- Posts
- 22
- Rep Power
- 0
Well ObjectOutputStream.writeObject(INSTANCE) doesnt throw an exception and create a file, but this file has only 5bytes, so i donīt think that all enum attributes are inside (currently 16 attributes). ObjectInputStream.readObject() is not usable. I cannot assign object to INSTANCE or this.
Last edited by bassfero; 09-07-2010 at 11:41 AM. Reason: my poor english grammar
- 09-07-2010, 11:54 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
You don't assing the read object to this, you assign it to another instance of Parameters (which is why I'm not convinced this'll work), and then assign the individual attributes of this temporary Parameters object to this.
I assume from your description then that the above isn't your real code.
- 09-07-2010, 12:18 PM #7
Member
- Join Date
- Jul 2010
- Posts
- 22
- Rep Power
- 0
that is exactly what I am trying to avoid
whole enum has 288 lines, i pasted only part of it
Anyway, if there's not way how to save/load enum within few lines I am going to implement this singleton another way. I expected a simple solution (due to book).
Thank you for your time Tolls
- 09-07-2010, 12:32 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Does the book serialise them?
I ask because I've just done what you're trying to do and this:
ETA: note ois is an ObjectInputStream.Java Code:TestEnum temp = (TestEnum)ois.readObject();
throws this:
which is why I talked about it throwing a wobbly.Java Code:java.io.InvalidObjectException: enum constant INSTANCE does not exist in class test.TestEnum at java.io.ObjectInputStream.readEnum(ObjectInputStream.java:1704) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1326) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351) at test.TestEnum.<init>(TestEnum.java:26) at test.TestEnum.<clinit>(TestEnum.java:14) at Scratch.main(Scratch.java:27) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) 0 at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110) Caused by: java.lang.IllegalArgumentException: test.TestEnum is not an enum type at java.lang.Class.enumConstantDirectory(Class.java:2965) at java.lang.Enum.valueOf(Enum.java:191) at java.io.ObjectInputStream.readEnum(ObjectInputStream.java:1702) ... 10 more
- 09-07-2010, 12:42 PM #9
Member
- Join Date
- Jul 2010
- Posts
- 22
- Rep Power
- 0
- 09-07-2010, 01:58 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
But does he serialise them in the book?
Does he have code reading them in using an Object stream?
What does the book say about what you're trying to do?
- 09-08-2010, 08:01 AM #11
Member
- Join Date
- Jul 2010
- Posts
- 22
- Rep Power
- 0
This is quote from book
There are some sample codes in the book but not about serialization.This approach is functionally equivalent to the public field approach, except that it
is more concise, provides the serialization machinery for free, and provides an
ironclad guarantee against multiple instantiation, even in the face of sophisticated
serialization or reflection attacks. While this approach has yet to be widely
adopted, a single-element enum type is the best way to implement a singleton.
- 09-08-2010, 08:51 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
I don't think he's talking about using serialisation to store the singleton, but about using the nature of an enum to prevent using serialisation to bypass the standard singleton mechanism. Can't guarantee that since I don't have the book, but a "normal" singleton pattern can be bypassed via serialisation, allowing the creation of multiple instances.
In addition, look here. That is how enum constants are serialised. Which makes sense. If you want to save an load parameters into your enum you'll have to have specific code for that.
- 09-08-2010, 09:39 AM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
(De)serialization of enums is different from (de)serialization of ordinary class objects. An enum in serialized form only contains the enum name and a notion of the particular enum (class). All attributes you have added yourself are not (de)serialized. This is to preserve the singleton behaviour of a particular enum instantiation.
kind regards,
Jos
- 09-08-2010, 11:09 AM #14
Member
- Join Date
- Jul 2010
- Posts
- 22
- Rep Power
- 0
- 09-08-2010, 11:39 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
I didn't think second edition was free?
I thought only the first edition was?
- 09-08-2010, 12:57 PM #16
Member
- Join Date
- Jul 2010
- Posts
- 22
- Rep Power
- 0
- 09-08-2010, 01:37 PM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Serialization/Deserialization Error
By andrepezzo in forum Advanced JavaReplies: 2Last Post: 12-16-2008, 05:36 PM -
Serialization/Deserialization Error
By andrepezzo in forum NetworkingReplies: 0Last Post: 12-16-2008, 04:21 PM -
[SOLVED] Hacking Singleton- Multiple instance of a Singleton class
By piyu.sha in forum New To JavaReplies: 2Last Post: 10-06-2008, 09:06 PM -
Whether to make Bean Singleton or non Singleton (prototype)
By Java Tip in forum Java TipReplies: 0Last Post: 03-29-2008, 12:41 PM -
Whether to make Bean Singleton or non Singleton (prototype)
By JavaBean in forum Java TipReplies: 0Last Post: 09-26-2007, 08:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks