author | rriggs |
Fri, 11 Oct 2019 13:11:56 -0400 | |
changeset 58565 | baa5969ecf34 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
||
32 |
import java.io.*; |
|
33 |
||
34 |
class MismatchedRead implements Serializable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
35 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
36 |
|
2 | 37 |
int i; |
38 |
float f; |
|
39 |
||
40 |
MismatchedRead(int i, float f) { |
|
41 |
this.i = i; |
|
42 |
this.f = f; |
|
43 |
} |
|
44 |
||
45 |
private void writeObject(ObjectOutputStream out) throws IOException { |
|
46 |
out.writeInt(i); |
|
47 |
out.writeFloat(f); |
|
48 |
out.writeUTF("skip me"); |
|
49 |
} |
|
50 |
||
51 |
private void readObject(ObjectInputStream in) |
|
52 |
throws IOException, ClassNotFoundException |
|
53 |
{ |
|
54 |
i = in.readInt(); |
|
55 |
f = in.readFloat(); |
|
56 |
} |
|
57 |
||
58 |
public boolean equals(Object obj) { |
|
59 |
if (! (obj instanceof MismatchedRead)) |
|
60 |
return false; |
|
61 |
MismatchedRead other = (MismatchedRead) obj; |
|
62 |
return (i == other.i && f == other.f); |
|
63 |
} |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
64 |
|
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
65 |
public int hashCode() { |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
66 |
return i; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
67 |
} |
2 | 68 |
} |
69 |
||
70 |
class MismatchedReadExternal implements Externalizable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
71 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
72 |
|
2 | 73 |
int i; |
74 |
float f; |
|
75 |
||
76 |
public MismatchedReadExternal() { |
|
77 |
this(0, (float) 0.0); |
|
78 |
} |
|
79 |
||
80 |
MismatchedReadExternal(int i, float f) { |
|
81 |
this.i = i; |
|
82 |
this.f = f; |
|
83 |
} |
|
84 |
||
85 |
public void writeExternal(ObjectOutput out) throws IOException { |
|
86 |
out.writeInt(i); |
|
87 |
out.writeFloat(f); |
|
88 |
out.writeUTF("skip another"); |
|
89 |
} |
|
90 |
||
91 |
public void readExternal(ObjectInput in) throws IOException { |
|
92 |
i = in.readInt(); |
|
93 |
f = in.readFloat(); |
|
94 |
} |
|
95 |
||
96 |
public boolean equals(Object obj) { |
|
97 |
if (! (obj instanceof MismatchedReadExternal)) |
|
98 |
return false; |
|
99 |
MismatchedReadExternal other = (MismatchedReadExternal) obj; |
|
100 |
return (i == other.i && f == other.f); |
|
101 |
} |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
102 |
|
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
103 |
public int hashCode() { |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
104 |
return i; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
105 |
} |
2 | 106 |
} |
107 |
||
108 |
class InnocentBystander implements Serializable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
109 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
110 |
|
2 | 111 |
String s; |
112 |
||
113 |
InnocentBystander(String s) { |
|
114 |
this.s = s; |
|
115 |
} |
|
116 |
||
117 |
public boolean equals(Object obj) { |
|
118 |
if (! (obj instanceof InnocentBystander)) |
|
119 |
return false; |
|
120 |
InnocentBystander other = (InnocentBystander) obj; |
|
121 |
if (s != null) |
|
122 |
return s.equals(other.s); |
|
123 |
return (s == other.s); |
|
124 |
} |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
125 |
|
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
126 |
public int hashCode() { |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
127 |
return s.hashCode(); |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
128 |
} |
2 | 129 |
} |
130 |
||
131 |
public class SkipToEndOfBlockData { |
|
132 |
public static void main(String[] args) throws Exception { |
|
133 |
ObjectOutputStream oout; |
|
134 |
ObjectInputStream oin; |
|
135 |
ByteArrayOutputStream bout; |
|
136 |
ByteArrayInputStream bin; |
|
137 |
MismatchedRead mr, mrcopy; |
|
138 |
MismatchedReadExternal mre, mrecopy; |
|
139 |
InnocentBystander ib, ibcopy; |
|
140 |
||
141 |
bout = new ByteArrayOutputStream(); |
|
142 |
oout = new ObjectOutputStream(bout); |
|
143 |
mr = new MismatchedRead(1, (float) 2.34); |
|
144 |
mre = new MismatchedReadExternal(5, (float) 6.78); |
|
145 |
ib = new InnocentBystander("foo"); |
|
146 |
||
147 |
oout.writeObject(mr); |
|
148 |
oout.writeObject(mre); |
|
149 |
oout.writeObject(ib); |
|
150 |
oout.flush(); |
|
151 |
||
152 |
bin = new ByteArrayInputStream(bout.toByteArray()); |
|
153 |
oin = new ObjectInputStream(bin); |
|
154 |
mrcopy = (MismatchedRead) oin.readObject(); |
|
155 |
mrecopy = (MismatchedReadExternal) oin.readObject(); |
|
156 |
ibcopy = (InnocentBystander) oin.readObject(); |
|
157 |
||
158 |
if (! (mr.equals(mrcopy) && mre.equals(mrecopy) && ib.equals(ibcopy))) |
|
159 |
throw new Error("copy not equal to original"); |
|
160 |
} |
|
161 |
} |