Path: blob/master/test/jdk/java/io/Serializable/classDescHooks/Loopback.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 446129925* @summary Verify that serialization functions properly if26* ObjectInputStream.readClassDescriptor() returns a local class27* descriptor for which the serialVersionUID has not yet been28* calculated.29*/3031import java.io.*;32import java.util.*;3334class LoopbackOutputStream extends ObjectOutputStream {35LinkedList<ObjectStreamClass> descs;3637LoopbackOutputStream(OutputStream out, LinkedList<ObjectStreamClass> descs)38throws IOException39{40super(out);41this.descs = descs;42}4344protected void writeClassDescriptor(ObjectStreamClass desc)45throws IOException46{47descs.add(desc);48}49}5051class LoopbackInputStream extends ObjectInputStream {52LinkedList<ObjectStreamClass> descs;5354LoopbackInputStream(InputStream in, LinkedList<ObjectStreamClass> descs) throws IOException {55super(in);56this.descs = descs;57}5859protected ObjectStreamClass readClassDescriptor()60{61return descs.removeFirst();62}63}6465public class Loopback implements Serializable {66private static final long serialVersionUID = 1L;6768String str;6970Loopback(String str) {71this.str = str;72}7374public static void main(String[] args) throws Exception {75Loopback lb = new Loopback("foo");76LinkedList<ObjectStreamClass> descs = new LinkedList<>();77ByteArrayOutputStream bout = new ByteArrayOutputStream();78LoopbackOutputStream lout = new LoopbackOutputStream(bout, descs);79lout.writeObject(lb);80lout.close();8182LoopbackInputStream lin = new LoopbackInputStream(83new ByteArrayInputStream(bout.toByteArray()), descs);84Loopback lbcopy = (Loopback) lin.readObject();85if (!lb.str.equals(lbcopy.str)) {86throw new Error();87}88}89}909192