jdk/test/java/io/ByteArrayInputStream/Skip.java
author dsamersoff
Sun, 03 Feb 2013 22:28:08 +0400
changeset 15460 ac08a4bdd0f6
parent 7164 f0262c6da2d1
permissions -rw-r--r--
8002048: Protocol to discovery of manageable Java processes on a network Summary: Introduce a protocol to discover manageble Java instances across a network subnet, JDP Reviewed-by: sla, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7164
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
     1
/*
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
     4
 *
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
     8
 *
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    13
 * accompanied this code).
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    14
 *
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    18
 *
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    21
 * questions.
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    22
 */
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    23
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    24
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    25
/* @test
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    26
 * @bug 6720170
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    27
 * @summary check for ByteArrayInputStream.skip
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    28
 */
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    29
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    30
import java.io.*;
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    31
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    32
public class Skip {
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    33
    private static void dotest(InputStream in, int curpos, long total,
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    34
                               long toskip, long expected)
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    35
        throws Exception
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    36
    {
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    37
        System.err.println("\nCurrently at pos = " + curpos +
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    38
                           "\nTotal bytes in the stream = " + total +
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    39
                           "\nNumber of bytes to skip = " + toskip +
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    40
                           "\nNumber of bytes that should be skipped = " +
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    41
                           expected);
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    42
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    43
        // position to curpos; EOF if negative
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    44
        in.reset();
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    45
        int avail = curpos >= 0 ? curpos : in.available();
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    46
        long n = in.skip(avail);
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    47
        if (n != avail) {
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    48
            throw new RuntimeException("Unexpected number of bytes skipped = " + n);
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    49
        }
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    50
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    51
        long skipped = in.skip(toskip);
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    52
        System.err.println("actual number skipped: "+ skipped);
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    53
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    54
        if (skipped != expected) {
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    55
            throw new RuntimeException("Unexpected number of bytes skipped = " + skipped);
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    56
        }
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    57
    }
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    58
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    59
    public static void main(String argv[]) throws Exception {
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    60
        int total = 1024;
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    61
        ByteArrayInputStream in = new ByteArrayInputStream(new byte[total]);
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    62
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    63
        /* test for skip */
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    64
        dotest(in,  0, total, 23, 23);
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    65
        dotest(in,  10, total, 23, 23);
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    66
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    67
        /* test for negative skip */
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    68
        dotest(in,  0, total, -23,  0);
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    69
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    70
        /* check for skip after EOF */
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    71
        dotest(in, -1, total,  20,  0);
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    72
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    73
        /* check for skip beyond EOF starting from before EOF */
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    74
        dotest(in,  0, total, total+20, total);
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    75
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    76
        /* check for skip if the pos + toskip causes integer overflow */
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    77
        dotest(in, 10, total, Long.MAX_VALUE, total-10);
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    78
    }
f0262c6da2d1 6720170: ByteArrayInputStream.skip(long) can overflow internally
mchung
parents:
diff changeset
    79
}