Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By pbrockway2
  • 1 Post By Norm

Thread: ObjectInputStream and the Serializable class

  1. #1
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default ObjectInputStream and the Serializable class

    Hey all

    I'm never used Serialization before so I thought I'd play around to increase my knowledge. I have managed to write objects out but have an issue when reading the objects back in.

    All the code examples I have found only write one object out and read one object back in. How do you read in multiple objects? The OIS class doesn't seem to have any methods to determine if there are more objects to read or if you have reached EOF. Am I being dense or are you supposed to just write out the List containing all your objects instead of the individual objects themselves?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I'm no expert but I have always assumed that the reader and writer have to agree on a "protocol" - ie the reader has to know what it is reading (how many things and in what order). You can serialise a list if that's appropriate in which case the reader can read the whole thing and discover how many objects there are.
    JeffGrigg likes this.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    And - from memory, since I'm on a mobile device and searching is painful - doesn't Oracle's Tutorial example serialise *several* objects which are then read back in again?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Notice that DataStreams detects an end-of-file condition by catching EOFException, instead of testing for an invalid return value. All implementations of DataInput methods use EOFException instead of return values.

    I found the above in the tutorial for DataStreams. So it looks like if you do not know beforehand how many objects to read in, you have to catch the EOF exception. Which I consider somewhat unsavoury.

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

    Default

    I don't hitnk I've ever serilized multiple objhects that weren't themselves wrapped in some other parent.
    I'm trying to think of an instance and having trouble...

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    I think pbrockway2's post#2 is how it works. Write class A, write class B, write class C and then read then back in the same order.
    JeffGrigg likes this.

Similar Threads

  1. Replies: 1
    Last Post: 07-10-2011, 04:33 PM
  2. ObjectInputStream issue
    By aianta in forum New To Java
    Replies: 8
    Last Post: 04-13-2011, 01:22 AM
  3. ObjectInputStream does not initialize
    By Singing Boyo in forum New To Java
    Replies: 1
    Last Post: 06-03-2009, 08:11 AM
  4. Reading from ObjectInputStream
    By deepthought015 in forum Networking
    Replies: 8
    Last Post: 04-28-2009, 05:57 PM
  5. Stops when opening ObjectInputStream
    By Norberhuis in forum Networking
    Replies: 4
    Last Post: 01-09-2009, 04:19 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
  •