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, long curpos, long total, 61 long toskip, boolean expectIOE, boolean expectEOFE) { 62 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 (!expectEOFE) { 77 throw new RuntimeException("Unexpected EOFException", eofe); 78 } 79 System.err.println("Caught expected EOFException"); 80 } catch (IOException ioe) { 81 if (!expectIOE) { 82 throw new RuntimeException("Unexpected IOException", ioe); 83 } 84 System.err.println("Caught expected IOException"); 85 } 86 } 87 88 public static void main( String argv[] ) throws Exception { 89 MyInputStream in = new MyInputStream(11); 90 91 // test for negative skip 92 dotest(in, 0, 11, -23, 0); 93 94 // check for skip beyond EOF starting from before EOF 95 dotest(in, 0, 11, 20, 11); 96 97 // check for skip after EOF 98 dotest(in, EOF, 11, 20, 0); 99 100 in = new MyInputStream(9000); 101 102 // check for skip equal to the read chunk size in InputStream.java 103 dotest(in, 0, 9000, 2048, 2048); 104 105 // check for skip larger than the read chunk size in InputStream.java 106 dotest(in, 2048, 9000, 5000, 5000); 107 108 // check for skip beyond EOF starting from before EOF 109 dotest(in, 7048, 9000, 5000, 1952); 110 111 in = new MyInputStream(5000); 112 113 // check for multiple chunk reads 114 dotest(in, 0, 5000, 6000, 5000); 115 116 /* 117 * check for skip larger than Integer.MAX_VALUE 118 * (Takes about 2 hrs on a sparc ultra-1) 119 * long total = (long)Integer.MAX_VALUE + (long)10; 120 * long toskip = total - (long)6; 121 * in = new MyInputStream(total); 122 * dotest(in, 0, total, toskip, toskip); 123 */ 124 125 // tests for skipping an exact number of bytes 126 127 final long streamLength = Long.MAX_VALUE; 128 in = new MyInputStream(streamLength); 129 130 // negative skip: OK 131 dotestExact(in, 0, streamLength, -1, false, false); 132 133 // negative skip at EOF: OK 134 in.position(streamLength); 135 dotestExact(in, streamLength, streamLength, -1, false, false); 136 in.position(0); 137 138 // zero skip: OK 139 dotestExact(in, 0, streamLength, 0, false, false); 140 141 // zero skip at EOF: OK 142 in.position(streamLength); 143 dotestExact(in, streamLength, streamLength, 0, false, false); 144 145 // skip(1) at EOF: EOFE 146 dotestExact(in, streamLength, streamLength, 1, false, true); 147 in.position(0); 148 149 final long n = 31; // skip count 150 long pos = 0; 151 152 // skip(n) returns negative value: IOE 153 in.setState(-1, 100); 154 dotestExact(in, pos, streamLength, n, true, false); 155 156 // skip(n) returns n + 1: IOE 157 in.setState(n + 1, 100); 158 dotestExact(in, pos, streamLength, n, true, false); 159 160 // skip(n) returns n/2 but only n/4 subsequent reads succeed: EOFE 161 in.setState(n/2, n/2 + n/4); 162 dotestExact(in, pos, streamLength, n, false, true); 163 pos += n/2 + n/4; 164 165 // skip(n) returns n/2 but n - n/2 subsequent reads succeed: OK 166 in.setState(n/2, n); 167 dotestExact(in, pos, streamLength, n, false, false); 168 pos += n; 169 } 170 } 171 172 class MyInputStream extends InputStream { 173 private static final int EOF = -1; 174 175 private final long endoffile; 176 177 private long readctr = 0; 178 179 private boolean isStateSet = false; 180 private long skipReturn; 181 private long readLimit; 182 183 public MyInputStream(long endoffile) { 184 this.endoffile = endoffile; 185 } 186 187 /** 188 * Limits the behavior of skip() and read(). 189 * 190 * @param skipReturn the value to be returned by skip() 191 * @param maxReads the maximum number of reads past the current position 192 * before EOF is reached 193 */ 194 public void setState(long skipReturn, long maxReads) { 195 this.skipReturn = skipReturn; 196 this.readLimit = readctr + maxReads; 197 isStateSet = true; 198 } 199 200 public int read() { 201 if (readctr == endoffile || 202 (isStateSet && readctr >= readLimit)) { 203 return EOF; 204 } 205 else { 206 readctr++; 207 return 0; 208 } 209 } 210 211 public int available() { return 0; } 212 213 public long position() { return readctr == endoffile ? EOF : readctr; } 214 215 public void position(long pos) { 216 readctr = pos < 0 ? 0 : Math.min(pos, endoffile); 217 } 218 219 public long skip(long n) throws IOException { 220 if (isStateSet) { 221 return super.skip(skipReturn); 222 } 223 224 // InputStream skip implementation. 225 return super.skip(n); // readctr is implicitly incremented 226 } 227 }