Hi Everyone:

Any ideas as to why the following code doesn't work?  It generates the following 
exception:
I've concluded that the writeObject with the GZIPOutputStream in the mix doesn't 
generate
the byte array that should be generated.  Thus, an exception is thrown when the byte 
array is
decompressed.  I'm hoping to generate an array of bytes containing the compressed 
object.
Using jdk1.2.2.

Per my reading of O'Reilly's Java Examples in a Nutshell pp 266-7 and
Sun's  http://forum.java.sun.com/forum?[EMAIL PROTECTED]^[email protected]/0

the code should work.

the Exception:

Exception is Caught EOFException while reading the stream header
java.io.StreamCorruptedException: Caught EOFException while reading the stream header
        at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:727)
        at java.io.ObjectInputStream.<init>(ObjectInputStream.java:165)
        at examples.ejb.basic.rc.TestIt.main(TestIt.java, Compiled Code)

the code:

import java.util.*;
import java.util.zip.*;
import java.io.*;

 public class TestIt {

  public static void main(String[] args)
  {
    try
    {
          Vector names = new Vector();
          String fred = "fred";
          String skip = "skip";
          String lisa = "lisa";

          names.addElement(fred);
          names.addElement(skip);
          names.addElement(lisa);

          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          GZIPOutputStream gos       = new GZIPOutputStream( baos );
          ObjectOutputStream oos     = new ObjectOutputStream( gos );
          oos.writeObject( names );
          oos.flush();

          //This is required
          byte[] data = baos.toByteArray();
          //This is required

          oos.close();

          ByteArrayInputStream bin = new ByteArrayInputStream( data );
          GZIPInputStream gin      = new GZIPInputStream( bin );
          ObjectInputStream ois    = new ObjectInputStream( gin );
          Vector v = (Vector)ois.readObject();
          ois.close();

          Enumeration enum = v.elements();
          String stuff;
          while ( enum.hasMoreElements() )
          {
            stuff = (String)enum.nextElement();
            System.out.println("Name is " + stuff);
          }
    }
    catch( Exception e )
    {
        System.out.println( "Exception is " + e.getMessage() );
        e.printStackTrace();
    }
  }
}


Thanks if you can take a quick look.

Tom Rochelle

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to