1/* 2 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 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 35package install; 36 37import java.io.*; 38import extension.ExtendedObjectInputStream; 39 40public 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 } 51 52 public SerialDriver(String name, SerialDriver next) { 53 this.name = name; 54 this.next = next; 55 } 56 57 static boolean serialize; 58 static boolean deserialize; 59 60 public static void main(String args[]) throws Exception { 61 SerialDriver obj = new SerialDriver("SerialDriver_2", 62 new SerialDriver()); 63 SerialDriver[] array = new SerialDriver[5]; 64 for (int i = 0; i < array.length; i++) 65 array[i] = new SerialDriver("SerialDriver_1_" + i, new SerialDriver()); 66 67 /* 68 * see if we are serializing or deserializing. 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 133 */ 134 static void usage() { 135 System.out.println("Usage:"); 136 System.out.println(" -s (in order to serialize)"); 137 System.out.println(" -d (in order to deserialize)"); 138 } 139} 140