Path: blob/master/test/jdk/java/io/InputStream/Skip.java
41149 views
/*1* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 4016710 651609926* @summary check for correct implementation of InputStream.skip{NBytes}27*/2829import java.io.EOFException;30import java.io.InputStream;31import java.io.IOException;3233public class Skip {34private static final int EOF = -1;3536private static void dotest(InputStream in, int curpos, long total,37long toskip, long expected) throws Exception {38try {39System.err.println("\n\nCurrently at pos = " + curpos +40"\nTotal bytes in the Stream = " + total +41"\nNumber of bytes to skip = " + toskip +42"\nNumber of bytes that should be skipped = " +43expected);4445long skipped = in.skip(toskip);4647System.err.println("actual number skipped: "+ skipped);4849if ((skipped < 0) || (skipped > expected)) {50throw new RuntimeException("Unexpected byte count skipped");51}52} catch (IOException e) {53System.err.println("IOException is thrown: " + e);54} catch (Throwable e) {55throw new RuntimeException("Unexpected " + e + " is thrown!");56}57}5859private static void dotestExact(MyInputStream in, long curpos, long total,60long toskip, boolean expectIOE, boolean expectEOFE) {6162System.err.println("\n\nCurrently at pos = " + curpos +63"\nTotal bytes in the Stream = " + total +64"\nNumber of bytes to skip = " + toskip);6566try {67long pos = in.position();68assert pos == curpos : pos + " != " + curpos;69in.skipNBytes(toskip);70if (in.position() != pos + (toskip < 0 ? 0 : toskip)) {71throw new RuntimeException((in.position() - pos) +72" bytes skipped; expected " + toskip);73}74} catch (EOFException eofe) {75if (!expectEOFE) {76throw new RuntimeException("Unexpected EOFException", eofe);77}78System.err.println("Caught expected EOFException");79} catch (IOException ioe) {80if (!expectIOE) {81throw new RuntimeException("Unexpected IOException", ioe);82}83System.err.println("Caught expected IOException");84}85}8687public static void main( String argv[] ) throws Exception {88MyInputStream in = new MyInputStream(11);8990// test for negative skip91dotest(in, 0, 11, -23, 0);9293// check for skip beyond EOF starting from before EOF94dotest(in, 0, 11, 20, 11);9596// check for skip after EOF97dotest(in, EOF, 11, 20, 0);9899in = new MyInputStream(9000);100101// check for skip equal to the read chunk size in InputStream.java102dotest(in, 0, 9000, 2048, 2048);103104// check for skip larger than the read chunk size in InputStream.java105dotest(in, 2048, 9000, 5000, 5000);106107// check for skip beyond EOF starting from before EOF108dotest(in, 7048, 9000, 5000, 1952);109110in = new MyInputStream(5000);111112// check for multiple chunk reads113dotest(in, 0, 5000, 6000, 5000);114115/*116* check for skip larger than Integer.MAX_VALUE117* (Takes about 2 hrs on a sparc ultra-1)118* long total = (long)Integer.MAX_VALUE + (long)10;119* long toskip = total - (long)6;120* in = new MyInputStream(total);121* dotest(in, 0, total, toskip, toskip);122*/123124// tests for skipping an exact number of bytes125126final long streamLength = Long.MAX_VALUE;127in = new MyInputStream(streamLength);128129// negative skip: OK130dotestExact(in, 0, streamLength, -1, false, false);131132// negative skip at EOF: OK133in.position(streamLength);134dotestExact(in, streamLength, streamLength, -1, false, false);135in.position(0);136137// zero skip: OK138dotestExact(in, 0, streamLength, 0, false, false);139140// zero skip at EOF: OK141in.position(streamLength);142dotestExact(in, streamLength, streamLength, 0, false, false);143144// skip(1) at EOF: EOFE145dotestExact(in, streamLength, streamLength, 1, false, true);146in.position(0);147148final long n = 31; // skip count149long pos = 0;150151// skip(n) returns negative value: IOE152in.setState(-1, 100);153dotestExact(in, pos, streamLength, n, true, false);154155// skip(n) returns n + 1: IOE156in.setState(n + 1, 100);157dotestExact(in, pos, streamLength, n, true, false);158pos += n + 1;159160// skip(n) returns n/2 but only n/4 subsequent reads succeed: EOFE161in.setState(n/2, n/2 + n/4);162dotestExact(in, pos, streamLength, n, false, true);163pos += n/2 + n/4;164165// skip(n) returns n/2 but n - n/2 subsequent reads succeed: OK166in.setState(n/2, n);167dotestExact(in, pos, streamLength, n, false, false);168pos += n;169}170}171172class MyInputStream extends InputStream {173private static final int EOF = -1;174175private final long endoffile;176177private long readctr = 0;178179private boolean isStateSet = false;180private long skipReturn;181private long readLimit;182183public MyInputStream(long endoffile) {184this.endoffile = endoffile;185}186187/**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 position192* before EOF is reached193*/194public void setState(long skipReturn, long maxReads) {195this.skipReturn = skipReturn;196this.readLimit = readctr + maxReads;197isStateSet = true;198}199200public int read() {201if (readctr == endoffile ||202(isStateSet && readctr >= readLimit)) {203return EOF;204}205else {206readctr++;207return 0;208}209}210211public int available() { return 0; }212213public long position() { return readctr; }214215public void position(long pos) {216readctr = pos < 0 ? 0 : Math.min(pos, endoffile);217}218219public long skip(long n) throws IOException {220if (isStateSet) {221return skipReturn < 0 ? skipReturn : super.skip(skipReturn);222}223224// InputStream skip implementation.225return super.skip(n); // readctr is implicitly incremented226}227}228229230