author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 42338 | jdk/test/java/io/ObjectInputStream/PeekInputStreamTest.java@a60f280f803c |
permissions | -rw-r--r-- |
28116
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
1 |
/* |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
4 |
* |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
8 |
* |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
13 |
* accompanied this code). |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
14 |
* |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
18 |
* |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
21 |
* questions. |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
22 |
*/ |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
23 |
|
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
24 |
import java.io.ByteArrayInputStream; |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
25 |
import java.io.IOException; |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
26 |
import java.io.InputStream; |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
27 |
import java.lang.reflect.Constructor; |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
28 |
import java.lang.reflect.Method; |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
29 |
|
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
30 |
/* |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
31 |
* @test |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
32 |
* @bug 8067870 |
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
28116
diff
changeset
|
33 |
* @modules java.base/java.io:open |
28116
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
34 |
* @summary verifies java.io.ObjectInputStream.PeekInputStream.skip works |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
35 |
* as intended |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
36 |
*/ |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
37 |
public class PeekInputStreamTest { |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
38 |
|
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
39 |
public static void main(String[] args) throws ReflectiveOperationException, |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
40 |
IOException { |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
41 |
|
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
42 |
InputStream pin = createPeekInputStream( |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
43 |
new ByteArrayInputStream(new byte[]{1, 2, 3, 4})); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
44 |
peek(pin); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
45 |
if (pin.skip(1) != 1 || pin.read() != 2) |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
46 |
throw new AssertionError(); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
47 |
|
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
48 |
InputStream pin1 = createPeekInputStream( |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
49 |
new ByteArrayInputStream(new byte[]{1, 2, 3, 4})); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
50 |
if (pin1.skip(1) != 1 || pin1.read() != 2) |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
51 |
throw new AssertionError(); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
52 |
|
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
53 |
InputStream pin2 = createPeekInputStream( |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
54 |
new ByteArrayInputStream(new byte[]{1, 2, 3, 4})); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
55 |
if (pin2.skip(0) != 0 || pin2.read() != 1) |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
56 |
throw new AssertionError(); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
57 |
|
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
58 |
InputStream pin3 = createPeekInputStream( |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
59 |
new ByteArrayInputStream(new byte[]{1, 2, 3, 4})); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
60 |
if (pin3.skip(2) != 2 || pin3.read() != 3) |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
61 |
throw new AssertionError(); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
62 |
|
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
63 |
InputStream pin4 = createPeekInputStream( |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
64 |
new ByteArrayInputStream(new byte[]{1, 2, 3, 4})); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
65 |
if (pin4.skip(3) != 3 || pin4.read() != 4) |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
66 |
throw new AssertionError(); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
67 |
|
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
68 |
InputStream pin5 = createPeekInputStream( |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
69 |
new ByteArrayInputStream(new byte[]{1, 2, 3, 4})); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
70 |
if (pin5.skip(16) != 4 || pin5.read() != -1) |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
71 |
throw new AssertionError(); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
72 |
} |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
73 |
|
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
74 |
private static InputStream createPeekInputStream(InputStream underlying) |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
75 |
throws ReflectiveOperationException { |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
76 |
Class<? extends InputStream> clazz = |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
77 |
Class.forName("java.io.ObjectInputStream$PeekInputStream") |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
78 |
.asSubclass(InputStream.class); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
79 |
|
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
80 |
Constructor<? extends InputStream> ctr = |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
81 |
clazz.getDeclaredConstructor(InputStream.class); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
82 |
ctr.setAccessible(true); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
83 |
return ctr.newInstance(underlying); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
84 |
} |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
85 |
|
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
86 |
private static void peek(InputStream pin) |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
87 |
throws ReflectiveOperationException { |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
88 |
Method p = pin.getClass().getDeclaredMethod("peek"); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
89 |
p.setAccessible(true); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
90 |
p.invoke(pin); |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
91 |
} |
71d09d4e1ca3
8067870: Fix java.io.ObjectInputStream.PeekInputStream#skip
prappo
parents:
diff
changeset
|
92 |
} |