Results 1 to 17 of 17
  1. #1
    bassfero is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default 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:
    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;
       }
    }
    All I need is to know how to write save and load function.

    thanks...
    Last edited by bassfero; 09-07-2010 at 10:15 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    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.

  3. #3
    bassfero is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    this singleton supose to be a configuration, so i want to be able to save it in file

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    See my edits...

  5. #5
    bassfero is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    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.
    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

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    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.

  7. #7
    bassfero is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    ...then assign the individual attributes of this temporary Parameters object to this
    that is exactly what I am trying to avoid

    Quote Originally Posted by Tolls View Post
    I assume from your description then that the above isn't your real code.
    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

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    Does the book serialise them?

    I ask because I've just done what you're trying to do and this:
    Java Code:
    TestEnum temp = (TestEnum)ois.readObject();
    ETA: note ois is an ObjectInputStream.

    throws this:
    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
    which is why I talked about it throwing a wobbly.

  9. #9
    bassfero is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Does the book serialise them?

    I ask because I've just done what you're trying...
    All I was able to do was
    Java Code:
    oos.writeObject(INSTANCE);
    but as I mentioned, there were no attributes values in file.

    Maybe I should write to Joshua Bloch :D

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    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?

  11. #11
    bassfero is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    But does he serialise them in the book?
    This is quote from book

    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.
    There are some sample codes in the book but not about serialization.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    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.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bassfero View Post
    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.
    (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

  14. #14
    bassfero is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    I don't think he's talking about using serialisation to store the singleton...
    possible

    Quote Originally Posted by Tolls View Post
    I don't have the book...
    just google for it, this topic is on page 17-19

    Quote Originally Posted by Tolls View Post
    ...
    In addition, look here...
    I read this before posting on this forum.



    Probably I did't understand item in book.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    I didn't think second edition was free?
    I thought only the first edition was?

  16. #16
    bassfero is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    I didn't think second edition was free?
    I thought only the first edition was?
    I download first 2 chapters of a book for free(which include also this topic), in application for my iPod, but I am sure that is possible to find a full PDF for free (legality depends on the law of your country).

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

Similar Threads

  1. Serialization/Deserialization Error
    By andrepezzo in forum Advanced Java
    Replies: 2
    Last Post: 12-16-2008, 05:36 PM
  2. Serialization/Deserialization Error
    By andrepezzo in forum Networking
    Replies: 0
    Last Post: 12-16-2008, 04:21 PM
  3. Replies: 2
    Last Post: 10-06-2008, 09:06 PM
  4. Replies: 0
    Last Post: 03-29-2008, 12:41 PM
  5. Replies: 0
    Last Post: 09-26-2007, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •