blob: 14687ea7941341e11548dbb134bb0f108a847002 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @bug 4228592
26 * @summary Ensure that ObjectInputStream properly skips over block data when a
27 * class that defines readObject() or readExternal() fails to read all
28 * of the data written by the corresponding writeObject() or
29 * writeExternal() method.
30 */
31
32import java.io.*;
33
34class MismatchedRead implements Serializable {
35 int i;
36 float f;
37
38 MismatchedRead(int i, float f) {
39 this.i = i;
40 this.f = f;
41 }
42
43 private void writeObject(ObjectOutputStream out) throws IOException {
44 out.writeInt(i);
45 out.writeFloat(f);
46 out.writeUTF("skip me");
47 }
48
49 private void readObject(ObjectInputStream in)
50 throws IOException, ClassNotFoundException
51 {
52 i = in.readInt();
53 f = in.readFloat();
54 }
55
56 public boolean equals(Object obj) {
57 if (! (obj instanceof MismatchedRead))
58 return false;
59 MismatchedRead other = (MismatchedRead) obj;
60 return (i == other.i && f == other.f);
61 }
62}
63
64class MismatchedReadExternal implements Externalizable {
65 int i;
66 float f;
67
68 public MismatchedReadExternal() {
69 this(0, (float) 0.0);
70 }
71
72 MismatchedReadExternal(int i, float f) {
73 this.i = i;
74 this.f = f;
75 }
76
77 public void writeExternal(ObjectOutput out) throws IOException {
78 out.writeInt(i);
79 out.writeFloat(f);
80 out.writeUTF("skip another");
81 }
82
83 public void readExternal(ObjectInput in) throws IOException {
84 i = in.readInt();
85 f = in.readFloat();
86 }
87
88 public boolean equals(Object obj) {
89 if (! (obj instanceof MismatchedReadExternal))
90 return false;
91 MismatchedReadExternal other = (MismatchedReadExternal) obj;
92 return (i == other.i && f == other.f);
93 }
94}
95
96class InnocentBystander implements Serializable {
97 String s;
98
99 InnocentBystander(String s) {
100 this.s = s;
101 }
102
103 public boolean equals(Object obj) {
104 if (! (obj instanceof InnocentBystander))
105 return false;
106 InnocentBystander other = (InnocentBystander) obj;
107 if (s != null)
108 return s.equals(other.s);
109 return (s == other.s);
110 }
111}
112
113public class SkipToEndOfBlockData {
114 public static void main(String[] args) throws Exception {
115 ObjectOutputStream oout;
116 ObjectInputStream oin;
117 ByteArrayOutputStream bout;
118 ByteArrayInputStream bin;
119 MismatchedRead mr, mrcopy;
120 MismatchedReadExternal mre, mrecopy;
121 InnocentBystander ib, ibcopy;
122
123 bout = new ByteArrayOutputStream();
124 oout = new ObjectOutputStream(bout);
125 mr = new MismatchedRead(1, (float) 2.34);
126 mre = new MismatchedReadExternal(5, (float) 6.78);
127 ib = new InnocentBystander("foo");
128
129 oout.writeObject(mr);
130 oout.writeObject(mre);
131 oout.writeObject(ib);
132 oout.flush();
133
134 bin = new ByteArrayInputStream(bout.toByteArray());
135 oin = new ObjectInputStream(bin);
136 mrcopy = (MismatchedRead) oin.readObject();
137 mrecopy = (MismatchedReadExternal) oin.readObject();
138 ibcopy = (InnocentBystander) oin.readObject();
139
140 if (! (mr.equals(mrcopy) && mre.equals(mrecopy) && ib.equals(ibcopy)))
141 throw new Error("copy not equal to original");
142 }
143}