test/jdk/java/io/InputStream/Skip.java
author dholmes
Wed, 06 Nov 2019 21:18:42 -0500
changeset 58956 9a0a5e70eeb2
parent 52860 8dd8965df7f6
permissions -rw-r--r--
8233454: Test fails with assert(!is_init_completed(), "should only happen during init") after JDK-8229516 Reviewed-by: jiefu, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    24
/**
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    25
 * @test
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    26
 * @bug 4016710 6516099
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    27
 * @summary check for correct implementation of InputStream.skip{NBytes}
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    28
 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    30
import java.io.EOFException;
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    31
import java.io.InputStream;
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    32
import java.io.IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    34
public class Skip {
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    35
    private static final int EOF = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    37
    private static void dotest(InputStream in, int curpos, long total,
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    38
                               long toskip, long expected) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
            System.err.println("\n\nCurrently at pos = " + curpos +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                               "\nTotal bytes in the Stream = " + total +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                               "\nNumber of bytes to skip = " + toskip +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                               "\nNumber of bytes that should be skipped = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                               expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            long skipped = in.skip(toskip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            System.err.println("actual number skipped: "+ skipped);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            if ((skipped < 0) || (skipped > expected)) {
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    51
                throw new RuntimeException("Unexpected byte count skipped");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        } catch (IOException e) {
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    54
            System.err.println("IOException is thrown: " + e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        } catch (Throwable e) {
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    56
            throw new RuntimeException("Unexpected " + e + " is thrown!");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    58
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    60
    private static void dotestExact(MyInputStream in, long curpos, long total,
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    61
        long toskip, boolean expectIOE, boolean expectEOFE) {
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    62
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    63
        System.err.println("\n\nCurrently at pos = " + curpos +
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    64
                           "\nTotal bytes in the Stream = " + total +
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    65
                           "\nNumber of bytes to skip = " + toskip);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    66
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    67
        try {
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    68
            long pos = in.position();
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    69
            assert pos == curpos : pos + " != " + curpos;
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    70
            in.skipNBytes(toskip);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    71
            if (in.position() != pos + (toskip < 0 ? 0 : toskip)) {
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    72
                throw new RuntimeException((in.position() - pos) +
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    73
                    " bytes skipped; expected " + toskip);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    74
            }
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    75
        } catch (EOFException eofe) {
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    76
            if (!expectEOFE) {
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    77
                throw new RuntimeException("Unexpected EOFException", eofe);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    78
            }
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    79
            System.err.println("Caught expected EOFException");
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    80
        } catch (IOException ioe) {
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    81
            if (!expectIOE) {
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    82
                throw new RuntimeException("Unexpected IOException", ioe);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    83
            }
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    84
            System.err.println("Caught expected IOException");
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    85
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public static void main( String argv[] ) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        MyInputStream in = new MyInputStream(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    91
        // test for negative skip
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        dotest(in,  0, 11, -23,  0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    94
        // check for skip beyond EOF starting from before EOF
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        dotest(in,  0, 11,  20, 11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    97
        // check for skip after EOF
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
    98
        dotest(in, EOF, 11,  20,  0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        in = new MyInputStream(9000);
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   101
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   102
        // check for skip equal to the read chunk size in InputStream.java
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        dotest(in,  0, 9000, 2048, 2048);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   105
        // check for skip larger than the read chunk size in InputStream.java
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        dotest(in, 2048, 9000, 5000, 5000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   108
        // check for skip beyond EOF starting from before EOF
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        dotest(in, 7048, 9000, 5000, 1952);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        in = new MyInputStream(5000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   113
        // check for multiple chunk reads
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        dotest(in, 0, 5000, 6000, 5000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         * check for skip larger than Integer.MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
         * (Takes about 2 hrs on a sparc ultra-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         * long total = (long)Integer.MAX_VALUE + (long)10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
         * long toskip = total - (long)6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         * in = new MyInputStream(total);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
         * dotest(in, 0, total, toskip, toskip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   125
        // tests for skipping an exact number of bytes
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   126
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   127
        final long streamLength = Long.MAX_VALUE;
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   128
        in = new MyInputStream(streamLength);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   129
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   130
        // negative skip: OK
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   131
        dotestExact(in, 0, streamLength, -1, false, false);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   132
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   133
        // negative skip at EOF: OK
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   134
        in.position(streamLength);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   135
        dotestExact(in, streamLength, streamLength, -1, false, false);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   136
        in.position(0);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   137
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   138
        // zero skip: OK
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   139
        dotestExact(in, 0, streamLength, 0, false, false);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   140
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   141
        // zero skip at EOF: OK
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   142
        in.position(streamLength);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   143
        dotestExact(in, streamLength, streamLength, 0, false, false);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   144
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   145
        // skip(1) at EOF: EOFE
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   146
        dotestExact(in, streamLength, streamLength, 1, false, true);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   147
        in.position(0);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   148
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   149
        final long n = 31; // skip count
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   150
        long pos = 0;
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   151
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   152
        // skip(n) returns negative value: IOE
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   153
        in.setState(-1, 100);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   154
        dotestExact(in, pos, streamLength, n, true, false);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   155
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   156
        // skip(n) returns n + 1: IOE
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   157
        in.setState(n + 1, 100);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   158
        dotestExact(in, pos, streamLength, n, true, false);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   159
        pos += n + 1;
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   160
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   161
        // skip(n) returns n/2 but only n/4 subsequent reads succeed: EOFE
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   162
        in.setState(n/2, n/2 + n/4);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   163
        dotestExact(in, pos, streamLength, n, false, true);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   164
        pos += n/2 + n/4;
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   165
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   166
        // skip(n) returns n/2 but n - n/2 subsequent reads succeed: OK
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   167
        in.setState(n/2, n);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   168
        dotestExact(in, pos, streamLength, n, false, false);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   169
        pos += n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
class MyInputStream extends InputStream {
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   174
    private static final int EOF = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   176
    private final long endoffile;
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   177
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   178
    private long readctr = 0;
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   179
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   180
    private boolean isStateSet = false;
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   181
    private long skipReturn;
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   182
    private long readLimit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public MyInputStream(long endoffile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        this.endoffile = endoffile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   188
    /**
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   189
     * Limits the behavior of skip() and read().
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   190
     *
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   191
     * @param skipReturn the value to be returned by skip()
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   192
     * @param maxReads   the maximum number of reads past the current position
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   193
     *                   before EOF is reached
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   194
     */
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   195
    public void setState(long skipReturn, long maxReads) {
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   196
        this.skipReturn = skipReturn;
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   197
        this.readLimit = readctr + maxReads;
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   198
        isStateSet = true;
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   199
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   201
    public int read() {
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   202
        if (readctr == endoffile ||
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   203
            (isStateSet && readctr >= readLimit)) {
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   204
            return EOF;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            readctr++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public int available() { return 0; }
52860
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   213
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   214
    public long position() { return readctr; }
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   215
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   216
    public void position(long pos) {
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   217
        readctr = pos < 0 ? 0 : Math.min(pos, endoffile);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   218
    }
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   219
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   220
    public long skip(long n) throws IOException {
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   221
        if (isStateSet) {
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   222
            return skipReturn < 0 ? skipReturn : super.skip(skipReturn);
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   223
        }
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   224
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   225
        // InputStream skip implementation.
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   226
        return super.skip(n); // readctr is implicitly incremented
8dd8965df7f6 6516099: InputStream.skipFully(int k) to skip exactly k bytes
bpb
parents: 47216
diff changeset
   227
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
}