Path: blob/master/test/jdk/java/io/Externalizable/compatibility/ExternalizableBlockData.java
41153 views
/*1* Copyright (c) 2000, 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 408954025* @summary Verify compatibility with 1.1 externalizable format26*/2728import java.io.*;29import java.util.*;3031class Foo implements Externalizable {32private static final long serialVersionUID = 0xbabel;3334int x;35int y;36Object obj;3738public Foo() {39}4041public Foo(int x, int y, Object obj) {42this.x = x;43this.y = y;44this.obj = obj;45}4647public void writeExternal(ObjectOutput out) throws IOException {48out.writeInt(x);49out.writeInt(y);50out.writeObject(obj);51}5253public void readExternal(ObjectInput in)54throws IOException, ClassNotFoundException55{56x = in.readInt();57y = in.readInt();58obj = in.readObject();59}6061public boolean equals(Object other) {62if (other instanceof Foo) {63Foo f = (Foo) other;64return ((x == f.x) && (y == f.y) &&65((obj != null) ? obj.equals(f.obj) : (f.obj == null)));66}67return false;68}69}7071public class ExternalizableBlockData {72public static void main(String[] args) throws Exception {73byte[] oldExternalizableBytes = getFileBytes(74new File(System.getProperty("test.src", "."), "old.ser"));75Foo foo = new Foo(0xbad, 0xbeef, "burrito");76ByteArrayOutputStream bout = new ByteArrayOutputStream();77ObjectOutputStream oout = new ObjectOutputStream(bout);78oout.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_1);79oout.writeObject(foo);80oout.close();81if (! Arrays.equals(bout.toByteArray(), oldExternalizableBytes)) {82throw new Error();83}8485ObjectInputStream oin = new ObjectInputStream(86new ByteArrayInputStream(oldExternalizableBytes));87if (! foo.equals(oin.readObject())) {88throw new Error();89}9091bout = new ByteArrayOutputStream();92oout = new ObjectOutputStream(bout);93oout.writeObject(foo);94oout.close();95if (Arrays.equals(bout.toByteArray(), oldExternalizableBytes)) {96throw new Error();97}98}99100static byte[] getFileBytes(File file) throws IOException {101FileInputStream fin = new FileInputStream(file);102ByteArrayOutputStream bout = new ByteArrayOutputStream();103byte[] buf = new byte[256];104int n;105106while ((n = fin.read(buf)) != -1) {107bout.write(buf, 0, n);108}109fin.close();110return bout.toByteArray();111}112}113114115