Path: blob/master/test/jdk/java/io/Externalizable/definesWriteObject/DefinesWriteObject.java
41153 views
/*1* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 415146926* @summary Write and read an Externalizable class that defines writeObject.27* There was some confusion over writeObject method needing28* to be defined by an Externalizable class. It does not29* need to exist, but Externalizable class should work correctly30* if the member exists.31*/3233import java.io.*;3435public class DefinesWriteObject implements Externalizable {3637private int intData = 4;38private Object objData = new String("hello");3940public DefinesWriteObject() {41}4243public DefinesWriteObject(int i, Object o) {44intData = i;45objData = o;46}4748/**49* There was some confusion over writeObject method needing50* to be defined by an Externalizable class. It does not51* need to exist, but Externalizable class should work correctly52* if the member exists.53*/54private void writeObject(ObjectOutputStream out) throws IOException {55}5657/**58* @serialData Writes an integer, Object.59*/60public void writeExternal(ObjectOutput out)61throws IOException62{63out.writeInt(intData);64out.writeObject(objData);65}6667public void readExternal(ObjectInput in)68throws IOException, ClassNotFoundException69{70intData = in.readInt();71objData = in.readObject();72}7374public static void main(String args[])75throws IOException, ClassNotFoundException76{77DefinesWriteObject obj1 = new DefinesWriteObject(5, "GoodBye");78DefinesWriteObject obj2 = new DefinesWriteObject(6, "AuRevoir");7980ByteArrayOutputStream baos = new ByteArrayOutputStream();81ObjectOutputStream oos = new ObjectOutputStream(baos);82oos.writeObject(obj1);83oos.writeObject(obj2);84oos.close();8586ByteArrayInputStream bais =87new ByteArrayInputStream(baos.toByteArray());88ObjectInputStream ois = new ObjectInputStream(bais);89DefinesWriteObject readObject1 = (DefinesWriteObject)ois.readObject();90DefinesWriteObject readObject2 = (DefinesWriteObject)ois.readObject();91ois.close();9293// verify that deserialize data matches objects serialized.94if (obj1.intData != readObject1.intData ||95obj2.intData != readObject2.intData) {96throw new Error("Unexpected mismatch between integer data written and read.");97}98if ( ! ((String)obj1.objData).equals((String)readObject1.objData) ||99! ((String)obj2.objData).equals((String)readObject2.objData)) {100throw new Error("Unexpected mismatch between String data written and read.");101}102}103};104105106