1/* 2 * Copyright (c) 1997, 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 25/* @test 26 @bug 4085939 27 @summary Check for the correct behaviour of LineNumberInputStream.skip 28 */ 29 30import java.io.*; 31 32 33public class Skip{ 34 35 private static void dotest(LineNumberInputStream in , int curpos , 36 long total , long toskip , long expected) 37 throws Exception 38 { 39 40 try { 41 42 System.err.println("\n\nCurrently at pos = " + curpos + 43 "\nTotal bytes in the Stream = " + total + 44 "\nNumber of bytes to skip = " + toskip + 45 "\nNumber of bytes that should be skipped = " + 46 expected); 47 48 long skipped = in.skip(toskip); 49 50 System.err.println("actual number skipped: "+ skipped); 51 52 if ((skipped < 0) || (skipped > expected)) { 53 throw new RuntimeException("Unexpected number of bytes skipped"); 54 } 55 56 } catch (IOException e) { 57 System.err.println("IOException is thrown - possible result"); 58 } catch (Throwable e) { 59 throw new RuntimeException("Unexpected "+e+" is thrown!"); 60 } 61 62 } 63 64 public static void main( String argv[] ) throws Exception { 65 66 LineNumberInputStream in = new LineNumberInputStream(new MyInputStream(11)); 67 68 /* test for negative skip */ 69 dotest(in, 0, 11, -23, 0); 70 71 /* check for skip beyond EOF starting from before EOF */ 72 dotest(in, 0, 11, 20, 11); 73 74 /* check for skip after EOF */ 75 dotest(in, -1, 11, 20, 0); 76 77 } 78 79} 80 81class MyInputStream extends InputStream { 82 83 private int readctr = 0; 84 private long endoffile; 85 86 public MyInputStream(long endoffile) { 87 this.endoffile = endoffile; 88 } 89 90 public int read() { 91 92 if (readctr == endoffile) { 93 return -1; 94 } 95 else { 96 readctr++; 97 return 0; 98 } 99 } 100 101 public int available() { return 0; } 102} 103