test/java/io/Serializable/evolution/RenamePackage/install/SerialDriver.java

Print this page
rev 3565 : 7022624: use try-with-resources in java.io tests
Reviewed-by: XXX


  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  *
  26  * @bug 4087295
  27  * @build install/SerialDriver.java test/SerialDriver.java extension/ExtendedObjectInputStream.java
  28  * @summary Enable resolveClass() to accommodate package renaming.
  29  * This fix enables one to implement a resolveClass method that maps a
  30  * Serialiazable class within a serialization stream to the same class
  31  * in a different package within the JVM runtime. See run shell script
  32  * for instructions on how to run this test.
  33  */
  34 
  35 package install;
  36 
  37 import java.io.*;
  38 import extension.ExtendedObjectInputStream;
  39 
  40 public class SerialDriver implements Serializable {
  41     private static final long serialVersionUID = 1L;
  42 
  43     String name;
  44     SerialDriver next;
  45     transient Object objarray[];
  46 
  47     public SerialDriver() {
  48         name = "<terminator>";
  49         next = null;
  50     }


  69          * The ability to deserialize or serialize allows
  70          * us to see the bidirectional readability and writeability
  71          */
  72         if (args.length == 1) {
  73             if (args[0].equals("-d")) {
  74                 deserialize = true;
  75             } else if (args[0].equals("-s")) {
  76                 serialize = true;
  77             } else {
  78                 usage();
  79                 throw new Exception("incorrect command line arguments");
  80             }
  81         } else {
  82             usage();
  83             throw new Exception("incorrect command line arguments");
  84         }
  85 
  86         File f = new File("stream.ser");
  87         if (serialize) {
  88             // Serialize the subclass
  89             try {
  90                 FileOutputStream fo = new FileOutputStream(f);
  91                 ObjectOutputStream so = new ObjectOutputStream(fo);
  92                 so.writeObject(obj);
  93                 /* Skip arrays since they do not work with rename yet.
  94                    The serialVersionUID changes due to the name change
  95                    and there is no way to set the serialVersionUID for an
  96                    array. */
  97                 so.writeObject(array);
  98                 so.flush();
  99             } catch (Exception e) {
 100                 System.out.println(e);
 101                 throw e;
 102             }
 103         }
 104         if (deserialize) {
 105             // Deserialize the subclass
 106             try {
 107                 FileInputStream fi = new FileInputStream(f);
 108                 ExtendedObjectInputStream si =
 109                     new ExtendedObjectInputStream(fi);
 110                 si.addRenamedClassName("test.SerialDriver", "install.SerialDriver");
 111                 si.addRenamedClassName("[Ltest.SerialDriver;",
 112                                        "[Linstall.SerialDriver");
 113                 obj = (SerialDriver) si.readObject();
 114                 array = (SerialDriver[]) si.readObject();
 115                 si.close();
 116             } catch (Exception e) {
 117                 System.out.println(e);
 118                 throw e;
 119             }
 120             System.out.println();
 121             System.out.println("Printing deserialized class: ");
 122             System.out.println();
 123             System.out.println(obj.toString());
 124             System.out.println();
 125         }
 126     }
 127 
 128 
 129     public String toString() {
 130         String nextString = next != null ? next.toString() : "<null>";
 131         return "name =" + name + " next = <" + nextString + ">";
 132     }
 133 
 134     /**
 135      * Prints out the usage


  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  *
  26  * @bug 4087295
  27  * @build install/SerialDriver.java test/SerialDriver.java extension/ExtendedObjectInputStream.java
  28  * @summary Enable resolveClass() to accommodate package renaming.
  29  * This fix enables one to implement a resolveClass method that maps a
  30  * Serializable class within a serialization stream to the same class
  31  * in a different package within the JVM runtime. See run shell script
  32  * for instructions on how to run this test.
  33  */
  34 
  35 package install;
  36 
  37 import java.io.*;
  38 import extension.ExtendedObjectInputStream;
  39 
  40 public class SerialDriver implements Serializable {
  41     private static final long serialVersionUID = 1L;
  42 
  43     String name;
  44     SerialDriver next;
  45     transient Object objarray[];
  46 
  47     public SerialDriver() {
  48         name = "<terminator>";
  49         next = null;
  50     }


  69          * The ability to deserialize or serialize allows
  70          * us to see the bidirectional readability and writeability
  71          */
  72         if (args.length == 1) {
  73             if (args[0].equals("-d")) {
  74                 deserialize = true;
  75             } else if (args[0].equals("-s")) {
  76                 serialize = true;
  77             } else {
  78                 usage();
  79                 throw new Exception("incorrect command line arguments");
  80             }
  81         } else {
  82             usage();
  83             throw new Exception("incorrect command line arguments");
  84         }
  85 
  86         File f = new File("stream.ser");
  87         if (serialize) {
  88             // Serialize the subclass
  89             try (FileOutputStream fo = new FileOutputStream(f);
  90                  ObjectOutputStream so = new ObjectOutputStream(fo))
  91             {
  92                 so.writeObject(obj);
  93                 /* Skip arrays since they do not work with rename yet.
  94                    The serialVersionUID changes due to the name change
  95                    and there is no way to set the serialVersionUID for an
  96                    array. */
  97                 so.writeObject(array);

  98             } catch (Exception e) {
  99                 System.out.println(e);
 100                 throw e;
 101             }
 102         }
 103         if (deserialize) {
 104             // Deserialize the subclass
 105             try (FileInputStream fi = new FileInputStream(f);
 106                  ExtendedObjectInputStream si = new ExtendedObjectInputStream(fi))
 107             {

 108                 si.addRenamedClassName("test.SerialDriver", "install.SerialDriver");
 109                 si.addRenamedClassName("[Ltest.SerialDriver;",
 110                                        "[Linstall.SerialDriver");
 111                 obj = (SerialDriver) si.readObject();
 112                 array = (SerialDriver[]) si.readObject();

 113             } catch (Exception e) {
 114                 System.out.println(e);
 115                 throw e;
 116             }
 117             System.out.println();
 118             System.out.println("Printing deserialized class: ");
 119             System.out.println();
 120             System.out.println(obj.toString());
 121             System.out.println();
 122         }
 123     }
 124 
 125 
 126     public String toString() {
 127         String nextString = next != null ? next.toString() : "<null>";
 128         return "name =" + name + " next = <" + nextString + ">";
 129     }
 130 
 131     /**
 132      * Prints out the usage