FileDocCategorySizeDatePackage
Example7.javaAPI DocExample3107Tue Jun 03 23:35:54 BST 1997None

Example7.java

// This example is from the book Developing Java Beans by Robert Englander. 
// Copyright (c) 1997 O'Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or implied.

// Chapter 5 -- The Example7 class and associated classes

import java.io.*;

class A implements java.io.Serializable
{
   protected int a;

   private void writeObject(ObjectOutputStream stream)
               throws java.io.IOException
   {
      System.out.println("writeObject called for class A");
      stream.defaultWriteObject();
   }
  
   private void readObject(ObjectInputStream stream)
               throws java.io.IOException
   {
      System.out.println("readObject called for class A");

      try
      {
         stream.defaultReadObject();
      }
      catch (ClassNotFoundException e)
      {
         throw new IOException();
      }
   }

   public A()
   {
   }
} 

class B extends A
{
   protected int b;

   private void writeObject(ObjectOutputStream stream)
               throws java.io.IOException
   {
      System.out.println("writeObject called for class B");
      stream.defaultWriteObject();
   }

   private void readObject(ObjectInputStream stream)
               throws java.io.IOException
   {
      System.out.println("readObject called for class B");

      try
      {
         stream.defaultReadObject();
      }
      catch (ClassNotFoundException e)
      {
         throw new IOException();
      }
   }

   public B()
   {
      super();
   }
}

class C extends B
{
   protected int c;

   private void writeObject(ObjectOutputStream stream)
               throws java.io.IOException
   {
      System.out.println("writeObject called for class C");
      stream.defaultWriteObject();
   }

   private void readObject(ObjectInputStream stream)
               throws java.io.IOException
   {
      System.out.println("readObject called for class C");

      try
      {
         stream.defaultReadObject();
      }
      catch (ClassNotFoundException e)
      {
         throw new IOException();
      }
   }

   public C()
   {
      super();
   }
}

public class Example7
{
   // the application entry point
   public static void main(String[] args)
   {
      // the byte array buffer
      byte[] buffer = null;

      // create an instance of class C
      C c = new C();
      try
      {
         ByteArrayOutputStream f = new ByteArrayOutputStream();
         ObjectOutputStream s = new ObjectOutputStream(f);
         s.writeObject(c);
         s.flush();
         buffer = f.toByteArray();
      }
      catch (Exception e)
      {
         System.out.println(e);
      }

      try
      {
         ByteArrayInputStream f = new ByteArrayInputStream(buffer);
         ObjectInputStream s = new ObjectInputStream(f);
         c = (C)s.readObject();
      }
      catch (Exception e)
      {
         System.out.println(e);
      }
   }
}