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