Results 1 to 5 of 5
  1. #1
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default Singleton serialization / deserialization

    I am trying to write a class the can write a value to a file and retrieve it and utilize the readResolve() method to return one of the two singleton objects created in the singleton class itself.

    The code I have come up with is the following:
    Java Code:
    import java.io.*;
       
        public class OrientationTest {
       
            public static void main(String[] args) {
                try {
                    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Orientation.dat"));
                    out.write(2);
                    out.close();
                   
                    ObjectInputStream in = new ObjectInputStream(new FileInputStream("Orientation.dat"));
                    Orientation newOriginal = (Orientation) in.readObject();
                    in.close();
                   
                    if (newOriginal == Orientation.HORIZONTAL)
                        System.out.println("HORIZONTAL");
                    else if (newOriginal == Orientation.VERTICAL)
                        System.out.println("VERTICAL");
                    else
                        System.out.println("BAD VALUE");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }       
        }
       
        class Orientation implements Serializable {
            public static final Orientation HORIZONTAL = new Orientation(1);
            public static final Orientation VERTICAL = new Orientation(2);
            private int value;
               
            private Orientation(int v) { value = v; }
               
            protected Object readResolve() throws ObjectStreamException {
                if (value == 1) return Orientation.HORIZONTAL;
                if (value == 2) return Orientation.VERTICAL;
                return null; // this shouldn't happen
            }
        }
    I keep getting the following errors:

    java.io.OptionalDataException
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at OrientationTest.main(OrientationTest.java:12)

    Please let me know what I should be doing differently, I would really love to understand what is going on here! Thank you so much! :)

    -Derek

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    As far as I can see you're only writing a single byte 2 and you're trying to read an entire object; that fails. Read the API documentation for those streams for details.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    Derek you are doing a number of things incorrectly.

    A few of them are;
    1. You are not serializing the object of type Orientation, but while deserializing you are trying to typecast the read object in Orientation object. This is wrong. Serialize the Orientation object if you want to get Orientation object on deserialization.
    2. There is no way you can instantiate Orientation from outside Orientation class. So either make constructor public or define a static method which returns you an instance of Orientation class.


    Here is the suggested code:
    Java Code:
    public class OrientationTest {
    
    	public static void main(String[] args) {
    		try {
    			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Orientation.dat"));
    			
    			Orientation or = Orientation.getInstnce(1);
    			out.writeObject(or);
    			out.close();
    
    			ObjectInputStream in = new ObjectInputStream(new FileInputStream("Orientation.dat"));
    			Orientation newOriginal = (Orientation) in.readObject();
    			in.close();
    
    			if (newOriginal == Orientation.HORIZONTAL)
    				System.out.println("HORIZONTAL");
    			else if (newOriginal == Orientation.VERTICAL)
    				System.out.println("VERTICAL");
    			else
    				System.out.println("BAD VALUE");
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}       
    }
    
    class Orientation implements Serializable {
    	public static final Orientation HORIZONTAL = new Orientation(1);
    	public static final Orientation VERTICAL = new Orientation(2);
    	private int value;
    
    	private Orientation(int v) { value = v; }
    	
    	public static Orientation getInstnce(int v){
    		if(v == 1){
    			return HORIZONTAL;
    		}
    		else if(v == 2){
    			return VERTICAL;
    		}
    		else {
    			return null;
    		}
    	}
    
    	protected Object readResolve() throws ObjectStreamException {
    		if (value == 1) return Orientation.HORIZONTAL;
    		if (value == 2) return Orientation.VERTICAL;
    		return null; // this shouldn't happen
    	}
    }
    Hope that helps

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    I would use a enum class :p
    Then you do not have to pay attention of the hacks (like your readResolve method or the reflection hack :D) and the code is minimal and readable

    Java Code:
    public class OrientationTest {
    
    	public static void main(String[] args) {
    		try {
    			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Orientation.dat"));
    			
    			out.writeObject(Orientation.HORIZONTAL);
    			out.close();
    
    			ObjectInputStream in = new ObjectInputStream(new FileInputStream("Orientation.dat"));
    			Orientation newOriginal = (Orientation) in.readObject();
    			in.close();
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}       
    }
    
    enum Orientation{
    	HORIZONTAL, VERTICAL;
    }

  5. #5
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    Thanks for all of your input! You guys were right, I could either use the enums, or if I wanted to stick to the approach I was using I had to do a writeObject, not a regular write. That was my big mistake and I thank you for pointing that out. Thanks a lot guys!

    -Derek

Similar Threads

  1. Enum singleton serialization and deserialization
    By bassfero in forum Advanced Java
    Replies: 16
    Last Post: 09-08-2010, 02:37 PM
  2. Serialization/Deserialization Error
    By andrepezzo in forum Advanced Java
    Replies: 2
    Last Post: 12-16-2008, 06:36 PM
  3. Serialization/Deserialization Error
    By andrepezzo in forum Networking
    Replies: 0
    Last Post: 12-16-2008, 05:21 PM
  4. Replies: 2
    Last Post: 10-06-2008, 10:06 PM
  5. Replies: 0
    Last Post: 09-26-2007, 09: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
  •