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

  1. /*
  2.  *
  3.  * @(#)OriginalClass.java    1.4 98/10/01        
  4.  *
  5.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  6.  *
  7.  * This software is the confidential and proprietary information of Sun
  8.  * Microsystems, Inc. ("Confidential Information").  You shall not
  9.  * disclose such Confidential Information and shall use it only in
  10.  * accordance with the terms of the license agreement you entered into
  11.  * with Sun.
  12.  *
  13.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  14.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  15.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  16.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  17.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  18.  * THIS SOFTWARE OR ITS DERIVATIVES.
  19.  *
  20.  * 
  21.  */
  22.  
  23. import java.io.*;
  24. import java.awt.*;
  25.  
  26. /**
  27.  *
  28.  * This example shows how to use the Serializable Fields API with 
  29.  * Serialization, demonstrating that the class can define fields 
  30.  * other than those already in the class to be serializable. This 
  31.  * differs from just rewriting the writeObject method to customize 
  32.  * the data format (see the Custom Data Format example) because, 
  33.  * in this example, versioning support still holds.
  34.  *
  35.  * Using the Serializable Fields API, this example specifically 
  36.  * changes the internal representation of a rectangle from 
  37.  * x1,y1,x2,y2 implementation (see Original Class) to Point(x1,y1), 
  38.  * Point(x2,y2) (see Evolved Class) while the external representation 
  39.  * still remains x1, y1, x2, y2. This ensures bidirectional compatibility 
  40.  * between the original and evolved representations. 
  41.  *
  42.  * The original rectangle class (in this file) consists of four integers
  43.  * (x1, y1, x2, y2). Instead of four integers, the evolved rectangle class 
  44.  * (in EvolvedClass.java) has two Points as fields. In order for this evolved
  45.  * class to fulfill its contract with the original class, the evolved class 
  46.  * saves its fields as four integers (x1, y1, x2, y2) instead of two points. 
  47.  * By doing this, the evolved class ensures bidirectional compatibility 
  48.  * with the original class.
  49.  *
  50.  * How to Run:
  51.  *           
  52.  * Compile Original Class with JDK1.2: 
  53.  *         javac OriginalClass.java
  54.  * Run Original Class with serialization flag: 
  55.  *         java OriginalClass -s
  56.  * Compile Evolved Class:
  57.  *         javac EvolvedClass.java
  58.  * Run Evolved Class with deserialization flag:
  59.  *         java EvolvedClass -d
  60.  *
  61.  * This tests compatibility in one direction. Do the same in other
  62.  * direction to see  bidirectional compatibility.
  63.  *
  64.  *  Compiled and Tested with JDK1.2
  65.  */
  66.  
  67. public class OriginalClass {
  68.  
  69.     /**  
  70.      *  There are two options: either a user can serialize an object or
  71.      *  deserialize it. (using the -s or -d flag). These options allow
  72.      *  for the demonstration of bidirection readability and writeability
  73.      *  between the original and the evolved class. In other words,
  74.      *  one can serialize an object here and deserialize it with the evolved
  75.      *  class or vice versa.
  76.      */
  77.     public static void main(String args[]) {
  78.  
  79.         ARectangle orgClass = new ARectangle(0, 0, 2, 2);
  80.         ARectangle newClass = null;
  81.  
  82.         boolean serialize = false;
  83.         boolean deserialize = false;
  84.  
  85.         /* 
  86.      * see if we are serializing or deserializing.
  87.          * The ability to deserialize or serialize allows
  88.          * us to see the bidirectional readability and writeability
  89.          */
  90.         if (args.length == 1) {
  91.             if (args[0].equals("-d")) {
  92.         deserialize = true;
  93.             } else if (args[0].equals("-s")) {
  94.                 serialize = true;
  95.             } else {
  96.                 usage();
  97.                 System.exit(0);
  98.             }
  99.         } else {
  100.             usage();
  101.             System.exit(0);
  102.         }
  103.  
  104.         /* 
  105.      * Serialize the original class if that's the option chosen
  106.          */
  107.         if (serialize) {
  108.             try {
  109.                 FileOutputStream fo = new FileOutputStream("evolve.tmp");
  110.                 ObjectOutputStream so = new ObjectOutputStream(fo);
  111.                 so.writeObject(orgClass);
  112.                 so.flush();
  113.             } catch (Exception e) {
  114.                 System.out.println(e);
  115.                 System.exit(1);
  116.         }
  117.         }
  118.  
  119.         /*
  120.      * Deserialize, if that's the option chosen and print the name
  121.          * of the object, which will allow us to see who serialized the
  122.          * object, the original class or the evolved class file
  123.          */
  124.         if (deserialize) {
  125.             try {
  126.                 FileInputStream fi = new FileInputStream("evolve.tmp");
  127.                 ObjectInputStream si = new ObjectInputStream(fi);
  128.                 newClass = (ARectangle) si.readObject();
  129.             } catch (Exception e) {
  130.                 System.out.println(e);
  131.                 System.exit(1);
  132.             }
  133.             System.out.println("Now printing deserialized object: ");
  134.             System.out.println();
  135.         System.out.println(newClass);
  136.         }
  137.     }
  138.     
  139.     /** 
  140.      * Prints out the usage
  141.      */
  142.     static void usage() {
  143.         System.out.println("Usage:");
  144.         System.out.println("      -s (in order to serialize)");
  145.         System.out.println("      -d (in order to deserialize)");
  146.     }
  147. }
  148.  
  149. /**
  150.  * The original Rectangle Class. Simply consists of 4 integers
  151.  * representing the two diagonal points of a rectangle. There is
  152.  * no writeObject or readObject written, so that the external and the
  153.  * the internal representations are the same (4 integers)
  154.  */
  155. class ARectangle implements java.io.Serializable {
  156.  
  157.     /**
  158.      * X-coordinate of point 1 of two diagonal points of rectange.
  159.      * @serial 
  160.      */
  161.     int x1;
  162.  
  163.     /**
  164.      * Y-coordinate of point 1 of two diagonal points of rectange.
  165.      * @serial 
  166.      */
  167.     int y1;
  168.  
  169.     /**
  170.      * X-coordinate of point 2 of two diagonal points of rectange.
  171.      * @serial 
  172.      */
  173.     int x2;
  174.  
  175.     /**
  176.      * Y-coordinate of point 2 of two diagonal points of rectange.
  177.      * @serial 
  178.      */
  179.     int y2;
  180.  
  181.     ARectangle(int xone, int yone, int xtwo, int ytwo) {
  182.      x1 = xone;
  183.     y1 = yone;
  184.     x2 = xtwo;
  185.     y2 = ytwo;
  186.     }
  187.  
  188.     public String toString() {
  189.     return("x1: " + x1 + "\ny1: " + y1 + "\nx2: " + x2 + "\ny2: " + y2);
  190.     }
  191. }
  192.  
  193.