home *** CD-ROM | disk | FTP | other *** search
/ Internet News 1999 October / INEWS_10_CD.ISO / pc / jdk / jdk1.2.2 / docs / guide / serialization / examples / externpers / ExternExampleOriginalClass.java < prev    next >
Encoding:
Java Source  |  1999-09-19  |  5.7 KB  |  200 lines

  1. /*
  2.  * @(#)ExternExampleOriginalClass.java    1.3 98/10/01        
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  * 
  20.  */
  21.  
  22. import java.io.*;
  23. import java.util.*;
  24.  
  25. /** 
  26.  * This example shows how to evolve a persistent format using the
  27.  * Externalizable interface. A class using the Externalizable interface
  28.  * is responsible for saving its own state, for saving the state of its 
  29.  * supertype, and for versioning including skipping over data. 
  30.  * This example specifically deals with 
  31.  * versioning - (To see how to save the state of the supertype, which can
  32.  * Externalizable or not, see relevant examples)... 
  33.  *
  34.  * How to Run:
  35.  * 
  36.  * Compile Original Class: 
  37.  *         javac ExternExampleOriginalClass.java
  38.  * Run Original Class with serialization flag: 
  39.  *         java ExternExampleOriginalClass -s
  40.  * Compile Evolved Class:
  41.  *         javac ExternExampleEvolvedClass.java
  42.  * Run Evolved Class with deserialization flag:
  43.  *         java ExternExampleEvolvedClass -d
  44.  *
  45.  * This tests compatibility in one direction. Do the same in other
  46.  * direction to see  bidirectional compatibility.
  47.  *
  48.  * Compiled and Tested with JDK1.1.4 & JDK1.2
  49.  */
  50.  
  51. public class ExternExampleOriginalClass {
  52.  
  53.     /** 
  54.      *  There are two options: either a user can serialize an object or
  55.      *  deserialize it. (using the -s or -d flag). These options allow
  56.      *  for the demonstration of bidirection readability and writeability
  57.      *  between the original and the evolved class. In other words,
  58.      *  one can serialize an object here and deserialize it with the evolved
  59.      *  class or vice versa.
  60.      */
  61.     public static void main(String args[]) {  
  62.     
  63.     boolean serialize = false;
  64.     boolean deserialize = false;
  65.     
  66.     ExternVersioningClass wobj = new ExternVersioningClass(2, "oldclass");
  67.     ExternVersioningClass robj = null;
  68.     /* 
  69.      * see if we are serializing or deserializing.
  70.      * The ability to deserialize or serialize allows
  71.      * us to see the bidirectional readability and writeability
  72.      */
  73.     if (args.length == 1) {
  74.         if (args[0].equals("-d")) {
  75.         deserialize = true;
  76.         } else if (args[0].equals("-s")) {
  77.         serialize = true;
  78.         } else {
  79.                 usage();
  80.                 System.exit(0);
  81.         }
  82.     } else {
  83.         usage();
  84.         System.exit(0);
  85.      }
  86.     
  87.     /* 
  88.      * serialize, if that's the chosen option
  89.      */
  90.     if (serialize) {
  91.         try {
  92.         FileOutputStream fo = new FileOutputStream("evolve.tmp");
  93.         ObjectOutputStream so = new ObjectOutputStream(fo);
  94.         so.writeObject(wobj);
  95.         so.close();
  96.         } catch (Exception e) {
  97.         e.printStackTrace();
  98.         System.exit(1);
  99.         }
  100.     }
  101.     
  102.     /* 
  103.      * deserialize, if that's the chosen option
  104.      */
  105.     if (deserialize) {
  106.         try {
  107.         FileInputStream fi = new FileInputStream("evolve.tmp");
  108.         ObjectInputStream si = new ObjectInputStream(fi);
  109.         robj = (ExternVersioningClass) si.readObject();
  110.         } catch (Exception e) {
  111.         e.printStackTrace();
  112.         System.exit(1);
  113.         }
  114.     }
  115.     }
  116.     /** 
  117.      * Prints out the usage
  118.      */
  119.     static void usage() {
  120.         System.out.println("Usage:");
  121.         System.out.println("      -s (in order to serialize)");
  122.         System.out.println("      -d (in order to deserialize)");
  123.     }
  124. }
  125.  
  126. /*
  127.  * The original ExternVersioningClass
  128.  */
  129. class ExternVersioningClass implements Externalizable  {
  130.  
  131.   // some data that we will explicitly save
  132.   int dimension;
  133.   int array[];
  134.   String name;
  135.  
  136.   public static final int version = 1;
  137.  
  138.   // ***need to have a public-no-arg constructor*** with Exernalizable
  139.   public ExternVersioningClass() {}
  140.  
  141.   ExternVersioningClass(int dim, String n) {
  142.     // initialize 
  143.     dimension = dim;
  144.     array = new int[dimension];
  145.     name = n;
  146.   }
  147.  
  148.     /** 
  149.      * Mandatory writeExternal method. It writes out the class version
  150.      * number and then writes out the fields.
  151.      * 
  152.      * @serialData Writes out an int "class version number". Next, the
  153.      *             dimension field is written as an int. Followed by
  154.      *             the array field written as an object. Finally, the
  155.      *             name field is written as an object.
  156.      */
  157.     public void writeExternal(ObjectOutput out) throws IOException {
  158.     
  159.     // we first write out the class version number (ie. # 1)
  160.     out.writeInt(version);
  161.     
  162.     // now we write out the data
  163.     out.writeInt(dimension);
  164.     out.writeObject(array);
  165.     out.writeObject(name);
  166.     }
  167.  
  168.     /** 
  169.      * Mandatory readExternal method. Just reads in the fields for this
  170.      * class. Assumption is that later version of the class make only
  171.      * compatible changes and that their additional data is appended.
  172.      *
  173.      * @serialData Read a class version number as an int, the dimension field 
  174.      *             as an int, array field as an Object and name field as an 
  175.      *             object.
  176.      */
  177.     public void readExternal(ObjectInput in) throws IOException,
  178.     java.lang.ClassNotFoundException {
  179.     
  180.     int version = in.readInt();
  181.     System.out.println("Reading an object written by Version #: " + version);
  182.     dimension = in.readInt();
  183.     
  184.     // need to allocate memory for the array we will read in
  185.     array = (int[]) in.readObject();
  186.     name = (String) in.readObject();
  187.     }
  188. }
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.