I am currently developing a client/server application. The application server is Glassfish v3, the communication is by remote EJBs. The main problem is the heavy network bandwidth use when serializing objects graphs. For example in the next method:

```
@Stateless
public class MyEJB extends MyRemoteInterface {

   @Override
   public PurchaseOrder savePurchaseOrder( PurchaseOrder po ) { ... }

}

```

this method, when invoked remotely, will receive a PurchesOrder instance which is a object graph that when is transferred over the network it will take a lot of KB, the same when it returns.

I have manage this, for now, changing the prototype of the method like this:

```
...

   @Override
   public byte[] savePurchaseOrder( byte[] po ) { ... }

...

```

I manually de/compress and de/serialize the PurchaseOrder instance before and after transferring it over the network. But I loose type safe methods and it gets ugly.

Is there a way to use java custom serialization for compressing the output stream in the default serialization process? for example:

```
    @Entity
    public class PurchaseOrder implements Serializable {

       private void writeObject(ObjectOutputStream oos) throws IOException {
           // default serialization 
           oos.defaultWriteObject();

           // COMPRESS STREAM HERE (zip or gzip)
       }

       private void readObject(ObjectInputStream ois) 
           throws ClassNotFoundException, IOException {

           // DECOMPRES STREAM HERE 

           // default deserialization
           ois.defaultReadObject();
       }

    }

```

I need the code of "DE/COMPRESS STREAM HERE" part, or another good idea.

Thank you in advance for your advice.

Xavier.