J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 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 4253271 |
| 26 | * @summary Ensure that ObjectInputStream.readObject() is called, it doesn't |
| 27 | * read past the end of the object in the underlying stream. |
| 28 | */ |
| 29 | |
| 30 | import java.io.*; |
| 31 | |
| 32 | class LimitInputStream extends ByteArrayInputStream { |
| 33 | int limit; |
| 34 | |
| 35 | LimitInputStream(byte[] b) { |
| 36 | super(b); |
| 37 | limit = b.length; |
| 38 | } |
| 39 | |
| 40 | public int read() { |
| 41 | if (limit < 1) |
| 42 | throw new Error("limit exceeded"); |
| 43 | int c = super.read(); |
| 44 | if (c != -1) |
| 45 | limit--; |
| 46 | return c; |
| 47 | } |
| 48 | |
| 49 | public int read(byte[] b) { |
| 50 | return read(b, 0, b.length); |
| 51 | } |
| 52 | |
| 53 | public int read(byte[] b, int off, int len) { |
| 54 | if (limit < len) |
| 55 | throw new Error("limit exceeded"); |
| 56 | int n = super.read(b, off, len); |
| 57 | if (n != -1) |
| 58 | limit -= n; |
| 59 | return n; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public class ReadPastObject { |
| 64 | public static void main(String[] args) throws Exception { |
| 65 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 66 | ObjectOutputStream oout = new ObjectOutputStream(bout); |
| 67 | oout.writeObject("foo"); |
| 68 | LimitInputStream lin = new LimitInputStream(bout.toByteArray()); |
| 69 | ObjectInputStream oin = new ObjectInputStream(lin); |
| 70 | System.out.println(oin.readObject()); |
| 71 | } |
| 72 | } |