Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2006

Tip: Looking for answers? Try searching our database.

ObjectInputStream, how to find the end of the stream?

Thread view: 
Krystian - 26 Jan 2006 22:15 GMT
Hi

I've got a table model and i am reading objects from a prepared file
into it.
Tht thing is, i can't determine the end of the file when doing so.
All my efforts always end at having IOException thrown.

FileInputStream file = new FileInputStream("base.tmp");
ObjectInputStream read = new ObjectInputStream(file);
Object row;
while ((row = read.readObject()) != null) {
model.addRow(row);  //it's my table model i am adding rows into it
}
read.close();

This is how it looks like now.. I was trying to do it like with
StringBuffers [i know i'm sad ;> ] but still after reading the last
object it reads another and throws IOException :/

Any advices?
Knute Johnson - 27 Jan 2006 00:37 GMT
> Hi
>
[quoted text clipped - 16 lines]
>
> Any advices?

This throws an EOFException when reading the second string.  All of the
other methods readBoolean(), readShort(), readInt()... all throw
EOFException according to the docs.  Why that isn't specified for
readObject() I don't know.

import java.io.*;

public class test {
    public static void main(String[] args) throws Exception {
        ObjectOutputStream oos =
         new ObjectOutputStream(new FileOutputStream("file.x"));
        String str = "this is a test!";
        oos.writeObject(str);
        oos.close();

        ObjectInputStream ois =
         new ObjectInputStream(new FileInputStream("file.x"));
        String str2 = (String)ois.readObject();
        System.out.println(str2);
        String str3 = (String)ois.readObject();
        ois.close();
    }
}

Signature

Knute Johnson
email s/nospam/knute/

Krystian - 27 Jan 2006 08:01 GMT
Hmmm

It is specified for readObject that it will throw an exception. I only
want to know how to create a loop reading those Objects without reading
more then there is in file. I just don't know what statement should i
put in the loop. I really don't want that exception to be thrown.

All examples or descriptions of object input stream read only one or
few objects ant then closes the stream. I don't know how many of them
there are so i have to make a loop.

Bes regards
Gordon Beaton - 27 Jan 2006 09:18 GMT
> It is specified for readObject that it will throw an exception. I
> only want to know how to create a loop reading those Objects without
> reading more then there is in file. I just don't know what statement
> should i put in the loop. I really don't want that exception to be
> thrown.

What is your aversion to the exception, when it solves your problem?

 try {
   while ((o = ois.readObject())) {
     /* more stuff */
   }
 }
 catch (EOFException e) {
   System.out.println("Finished reading objects");
 }
 finally {
   ois.close();
 }

Alternatives include writing a special sentinal object last in the
stream or using some other mechanism to communicate the number of
objects to expect, but to me these are superfluous.

You still need to be able to handle that exception (i.e. you may never
reach the sentinal object or the expected count), and when it occurs
you have reached EOF whether you like it or not.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Krystian - 27 Jan 2006 10:49 GMT
> You still need to be able to handle that exception (i.e. you may never
> reach the sentinal object or the expected count), and when it occurs
> you have reached EOF whether you like it or not.

And this is why i am seeking for a different way to solve this then by
exeption as it can happen when ie. file is corrupted and then it would
only tell me that everything is done.

I thought about adding something at the end of the file but it was
going to be my final way of solving this.

As there is no other... i guess this must suffice.

Thank you for your help ;)

Best regards
Nigel Wade - 30 Jan 2006 11:31 GMT
>> You still need to be able to handle that exception (i.e. you may never
>> reach the sentinal object or the expected count), and when it occurs
[quoted text clipped - 3 lines]
> exeption as it can happen when ie. file is corrupted and then it would
> only tell me that everything is done.

No, it would not tell you that everything was done. You will get a different
exception if the file is corrupted. You need to handle them differently because
they are telling you different things. If you receive an EOFException it means
that you have successfully read all the objects from the stream, any other
IOException indicates that you have not successfully read the all the objects.

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555

Lothar Kimmeringer - 27 Jan 2006 19:01 GMT
> FileInputStream file = new FileInputStream("base.tmp");
> ObjectInputStream read = new ObjectInputStream(file);
[quoted text clipped - 3 lines]
> }
> read.close();

When serializing do the following:

for (int i = 0; i < model.getNumberOfRows(); i++){
   write.writeObject(model.getRow(i);
}
write.writeObject(null);

Then your code should work without change. Alternatively
you can do the following:

write.writeInt(model.getNumberOfRows()){
for (int i = 0; i < model.getNumberOfRows(); i++){
   write.writeObject(model.getRow(i);
}

and when deserializing:

int max = read.readInt();
for (int i = 0; i < max; i++){
   model.addRow(read.readObject());
}

Regards, Lothar
Signature

Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
              PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                questions!



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.