Path: blob/master/test/jdk/java/io/Serializable/classDescHooks/ExternLoopback.java
41154 views
/*1* Copyright (c) 2001, 2019, 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/* @test24* @bug 446173725* @summary Verify that serialization functions properly for externalizable26* classes if ObjectInputStream.readClassDescriptor() returns a local27* class descriptor.28*/2930import java.io.*;31import java.util.*;3233class LoopbackOutputStream extends ObjectOutputStream {34LinkedList<ObjectStreamClass> descs;3536LoopbackOutputStream(OutputStream out, LinkedList<ObjectStreamClass> descs)37throws IOException38{39super(out);40this.descs = descs;41}4243protected void writeClassDescriptor(ObjectStreamClass desc)44throws IOException45{46descs.add(desc);47}48}4950class LoopbackInputStream extends ObjectInputStream {51LinkedList<ObjectStreamClass> descs;5253LoopbackInputStream(InputStream in, LinkedList<ObjectStreamClass> descs) throws IOException {54super(in);55this.descs = descs;56}5758protected ObjectStreamClass readClassDescriptor()59throws IOException, ClassNotFoundException60{61return descs.removeFirst();62}63}6465public class ExternLoopback implements Externalizable {66private static final long serialVersionUID = 1L;6768String a, b, c;6970public ExternLoopback() {71}7273ExternLoopback(String a, String b, String c) {74this.a = a;75this.b = b;76this.c = c;77}7879public void writeExternal(ObjectOutput out) throws IOException {80out.writeBoolean(false);81out.writeObject(a);82out.writeObject(b);83out.writeObject(c);84}8586public void readExternal(ObjectInput in)87throws IOException, ClassNotFoundException88{89in.readBoolean();90a = (String) in.readObject();91b = (String) in.readObject();92c = (String) in.readObject();93}9495public boolean equals(Object obj) {96if (!(obj instanceof ExternLoopback)) {97return false;98}99ExternLoopback other = (ExternLoopback) obj;100return streq(a, other.a) && streq(b, other.b) && streq(c, other.c);101}102103public int hashCode() {104return a.hashCode();105}106107static boolean streq(String s1, String s2) {108return (s1 != null) ? s1.equals(s2) : (s2 == null);109}110111public static void main(String[] args) throws Exception {112ExternLoopback lb = new ExternLoopback("foo", "bar", "baz");113LinkedList<ObjectStreamClass> descs = new LinkedList<>();114ByteArrayOutputStream bout = new ByteArrayOutputStream();115LoopbackOutputStream lout = new LoopbackOutputStream(bout, descs);116lout.writeObject(lb);117lout.close();118119LoopbackInputStream lin = new LoopbackInputStream(120new ByteArrayInputStream(bout.toByteArray()), descs);121ExternLoopback lbcopy = (ExternLoopback) lin.readObject();122if (!lb.equals(lbcopy)) {123throw new Error();124}125}126}127128129