Path: blob/master/test/jdk/java/io/ObjectInputStream/PeekInputStreamTest.java
41149 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.ByteArrayInputStream;24import java.io.IOException;25import java.io.InputStream;26import java.lang.reflect.Constructor;27import java.lang.reflect.Method;2829/*30* @test31* @bug 806787032* @modules java.base/java.io:open33* @summary verifies java.io.ObjectInputStream.PeekInputStream.skip works34* as intended35*/36public class PeekInputStreamTest {3738public static void main(String[] args) throws ReflectiveOperationException,39IOException {4041InputStream pin = createPeekInputStream(42new ByteArrayInputStream(new byte[]{1, 2, 3, 4}));43peek(pin);44if (pin.skip(1) != 1 || pin.read() != 2)45throw new AssertionError();4647InputStream pin1 = createPeekInputStream(48new ByteArrayInputStream(new byte[]{1, 2, 3, 4}));49if (pin1.skip(1) != 1 || pin1.read() != 2)50throw new AssertionError();5152InputStream pin2 = createPeekInputStream(53new ByteArrayInputStream(new byte[]{1, 2, 3, 4}));54if (pin2.skip(0) != 0 || pin2.read() != 1)55throw new AssertionError();5657InputStream pin3 = createPeekInputStream(58new ByteArrayInputStream(new byte[]{1, 2, 3, 4}));59if (pin3.skip(2) != 2 || pin3.read() != 3)60throw new AssertionError();6162InputStream pin4 = createPeekInputStream(63new ByteArrayInputStream(new byte[]{1, 2, 3, 4}));64if (pin4.skip(3) != 3 || pin4.read() != 4)65throw new AssertionError();6667InputStream pin5 = createPeekInputStream(68new ByteArrayInputStream(new byte[]{1, 2, 3, 4}));69if (pin5.skip(16) != 4 || pin5.read() != -1)70throw new AssertionError();71}7273private static InputStream createPeekInputStream(InputStream underlying)74throws ReflectiveOperationException {75Class<? extends InputStream> clazz =76Class.forName("java.io.ObjectInputStream$PeekInputStream")77.asSubclass(InputStream.class);7879Constructor<? extends InputStream> ctr =80clazz.getDeclaredConstructor(InputStream.class);81ctr.setAccessible(true);82return ctr.newInstance(underlying);83}8485private static void peek(InputStream pin)86throws ReflectiveOperationException {87Method p = pin.getClass().getDeclaredMethod("peek");88p.setAccessible(true);89p.invoke(pin);90}91}929394