test/jdk/java/io/ObjectInputStream/PeekInputStreamTest.java
changeset 47216 71c04702a3d5
parent 42338 a60f280f803c
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2014, 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 import java.io.ByteArrayInputStream;
       
    25 import java.io.IOException;
       
    26 import java.io.InputStream;
       
    27 import java.lang.reflect.Constructor;
       
    28 import java.lang.reflect.Method;
       
    29 
       
    30 /*
       
    31  * @test
       
    32  * @bug 8067870
       
    33  * @modules java.base/java.io:open
       
    34  * @summary verifies java.io.ObjectInputStream.PeekInputStream.skip works
       
    35  *          as intended
       
    36  */
       
    37 public class PeekInputStreamTest {
       
    38 
       
    39     public static void main(String[] args) throws ReflectiveOperationException,
       
    40             IOException {
       
    41 
       
    42         InputStream pin = createPeekInputStream(
       
    43                 new ByteArrayInputStream(new byte[]{1, 2, 3, 4}));
       
    44         peek(pin);
       
    45         if (pin.skip(1) != 1 || pin.read() != 2)
       
    46             throw new AssertionError();
       
    47 
       
    48         InputStream pin1 = createPeekInputStream(
       
    49                 new ByteArrayInputStream(new byte[]{1, 2, 3, 4}));
       
    50         if (pin1.skip(1) != 1 || pin1.read() != 2)
       
    51             throw new AssertionError();
       
    52 
       
    53         InputStream pin2 = createPeekInputStream(
       
    54                 new ByteArrayInputStream(new byte[]{1, 2, 3, 4}));
       
    55         if (pin2.skip(0) != 0 || pin2.read() != 1)
       
    56             throw new AssertionError();
       
    57 
       
    58         InputStream pin3 = createPeekInputStream(
       
    59                 new ByteArrayInputStream(new byte[]{1, 2, 3, 4}));
       
    60         if (pin3.skip(2) != 2 || pin3.read() != 3)
       
    61             throw new AssertionError();
       
    62 
       
    63         InputStream pin4 = createPeekInputStream(
       
    64                 new ByteArrayInputStream(new byte[]{1, 2, 3, 4}));
       
    65         if (pin4.skip(3) != 3 || pin4.read() != 4)
       
    66             throw new AssertionError();
       
    67 
       
    68         InputStream pin5 = createPeekInputStream(
       
    69                 new ByteArrayInputStream(new byte[]{1, 2, 3, 4}));
       
    70         if (pin5.skip(16) != 4 || pin5.read() != -1)
       
    71             throw new AssertionError();
       
    72     }
       
    73 
       
    74     private static InputStream createPeekInputStream(InputStream underlying)
       
    75             throws ReflectiveOperationException {
       
    76         Class<? extends InputStream> clazz =
       
    77                 Class.forName("java.io.ObjectInputStream$PeekInputStream")
       
    78                         .asSubclass(InputStream.class);
       
    79 
       
    80         Constructor<? extends InputStream> ctr =
       
    81                 clazz.getDeclaredConstructor(InputStream.class);
       
    82         ctr.setAccessible(true);
       
    83         return ctr.newInstance(underlying);
       
    84     }
       
    85 
       
    86     private static void peek(InputStream pin)
       
    87             throws ReflectiveOperationException {
       
    88         Method p = pin.getClass().getDeclaredMethod("peek");
       
    89         p.setAccessible(true);
       
    90         p.invoke(pin);
       
    91     }
       
    92 }