Path: blob/master/test/jdk/java/io/DataInputStream/SkipBytes.java
41149 views
/*1* Copyright (c) 1997, 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*/2223/* @test24@bug 408593825@summary Check for the correct behaviour of DataInputStream.skipBytes26*/2728import java.io.*;2930public class SkipBytes{3132private static void dotest(DataInputStream dis, int pos, int total,33int toskip, int expected) {3435try {36System.err.println("\n\nTotal bytes in the stream = " + total);37System.err.println("Currently at position = " + pos);38System.err.println("Bytes to skip = " + toskip);39System.err.println("Expected result = " + expected);40int skipped = dis.skipBytes(toskip);41System.err.println("Actual skipped = " + skipped);42if (skipped != expected) {43throw new RuntimeException44("DataInputStream.skipBytes does not return expected value");45}46} catch(EOFException e){47throw new RuntimeException48("DataInputStream.skipBytes throws unexpected EOFException");49} catch (IOException e) {50System.err.println("IOException is thrown - possible result");51}525354}55565758public static void main(String args[]) throws Exception {5960DataInputStream dis = new DataInputStream(new MyInputStream());61dotest(dis, 0, 11, -1, 0);62dotest(dis, 0, 11, 5, 5);63System.err.println("\n***CAUTION**** - may go into an infinite loop");64dotest(dis, 5, 11, 20, 6);65}66}676869class MyInputStream extends InputStream {7071private int readctr = 0;7273public int read() {7475if (readctr > 10){76return -1;77}78else{79readctr++;80return 0;81}8283}8485public int available() { return 0; }86}878889