1/* 2 * Copyright (c) 2000, 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/* @test 25 * @bug 4311991 26 * 27 * @clean Write Read Foo Bar 28 * @compile Write.java 29 * @run main Write 30 * @clean Write Read Foo Bar 31 * @compile Read.java 32 * @run main Read 33 * 34 * @summary Test ObjectOutputStream.writeUnshared/readUnshared functionality. 35 */ 36 37import java.io.*; 38 39class Foo implements Serializable { 40 private static final ObjectStreamField[] serialPersistentFields = 41 new ObjectStreamField[] { 42 new ObjectStreamField("shared1", String.class), 43 new ObjectStreamField("shared2", String.class, false), 44 new ObjectStreamField("unshared1", String.class, true), 45 new ObjectStreamField("unshared2", String.class, true) 46 }; 47 48 String shared1, shared2, unshared1, unshared2; 49 50 Foo() { 51 shared1 = shared2 = unshared1 = unshared2 = "foo"; 52 } 53} 54 55class Bar implements Serializable { 56 private static final long serialVersionUID = 0L; 57 Object obj; 58 59 Bar(Object obj) { 60 this.obj = obj; 61 } 62} 63 64public class Write { 65 public static void main(String[] args) throws Exception { 66 String str1 = "foo"; 67 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 68 ObjectOutputStream oout = new ObjectOutputStream(bout); 69 70 oout.writeObject(str1); 71 oout.writeObject(str1); 72 oout.writeUnshared(str1); 73 oout.writeUnshared(str1); 74 oout.writeObject(new Foo()); 75 oout.close(); 76 77 ByteArrayInputStream bin = 78 new ByteArrayInputStream(bout.toByteArray()); 79 ObjectInputStream oin = new ObjectInputStream(bin); 80 str1 = (String) oin.readObject(); 81 if (oin.readObject() != str1) { 82 throw new Error(); 83 } 84 String str2 = (String) oin.readObject(); 85 String str3 = (String) oin.readObject(); 86 if (str2 == str1 || str3 == str1 || str2 == str3) { 87 throw new Error(); 88 } 89 if (! (str1.equals(str2) && str1.equals(str3))) { 90 throw new Error(); 91 } 92 93 Foo foo = (Foo) oin.readObject(); 94 if ((foo.shared1 != foo.shared2) || 95 (foo.shared1 == foo.unshared1) || 96 (foo.shared1 == foo.unshared2) || 97 (foo.shared2 == foo.unshared1) || 98 (foo.shared2 == foo.unshared2) || 99 (foo.unshared1 == foo.unshared2)) 100 { 101 throw new Error(); 102 } 103 104 // write out object to be read by Read.main() 105 oout = new ObjectOutputStream(new FileOutputStream("tmp.ser")); 106 oout.writeObject(new Bar(str1)); 107 oout.writeObject(str1); 108 oout.close(); 109 } 110} 111