< prev index next >

test/jdk/java/io/InputStream/Skip.java

Print this page


   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(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(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(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         in = new MyInputStream(11);
 132 
 133         /* test for negative skip */
 134         dotestExact(in,  0, 11, -23);
 135 
 136         /* test for zero skip */
 137         dotestExact(in,  0, 11, 0);
 138 
 139         /* test for skip before EOF */
 140         dotestExact(in,  0, 11, 5);
 141 
 142         /* check for skip beyond EOF starting from before EOF */
 143         dotestExact(in,  5, 11, 7);
 144 
 145         /* check for skip from / after EOF */
 146         dotestExact(in, EOF, 11, 1);
 147     }
 148 }
 149 
 150 class MyInputStream extends InputStream {
 151     private static final int EOF = -1;
 152 
 153     private long readctr = 0;
 154     private long endoffile;
 155 
 156     public MyInputStream(long endoffile) {
 157         this.endoffile = endoffile;
 158     }
 159 
 160     public int read() {

 161         if (readctr == endoffile) {
 162             return EOF;
 163         }
 164         else {
 165             readctr++;
 166             return 0;
 167         }
 168     }
 169 
 170     public int available() { return 0; }
 171 
 172     public long position() { return readctr == endoffile ? EOF : readctr; }
 173 }
< prev index next >