Solved

ObjectInputStream getting EOFException

Posted on 2006-11-06
3
222 Views
Last Modified: 2010-03-31
What I'm attempting to do is read in a serialized object (in this case hashtable) from a file and add a new element to it and rewrite it back to the file with the updated hashtable, then later I will go back through and remove the elements from the hashtable in the temporary file when i don't need them in processing anymore.   I'm getting an EOFException when trying to read in the object.

Note: The first time it hits the EOFException, i was assuming that the hashtable had not been added to the file at that time so I add a new one.  But it seems to hit EOFException every time without pulling the hashtable, so in other words it's like it's writing a new hashtable everytime it's run.

FileInputStream filein = null;
FileOutputStream fileout = null;
ObjectOutputStream objout = null;
ObjectInputStream objin = null;
SecretKey key = null;

try {
         response.setContentType("text/html");
         out = response.getWriter();

         // create encryption key
         key = KeyGenerator.getInstance("DES").generateKey();
       
         // read in key file
         filein = new FileInputStream(filepath);
         fileout = new FileOutputStream(filepath);
         objout = new ObjectOutputStream(fileout);
         objin = new ObjectInputStream(filein);
       
         // read in hashtable of member ids and keys
         mem_keys = (Hashtable)objin.readObject();        
         mem_keys.put(member.member_id, key); // add the new member to the hashtable
         objout.writeObject(mem_keys); // write back out

         // init the DES encryptor
         DesEncrypter des = new DesEncrypter(key);
         if(password.compareTo(confirm) == 0) {
            // encrypt password using SecretKey DES encryptor
            member.password = des.encrypt(password);
         }
}
catch(EOFException e) {
        // to print the error to screen
        //out.println("<PRE>" + e.getMessage() + "<BR>");
        //e.printStackTrace(out);

        // since we are keeping only one hashtable in the file
        // and it's end of file, then no objects where found so create new hashtable to add to
        mem_keys = new Hashtable();
        mem_keys.put("" + member.member_id, key);
        try {
             objout.writeObject(mem_keys.clone());
        }
        catch(Exception ex) {
            out.println("<PRE>" + ex.getMessage() + "<BR>");
            ex.printStackTrace(out);
        }
}
catch(IOException e) {
        out.println("<PRE>" + e.getMessage() + "<BR>");
        e.printStackTrace(out);
}
catch(Exception e) {
        out.println("<PRE>" + e.getMessage() + "<BR>");
        e.printStackTrace(out);
}
     
// clean up
try {
        objin.close();
        filein.close();
        objout.flush();
        objout.close();
        fileout.close();
}
catch(Exception e) {
        out.println("<PRE>" + e.getMessage() + "<BR>");
        e.printStackTrace(out);
}

Any thoughts?  thanks
0
Comment
Question by:tbboyett
  • 3
3 Comments
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 500 total points
ID: 17886614
Since your code:

         filein = new FileInputStream(filepath);
         fileout = new FileOutputStream(filepath);

filepath are the same, therefore, they are pointing to the same file and the file is re-created to be empty every time before you read in object.
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 17886617
You should move the following lines:

         fileout = new FileOutputStream(filepath);
         objout = new ObjectOutputStream(fileout);

after

mem_keys = (Hashtable)objin.readObject();  
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 17886625
A better way is :

try {
         response.setContentType("text/html");
         out = response.getWriter();

         // create encryption key
         key = KeyGenerator.getInstance("DES").generateKey();
       
         // read in key file
         filein = new FileInputStream(filepath);
         objin = new ObjectInputStream(filein);
       
         // read in hashtable of member ids and keys
         mem_keys = (Hashtable)objin.readObject();        
         mem_keys.put(member.member_id, key); // add the new member to the hashtable

        objin.close();
        filein.close();

         fileout = new FileOutputStream(filepath);
         objout = new ObjectOutputStream(fileout);
         objout.writeObject(mem_keys); // write back out

        objout.flush();
        objout.close();
        fileout.close();

         // init the DES encryptor
         DesEncrypter des = new DesEncrypter(key);
         if(password.compareTo(confirm) == 0) {
            // encrypt password using SecretKey DES encryptor
            member.password = des.encrypt(password);
         }
}
catch(EOFException e) {
        // to print the error to screen
        //out.println("<PRE>" + e.getMessage() + "<BR>");
        //e.printStackTrace(out);

        // since we are keeping only one hashtable in the file
        // and it's end of file, then no objects where found so create new hashtable to add to
        mem_keys = new Hashtable();
        mem_keys.put("" + member.member_id, key);
        try {
             objout.writeObject(mem_keys.clone());
        }
        catch(Exception ex) {
            out.println("<PRE>" + ex.getMessage() + "<BR>");
            ex.printStackTrace(out);
        }
}
catch(IOException e) {
        out.println("<PRE>" + e.getMessage() + "<BR>");
        e.printStackTrace(out);
}
catch(Exception e) {
        out.println("<PRE>" + e.getMessage() + "<BR>");
        e.printStackTrace(out);
}
     
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 102
Eclipse Neon and jdk 1.8.0 11 138
VB Script to add site to Java Exception List 4 53
java stored proc example 9 22
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now