Path: blob/master/test/jdk/java/io/Serializable/cloneArray/CloneArray.java
41154 views
/*1* Copyright (c) 2010, 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*/222324/* @test25* @bug 699009426* @summary Verify ObjectInputStream.cloneArray works on many kinds of arrays27* @author Stuart Marks, Joseph D. Darcy28*/2930import java.io.ByteArrayInputStream;31import java.io.ByteArrayOutputStream;32import java.io.IOException;33import java.io.ObjectInputStream;34import java.io.ObjectOutputStream;35import java.io.ObjectStreamException;36import java.io.Serializable;3738public class CloneArray {39static Object replacement;4041static class Resolver implements Serializable {42private static final long serialVersionUID = 1L;4344private Object readResolve() throws ObjectStreamException {45return replacement;46}47}4849private static void test(Object rep)50throws IOException, ClassNotFoundException {5152try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) {53try(ObjectOutputStream oos = new ObjectOutputStream(baos)) {54oos.writeObject(new Resolver());55oos.writeObject(new Resolver());56}5758Object o1;59Object o2;60try(ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());61ObjectInputStream ois = new ObjectInputStream(bais)) {62replacement = rep;63o1 = ois.readUnshared();64o2 = ois.readUnshared();65}6667if (o1 == o2)68throw new AssertionError("o1 and o2 must not be identical");69}70}7172public static void main(String[] args)73throws IOException, ClassNotFoundException {74Object[] replacements = {75new byte[] {1},76new char[] {'2'},77new short[] {3},78new int[] {4},79new long[] {5},80new float[] {6.0f},81new double[] {7.0},82new boolean[] {true},83new Object[] {"A string."}84};8586for(Object replacement : replacements) {87test(replacement);88}89}90}919293