1/* 2 * Copyright (c) 2001, 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 4360508 26 * @summary Verify that a custom readObject() method reading in data written 27 * via default serialization cannot read past the end of the default 28 * data. 29 */ 30 31import java.io.*; 32 33class A implements Serializable { 34 int i1 = 1, i2 = 2; 35 String s1 = "foo", s2 = "bar"; 36 37 private void readObject(ObjectInputStream in) 38 throws IOException, ClassNotFoundException 39 { 40 in.defaultReadObject(); 41 if (in.read() != -1) { 42 throw new Error(); 43 } 44 try { 45 in.readInt(); 46 throw new Error(); 47 } catch (EOFException ex) { 48 } 49 try { 50 in.readObject(); 51 throw new Error(); 52 } catch (OptionalDataException ex) { 53 if (!ex.eof) { 54 throw new Error(); 55 } 56 } 57 try { 58 in.readUnshared(); 59 throw new Error(); 60 } catch (OptionalDataException ex) { 61 if (!ex.eof) { 62 throw new Error(); 63 } 64 } 65 } 66} 67 68class B implements Serializable { 69 int i1 = 1, i2 = 2; 70 String s1 = "foo", s2 = "bar"; 71 72 private void readObject(ObjectInputStream in) 73 throws IOException, ClassNotFoundException 74 { 75 in.readFields(); 76 try { 77 in.readObject(); 78 throw new Error(); 79 } catch (OptionalDataException ex) { 80 if (!ex.eof) { 81 throw new Error(); 82 } 83 } 84 try { 85 in.readUnshared(); 86 throw new Error(); 87 } catch (OptionalDataException ex) { 88 if (!ex.eof) { 89 throw new Error(); 90 } 91 } 92 if (in.read() != -1) { 93 throw new Error(); 94 } 95 try { 96 in.readInt(); 97 throw new Error(); 98 } catch (EOFException ex) { 99 } 100 } 101} 102 103class C implements Serializable { 104 private void readObject(ObjectInputStream in) 105 throws IOException, ClassNotFoundException 106 { 107 in.defaultReadObject(); 108 try { 109 in.readObject(); 110 throw new Error(); 111 } catch (OptionalDataException ex) { 112 if (!ex.eof) { 113 throw new Error(); 114 } 115 } 116 try { 117 in.readUnshared(); 118 throw new Error(); 119 } catch (OptionalDataException ex) { 120 if (!ex.eof) { 121 throw new Error(); 122 } 123 } 124 if (in.read() != -1) { 125 throw new Error(); 126 } 127 try { 128 in.readInt(); 129 throw new Error(); 130 } catch (EOFException ex) { 131 } 132 } 133} 134 135public class DefaultDataEnd { 136 public static void main(String[] args) throws Exception { 137 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 138 ObjectOutputStream oout = new ObjectOutputStream(bout); 139 oout.writeObject(new A()); 140 oout.writeObject(new B()); 141 oout.writeObject(new C()); 142 oout.close(); 143 ObjectInputStream oin = new ObjectInputStream( 144 new ByteArrayInputStream(bout.toByteArray())); 145 oin.readObject(); 146 oin.readObject(); 147 oin.readObject(); 148 } 149} 150