0N/A/*
2362N/A * Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4099013
0N/A * @summary Enable substitution of String and Array by ObjectStreams.
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.lang.reflect.Array;
0N/A
0N/Aclass A implements Serializable {
0N/A String stringA;
0N/A String stringB;
0N/A String stringC;
0N/A String[] arrayOfString;
0N/A
0N/A A() {
0N/A stringA = "hello";
0N/A stringB = "goodbye";
0N/A stringC = stringA;
0N/A arrayOfString = new String[2];
0N/A for (int i = 0; i < arrayOfString.length; i++)
0N/A arrayOfString[i] = new String("array element " + i);
0N/A }
0N/A
0N/A void report() {
0N/A System.out.println("stringA = " + stringA);
0N/A System.out.println("stringB = " + stringB);
0N/A System.out.println("stringC = " + stringC);
0N/A System.out.println("length of arrayOfString = " +
0N/A arrayOfString.length);
0N/A for (int i = 0; i < arrayOfString.length; i++)
0N/A System.out.println("arrayOfString[" + i + "]= " +
0N/A arrayOfString[i]);
0N/A }
0N/A}
0N/A
0N/Aclass SubstituteObjectOutputStream extends ObjectOutputStream {
0N/A public int numStringsReplaced = 0;
0N/A public int numArraysCounted = 0;
0N/A
0N/A public SubstituteObjectOutputStream(OutputStream out) throws IOException {
0N/A super(out);
0N/A enableReplaceObject(true);
0N/A }
0N/A
0N/A protected Object replaceObject(Object obj) throws IOException {
0N/A if (obj instanceof String) {
0N/A numStringsReplaced++;
0N/A return obj + "_WriteReplaced";
0N/A }
0N/A if (obj.getClass().isArray()) {
0N/A Object[] array = (Object[]) obj;
0N/A /* Double the array.
0N/A * Initialize new array elements with original array. */
0N/A Class arrayComponentType = array.getClass().getComponentType();
0N/A Object[] newarray =
0N/A (Object[])Array.newInstance(arrayComponentType,
0N/A array.length * 2);
0N/A for (int i = 0; i < array.length; i++)
0N/A newarray[i] = array[i];
0N/A for (int ni = array.length; ni < 2* array.length; ni++)
0N/A newarray[ni] = array[ni - array.length];
0N/A numArraysCounted++;
0N/A obj = newarray;
0N/A }
0N/A return obj;
0N/A }
0N/A}
0N/A
0N/Aclass SubstituteObjectInputStream extends ObjectInputStream {
0N/A public int numStringsReplaced = 0;
0N/A public int numArraysCounted = 0;
0N/A
0N/A public SubstituteObjectInputStream(InputStream in) throws IOException {
0N/A super(in);
0N/A enableResolveObject(true);
0N/A }
0N/A
0N/A protected Object resolveObject(Object obj) throws IOException {
0N/A if (obj instanceof String) {
0N/A numStringsReplaced++;
0N/A return obj + "_ReadResolved";
0N/A }
0N/A if (obj.getClass().isArray()) {
0N/A Object[] array = (Object[])obj;
0N/A
0N/A /* Double the array.
0N/A * Initialize new array elements with original array. */
0N/A Class arrayComponentType = array.getClass().getComponentType();
0N/A Object[] newarray =
0N/A (Object[])Array.newInstance(arrayComponentType,
0N/A array.length * 2);
0N/A for (int i = 0; i < array.length; i++)
0N/A newarray[i] = array[i];
0N/A for (int ni = array.length; ni < 2* array.length; ni++)
0N/A newarray[ni] = array[ni - array.length];
0N/A numArraysCounted++;
0N/A obj = newarray;
0N/A }
0N/A return obj;
0N/A }
0N/A}
0N/A
0N/Apublic class ReplaceStringArray {
0N/A public static void main(String args[]) throws IOException, ClassNotFoundException {
0N/A boolean verbose = false;
0N/A if (args.length >= 1 && args[0].compareTo("verbose") == 0)
0N/A verbose = true;
0N/A
0N/A
0N/A A a = new A();
0N/A if (verbose) {
0N/A System.out.println("Value of Class A");
0N/A a.report();
0N/A System.out.println("");
0N/A }
0N/A
0N/A
0N/A /* Serialize object a to bytestream. */
0N/A if (verbose) {
0N/A System.out.println("Serialize A to SubstituteObjectOutputStream");
0N/A System.out.println("");
0N/A }
0N/A ByteArrayOutputStream baos = new ByteArrayOutputStream();
0N/A SubstituteObjectOutputStream out = new SubstituteObjectOutputStream(baos);
0N/A out.writeObject(a);
0N/A out.close();
0N/A a = null;
0N/A
0N/A /* Validate that writeReplace called by SubstituteObjectOutputStream.*/
0N/A boolean expectedResult = (out.numStringsReplaced == 4);
0N/A if (!expectedResult)
0N/A throw new Error("Expected " + 4 + " strings to be replaced during serialization;" +
0N/A " only " + out.numStringsReplaced + " strings were replaced.");
0N/A if (out.numArraysCounted != 1)
0N/A throw new Error("Expected 1 array during serialization; only " +
0N/A out.numArraysCounted + " arrays");
0N/A
0N/A if (verbose) {
0N/A System.out.println("DeSerialize A from SubstituteObjectInputStream");
0N/A System.out.println("");
0N/A }
0N/A SubstituteObjectInputStream in =
0N/A new SubstituteObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
0N/A a = (A)in.readObject();
0N/A in.close();
0N/A
0N/A /* Validate that readResolve called by SubstituteObjectInputStream.*/
0N/A if (in.numStringsReplaced != 4)
0N/A throw new Error("Expected 4 strings to be resolved during deserialization;" +
0N/A " only " + in.numStringsReplaced + " strings were resolved.");
0N/A if (in.numArraysCounted != 1)
0N/A throw new Error("Expected 1 array during deserialization; only " +
0N/A out.numArraysCounted + " arrays");
0N/A if (a.arrayOfString.length != 8)
0N/A throw new Error("Expected a.arrayOfString.length to be 8, observed " +
0N/A a.arrayOfString.length);
0N/A if (verbose) {
0N/A System.out.println("Value of Class A after serialize/deserialize with writeReplace/readResolve");
0N/A a.report();
0N/A }
0N/A }
0N/A}