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 4016710 27 @summary check for correct implementation of InputStream.skip 28 */ 29 30 import java.io.*; 31 32 33 public class Skip{ 34 35 private static void dotest(InputStream 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 MyInputStream in = 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 in = new MyInputStream(9000); 78 /* check for skip equal to the read chunk size in InputStream.java */ 79 dotest(in, 0, 9000, 2048, 2048); 80 81 /* check for skip greater than the read chunk size in InputStream.java */ 82 dotest(in, 2048, 9000, 5000, 5000); 83 84 /* check for skip beyond EOF starting from before EOF */ 85 dotest(in, 7048, 9000, 5000, 1952); 86 87 in = new MyInputStream(5000); 88 89 /* check for multiple chunk reads */ 90 dotest(in, 0, 5000, 6000, 5000); 91 92 /* 93 * check for skip larger than Integer.MAX_VALUE 94 * (Takes about 2 hrs on a sparc ultra-1) 95 * long total = (long)Integer.MAX_VALUE + (long)10; 96 * long toskip = total - (long)6; 97 * in = new MyInputStream(total); 98 * dotest(in, 0, total, toskip, toskip); 99 */ 100 101 } 102 103 } 104 105 class MyInputStream extends InputStream { 106 107 private int readctr = 0; 108 private long endoffile; 109 110 public MyInputStream(long endoffile) { 111 this.endoffile = endoffile; 112 } 113 114 public int read() { 115 116 if (readctr == endoffile) { 117 return -1; 118 } 119 else { 120 readctr++; 121 return 0; 122 } 123 } 124 125 public int available() { return 0; } 126 } | 1 /* 2 * Copyright (c) 1997, 2018 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 4016710 6516099 27 * @summary check for correct implementation of InputStream.skip{NBytes} 28 */ 29 30 import java.io.EOFException; 31 import java.io.InputStream; 32 import java.io.IOException; 33 34 public class Skip { 35 private static final int EOF = -1; 36 37 private static void dotest(InputStream in, int curpos, long total, 38 long toskip, long expected) throws Exception { 39 try { 40 System.err.println("\n\nCurrently at pos = " + curpos + 41 "\nTotal bytes in the Stream = " + total + 42 "\nNumber of bytes to skip = " + toskip + 43 "\nNumber of bytes that should be skipped = " + 44 expected); 45 46 long skipped = in.skip(toskip); 47 48 System.err.println("actual number skipped: "+ skipped); 49 50 if ((skipped < 0) || (skipped > expected)) { 51 throw new RuntimeException("Unexpected byte count skipped"); 52 } 53 } catch (IOException e) { 54 System.err.println("IOException is thrown: " + e); 55 } catch (Throwable e) { 56 throw new RuntimeException("Unexpected " + e + " is thrown!"); 57 } 58 } 59 60 private static void dotestExact(MyInputStream in, int curpos, long total, 61 long toskip) throws Exception { 62 try { 63 System.err.println("\n\nCurrently at pos = " + curpos + 64 "\nTotal bytes in the Stream = " + total + 65 "\nNumber of bytes to skip = " + toskip); 66 67 try { 68 long pos = in.position(); 69 assert pos == curpos : pos + " != " + curpos; 70 in.skipNBytes(toskip); 71 if (in.position() != pos + (toskip < 0 ? 0 : toskip)) { 72 throw new RuntimeException((in.position() - pos) + 73 " bytes skipped; expected " + toskip); 74 } 75 } catch (EOFException eofe) { 76 if (!(toskip > 0 && 77 (curpos == EOF || (curpos + toskip > total)))) { 78 throw new RuntimeException("Unexpected EOFException", eofe); 79 } 80 81 System.err.println("Caught expected EOFException"); 82 83 return; 84 } 85 } catch (IOException e) { 86 System.err.println("IOException is thrown: " + e); 87 } catch (Throwable e) { 88 throw new RuntimeException("Unexpected " + e + " is thrown!"); 89 } 90 } 91 92 public static void main( String argv[] ) throws Exception { 93 MyInputStream in = new MyInputStream(false, 11); 94 95 /* test for negative skip */ 96 dotest(in, 0, 11, -23, 0); 97 98 /* check for skip beyond EOF starting from before EOF */ 99 dotest(in, 0, 11, 20, 11); 100 101 /* check for skip after EOF */ 102 dotest(in, EOF, 11, 20, 0); 103 104 in = new MyInputStream(false, 9000); 105 106 /* check for skip equal to the read chunk size in InputStream.java */ 107 dotest(in, 0, 9000, 2048, 2048); 108 109 /* check for skip larger than the read chunk size in InputStream.java */ 110 dotest(in, 2048, 9000, 5000, 5000); 111 112 /* check for skip beyond EOF starting from before EOF */ 113 dotest(in, 7048, 9000, 5000, 1952); 114 115 in = new MyInputStream(false, 5000); 116 117 /* check for multiple chunk reads */ 118 dotest(in, 0, 5000, 6000, 5000); 119 120 /* 121 * check for skip larger than Integer.MAX_VALUE 122 * (Takes about 2 hrs on a sparc ultra-1) 123 * long total = (long)Integer.MAX_VALUE + (long)10; 124 * long toskip = total - (long)6; 125 * in = new MyInputStream(total); 126 * dotest(in, 0, total, toskip, toskip); 127 */ 128 129 /* tests for skipping an exact number of bytes */ 130 131 final int length = 16; 132 in = new MyInputStream(true, length); 133 134 /* test for negative skip */ 135 dotestExact(in, 0, length, -23); 136 137 /* test for zero skip */ 138 dotestExact(in, 0, length, 0); 139 140 /* test for skip before EOF in first half of stream */ 141 dotestExact(in, 0, length, length/2); 142 143 /* test for skip before EOF in second half of stream */ 144 dotestExact(in, length/2, length, length/4); 145 146 /* check for skip beyond EOF starting from before EOF */ 147 dotestExact(in, 3*length/4, length, length/2); 148 149 /* check for skip from / after EOF */ 150 dotestExact(in, EOF, 11, 1); 151 } 152 } 153 154 class MyInputStream extends InputStream { 155 private static final int EOF = -1; 156 157 private final boolean hasBadSkip; 158 private final long endoffile; 159 private long readctr = 0; 160 161 public MyInputStream(boolean hasBadSkip, long endoffile) { 162 this.hasBadSkip = hasBadSkip; 163 this.endoffile = endoffile; 164 } 165 166 public int read() { 167 if (readctr == endoffile) { 168 return EOF; 169 } 170 else { 171 readctr++; 172 return 0; 173 } 174 } 175 176 public int available() { return 0; } 177 178 public long position() { return readctr == endoffile ? EOF : readctr; } 179 180 public long skip(long n) throws IOException { 181 if (hasBadSkip && n > 0 && 182 readctr > endoffile / 2 && readctr != endoffile) { 183 long ns = n / 2; 184 System.out.format("Bad skip(%d): position %d, skipped %d%n", 185 n, readctr, ns); 186 n = ns; 187 } 188 189 // InputStream skip implementation. 190 return super.skip(n); 191 } 192 } |