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 }