Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/misplacedArrayClassDesc/MisplacedArrayClassDesc.java
38821 views
/*1* Copyright (c) 2005, 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 631368725* @summary Verify that if the class descriptor for an ordinary object is the26* descriptor for an array class, an ObjectStreamException is thrown.27*28*/2930import java.io.*;3132class TestArray implements Serializable {33// size of array34private static final int ARR_SIZE = 5;35// serializable field36private String[] strArr = new String[ARR_SIZE];3738public TestArray() {39for (int i = 0; i < ARR_SIZE; i++) {40strArr[i] = "test" + i;41}42}43}4445public class MisplacedArrayClassDesc {46public static final void main(String[] args) throws Exception {47System.err.println("\nRegression test for CR6313687");48TestArray object = new TestArray();49try {50// Serialize to a byte array51ByteArrayOutputStream bos = new ByteArrayOutputStream() ;52ObjectOutputStream out = new ObjectOutputStream(bos) ;53out.writeObject(object);54out.close();5556// Get the bytes of the serialized object57byte[] buf = bos.toByteArray();58for (int i = 0; i < buf.length; i++) {59if (buf[i] == ObjectOutputStream.TC_ARRAY) {60buf[i] = ObjectOutputStream.TC_OBJECT;61break;62}63}6465// Deserialize from a byte array66ByteArrayInputStream bais = new ByteArrayInputStream(buf);67ObjectInputStream in = new ObjectInputStream(bais);68TestArray ta = (TestArray) in.readObject();69in.close();70} catch (InstantiationError e) {71throw new Error();72} catch (InvalidClassException e) {73System.err.println("\nTest passed");74return;75}76throw new Error();77}78}798081