Path: blob/master/test/jdk/java/io/LineNumberInputStream/Skip.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*/222324/* @test25@bug 408593926@summary Check for the correct behaviour of LineNumberInputStream.skip27*/2829import java.io.*;303132public class Skip{3334private static void dotest(LineNumberInputStream in , int curpos ,35long total , long toskip , long expected)36throws Exception37{3839try {4041System.err.println("\n\nCurrently at pos = " + curpos +42"\nTotal bytes in the Stream = " + total +43"\nNumber of bytes to skip = " + toskip +44"\nNumber of bytes that should be skipped = " +45expected);4647long skipped = in.skip(toskip);4849System.err.println("actual number skipped: "+ skipped);5051if ((skipped < 0) || (skipped > expected)) {52throw new RuntimeException("Unexpected number of bytes skipped");53}5455} catch (IOException e) {56System.err.println("IOException is thrown - possible result");57} catch (Throwable e) {58throw new RuntimeException("Unexpected "+e+" is thrown!");59}6061}6263public static void main( String argv[] ) throws Exception {6465LineNumberInputStream in = new LineNumberInputStream(new MyInputStream(11));6667/* test for negative skip */68dotest(in, 0, 11, -23, 0);6970/* check for skip beyond EOF starting from before EOF */71dotest(in, 0, 11, 20, 11);7273/* check for skip after EOF */74dotest(in, -1, 11, 20, 0);7576}7778}7980class MyInputStream extends InputStream {8182private int readctr = 0;83private long endoffile;8485public MyInputStream(long endoffile) {86this.endoffile = endoffile;87}8889public int read() {9091if (readctr == endoffile) {92return -1;93}94else {95readctr++;96return 0;97}98}99100public int available() { return 0; }101}102103104