Path: blob/master/test/jdk/java/io/Serializable/misplacedArrayClassDesc/MisplacedArrayClassDesc.java
41152 views
/*1* Copyright (c) 2005, 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 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 {33private static final long serialVersionUID = 1L;3435// size of array36private static final int ARR_SIZE = 5;37// serializable field38private String[] strArr = new String[ARR_SIZE];3940public TestArray() {41for (int i = 0; i < ARR_SIZE; i++) {42strArr[i] = "test" + i;43}44}45}4647public class MisplacedArrayClassDesc {48public static final void main(String[] args) throws Exception {49System.err.println("\nRegression test for CR6313687");50TestArray object = new TestArray();51try {52// Serialize to a byte array53ByteArrayOutputStream bos = new ByteArrayOutputStream() ;54ObjectOutputStream out = new ObjectOutputStream(bos) ;55out.writeObject(object);56out.close();5758// Get the bytes of the serialized object59byte[] buf = bos.toByteArray();60for (int i = 0; i < buf.length; i++) {61if (buf[i] == ObjectOutputStream.TC_ARRAY) {62buf[i] = ObjectOutputStream.TC_OBJECT;63break;64}65}6667// Deserialize from a byte array68ByteArrayInputStream bais = new ByteArrayInputStream(buf);69ObjectInputStream in = new ObjectInputStream(bais);70TestArray ta = (TestArray) in.readObject();71in.close();72} catch (InstantiationError e) {73throw new Error();74} catch (InvalidClassException e) {75System.err.println("\nTest passed");76return;77}78throw new Error();79}80}818283