Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/classDescHooks/ClassDescHooks.java
38828 views
/*1* Copyright (c) 1999, 2010, 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 422718925* @summary Ensure that class descriptor read, write hooks exist, are backwards26* compatible, and work as advertised.27*/2829import java.io.*;30import java.util.*;3132class Foo implements Serializable {33private static final long serialVersionUID = 1L;34Short s = new Short((short) 1);35Integer i = new Integer(2);36Long l = new Long(3);3738public boolean equals(Object obj) {39if (obj instanceof Foo) {40Foo ofoo = (Foo) obj;41return s.equals(ofoo.s) && i.equals(ofoo.i) && l.equals(ofoo.l);42}43return false;44}45}4647class CustomOutputStream extends ObjectOutputStream {4849boolean hookCalled = false;5051CustomOutputStream(OutputStream out) throws IOException {52super(out);53useProtocolVersion(PROTOCOL_VERSION_2);54}5556protected void writeClassDescriptor(ObjectStreamClass desc)57throws IOException58{59writeUTF(desc.getName());60hookCalled = true;61}62}6364class CustomInputStream extends ObjectInputStream {6566boolean hookCalled = false;6768CustomInputStream(InputStream in) throws IOException {69super(in);70}7172protected ObjectStreamClass readClassDescriptor()73throws IOException, ClassNotFoundException74{75hookCalled = true;76return ObjectStreamClass.lookup(Class.forName(readUTF()));77}78}7980public class ClassDescHooks implements ObjectStreamConstants {81public static void main(String[] args) throws Exception {82ByteArrayOutputStream bout;83ByteArrayInputStream bin;84ObjectOutputStream oout;85ObjectInputStream oin;86FileInputStream fin;87File foof;88CustomOutputStream cout;89CustomInputStream cin;9091// test for backwards compatibility92bout = new ByteArrayOutputStream();93foof = new File(System.getProperty("test.src", "."), "Foo.ser");94fin = new FileInputStream(foof);95try {96while (fin.available() > 0)97bout.write(fin.read());98} finally {99fin.close();100}101byte[] buf1 = bout.toByteArray();102103bout = new ByteArrayOutputStream();104oout = new ObjectOutputStream(bout);105Foo foo = new Foo();106oout.writeObject(foo);107oout.flush();108byte[] buf2 = bout.toByteArray();109110if (! Arrays.equals(buf1, buf2))111throw new Error("Incompatible stream format (write)");112113Foo foocopy;114fin = new FileInputStream(foof);115try {116oin = new ObjectInputStream(fin);117foocopy = (Foo) oin.readObject();118if (! foo.equals(foocopy))119throw new Error("Incompatible stream format (read)");120} finally {121fin.close();122}123124// make sure write hook not called when old protocol in use125bout = new ByteArrayOutputStream();126cout = new CustomOutputStream(bout);127cout.useProtocolVersion(PROTOCOL_VERSION_1);128cout.writeObject(foo);129if (cout.hookCalled)130throw new Error("write descriptor hook should not be called");131132// write custom class descriptor representations133bout = new ByteArrayOutputStream();134cout = new CustomOutputStream(bout);135cout.writeObject(foo);136cout.flush();137bin = new ByteArrayInputStream(bout.toByteArray());138cin = new CustomInputStream(bin);139foocopy = (Foo) cin.readObject();140if (! cout.hookCalled)141throw new Error("write descriptor hook never called");142if (! cin.hookCalled)143throw new Error("read descriptor hook never called");144if (! foo.equals(foocopy))145throw new Error("serialization failed when hooks active");146}147}148149150