1/* 2 * Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* @test 25 * @bug 4089540 26 * @summary Verify compatibility with 1.1 externalizable format 27 */ 28 29import java.io.*; 30import java.util.*; 31 32class Foo implements Externalizable { 33 private static final long serialVersionUID = 0xbabel; 34 35 int x; 36 int y; 37 Object obj; 38 39 public Foo() { 40 } 41 42 public Foo(int x, int y, Object obj) { 43 this.x = x; 44 this.y = y; 45 this.obj = obj; 46 } 47 48 public void writeExternal(ObjectOutput out) throws IOException { 49 out.writeInt(x); 50 out.writeInt(y); 51 out.writeObject(obj); 52 } 53 54 public void readExternal(ObjectInput in) 55 throws IOException, ClassNotFoundException 56 { 57 x = in.readInt(); 58 y = in.readInt(); 59 obj = in.readObject(); 60 } 61 62 public boolean equals(Object other) { 63 if (other instanceof Foo) { 64 Foo f = (Foo) other; 65 return ((x == f.x) && (y == f.y) && 66 ((obj != null) ? obj.equals(f.obj) : (f.obj == null))); 67 } 68 return false; 69 } 70} 71 72public class ExternalizableBlockData { 73 public static void main(String[] args) throws Exception { 74 byte[] oldExternalizableBytes = getFileBytes( 75 new File(System.getProperty("test.src", "."), "old.ser")); 76 Foo foo = new Foo(0xbad, 0xbeef, "burrito"); 77 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 78 ObjectOutputStream oout = new ObjectOutputStream(bout); 79 oout.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_1); 80 oout.writeObject(foo); 81 oout.close(); 82 if (! Arrays.equals(bout.toByteArray(), oldExternalizableBytes)) { 83 throw new Error(); 84 } 85 86 ObjectInputStream oin = new ObjectInputStream( 87 new ByteArrayInputStream(oldExternalizableBytes)); 88 if (! foo.equals(oin.readObject())) { 89 throw new Error(); 90 } 91 92 bout = new ByteArrayOutputStream(); 93 oout = new ObjectOutputStream(bout); 94 oout.writeObject(foo); 95 oout.close(); 96 if (Arrays.equals(bout.toByteArray(), oldExternalizableBytes)) { 97 throw new Error(); 98 } 99 } 100 101 static byte[] getFileBytes(File file) throws IOException { 102 FileInputStream fin = new FileInputStream(file); 103 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 104 byte[] buf = new byte[256]; 105 int n; 106 107 while ((n = fin.read(buf)) != -1) { 108 bout.write(buf, 0, n); 109 } 110 fin.close(); 111 return bout.toByteArray(); 112 } 113} 114