Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/classDescHooks/ExternLoopback.java
38828 views
/*1* Copyright (c) 2001, 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 descs;3536LoopbackOutputStream(OutputStream out, LinkedList 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 descs;5253LoopbackInputStream(InputStream in, LinkedList descs) throws IOException {54super(in);55this.descs = descs;56}5758protected ObjectStreamClass readClassDescriptor()59throws IOException, ClassNotFoundException60{61return (ObjectStreamClass) descs.removeFirst();62}63}6465public class ExternLoopback implements Externalizable {6667String a, b, c;6869public ExternLoopback() {70}7172ExternLoopback(String a, String b, String c) {73this.a = a;74this.b = b;75this.c = c;76}7778public void writeExternal(ObjectOutput out) throws IOException {79out.writeBoolean(false);80out.writeObject(a);81out.writeObject(b);82out.writeObject(c);83}8485public void readExternal(ObjectInput in)86throws IOException, ClassNotFoundException87{88in.readBoolean();89a = (String) in.readObject();90b = (String) in.readObject();91c = (String) in.readObject();92}9394public boolean equals(Object obj) {95if (!(obj instanceof ExternLoopback)) {96return false;97}98ExternLoopback other = (ExternLoopback) obj;99return streq(a, other.a) && streq(b, other.b) && streq(c, other.c);100}101102static boolean streq(String s1, String s2) {103return (s1 != null) ? s1.equals(s2) : (s2 == null);104}105106public static void main(String[] args) throws Exception {107ExternLoopback lb = new ExternLoopback("foo", "bar", "baz");108LinkedList descs = new LinkedList();109ByteArrayOutputStream bout = new ByteArrayOutputStream();110LoopbackOutputStream lout = new LoopbackOutputStream(bout, descs);111lout.writeObject(lb);112lout.close();113114LoopbackInputStream lin = new LoopbackInputStream(115new ByteArrayInputStream(bout.toByteArray()), descs);116ExternLoopback lbcopy = (ExternLoopback) lin.readObject();117if (!lb.equals(lbcopy)) {118throw new Error();119}120}121}122123124