This discussion is archived
3 Replies Latest reply: Jul 15, 2011 3:14 AM by 809490 RSS

read objects from ObjectInputStream

809490 Newbie
Currently Being Moderated
Hello

I have a servlet which writes Employee objects to an ObjectOutputStream. The Employee object implements the Serialization interface.
ObjectOutputStream outstream = new ObjectOutputStream(response.getOutputStream());
while(rslt.next())
{
     Employee emp = new Employee(rslt.getString(2), rslt.getInt(1), rslt.getInt(5), rslt.getInt(4), rslt.getString(6), new Date(), 
               rslt.getString(7), rslt.getString(8), new Date(), rslt.getString(9), rslt.getString(10), rslt.getString(11), "", rslt.getInt(12));
     outstream.writeObject(emp);               
}
When the client reads the request,
try 
{
     HttpResponse response = this.httpClient.execute(httpget);
     HttpEntity entity = response.getEntity();
     if (entity != null) 
     {
         InputStream instream = entity.getContent();
         ObjectInputStream oinstream = new ObjectInputStream(instream);
         try 
         {
              Object obj = null;
              while((obj = oinstream.readObject()) != null)
              {
                 Employee emp = null;
                   if(obj instanceof Employee)
                   {
                        emp = (Employee) oinstream.readObject();
                   }
              }
                         
          } 
             catch (ClassNotFoundException e) 
          {
          e.printStackTrace();
          }
          catch(EOFException eof)
          {
              eof.printStackTrace();
          }
          finally
          {
               try
              {
                   if(oinstream != null)
                       oinstream.close();
                   if(instream != null)
                        instream.close();
              }
              catch(IOException e)
              {
                   e.printStackTrace();
              }
         }
                  
}
just every second Employee objects gets reconstructed from the inputstream.

Example:
It gets written by the servet
employee1
employee2
employee3
employee4
employee5
employee6
employee7
employee8

and the client just gets
employee2
employee4
employee6
employee8

How can i read the ObjectOutputStream properly?

regards,
Chang
  • 1. Re: read objects from ObjectInputStream
    EJP Guru
    Currently Being Moderated
    That's because you're throwing every second one away when reading.
                  while((obj = oinstream.readObject()) != null)
                  {
                     Employee emp = null;
                       if(obj instanceof Employee)
                       {
                            emp = (Employee) oinstream.readObject();
                       }
                  }
    See? You threw away 'obj' and read a second object. So you are missing out on all the odd-numbered objects.

    All you needed here was emp = (Employee)obj;

    But there is another problem lurking. readObject() only returns null if you wrote a null. If that loop is intended to work until EOF, it won't. readObject() throws EOFException when it reaches the end of the object stream ... rather than returning null. So you need to treat EOFException as expected, not unexpected, and get rid of the null test completely.
  • 2. Re: read objects from ObjectInputStream
    809490 Newbie
    Currently Being Moderated
    Ah ok thanks for the help.
    Now i have all Employee objects. The problem with the EOFException i wanted to avoid by setting a header field in the response which has the number of employees written to the objectoutputstream.
    ObjectOutputStream outstream = new ObjectOutputStream(response.getOutputStream());
    int counter = 0;
    while(rslt.next())
    {
         counter++;
         Employee emp = new Employee(rslt.getString(2), rslt.getInt(1), rslt.getInt(5), rslt.getInt(4), rslt.getString(6), new Date(), 
                   rslt.getString(7), rslt.getString(8), new Date(), rslt.getString(9), rslt.getString(10), rslt.getString(11), "", rslt.getInt(12));
         outstream.writeObject(emp);
              
    }
         
    response.addIntHeader("empnum", counter);
    But when I read the response header fields on the client side there is no response header called 'empnum'.
    HttpResponse response = this.httpClient.execute(httpget);
    Header[] headers = response.getAllHeaders();
    for(int i = 0; i < headers.length; i++)
         System.out.println("header: " + headers.getName() + " -> " + headers[i].getValue());


    the output is:
    header: Server -> Apache-Coyote/1.1
    header: Transfer-Encoding -> chunked
    header: Date -> Fri, 15 Jul 2011 10:07:47 GMT
  • 3. Re: read objects from ObjectInputStream
    EJP Guru
    Currently Being Moderated
    There's no 'problem with the EOFException'. Just catch it and stop reading. And you can't add a header after you've written the output. You're digging a hole here. Stop digging. Just catch the exception.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points