jdk/test/java/io/FileInputStream/NegativeAvailable.java
author jbachorik
Tue, 21 Jan 2014 13:04:55 +0100
changeset 22353 d09e3ff5fd63
parent 17691 13931cd9405f
permissions -rw-r--r--
8032377: test/java/lang/management/ThreadMXBean/ThreadBlockedCount.java still fails intermittently Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16716
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
     1
/*
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
     4
 *
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
     8
 *
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    13
 * accompanied this code).
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    14
 *
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    18
 *
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    21
 * questions.
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    22
 */
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    23
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    24
/*
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    25
 * @test
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 16716
diff changeset
    26
 * @bug 8010837 8011136
16716
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    27
 * @summary Test if available returns correct value when skipping beyond
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    28
 *          the end of a file.
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    29
 * @author Dan Xu
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    30
 */
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    31
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    32
import java.io.BufferedWriter;
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    33
import java.io.File;
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    34
import java.io.FileInputStream;
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    35
import java.io.IOException;
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    36
import java.nio.charset.Charset;
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    37
import java.nio.file.Files;
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    38
import java.nio.file.Path;
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    39
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    40
public class NegativeAvailable {
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    41
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    42
    public static void main(String[] args) throws IOException {
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    43
        final int SIZE = 10;
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    44
        final int SKIP = 5;
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 16716
diff changeset
    45
        final int NEGATIVE_SKIP = -5;
16716
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    46
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    47
        // Create a temporary file with size of 10 bytes.
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    48
        Path tmp = Files.createTempFile(null, null);
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    49
        try (BufferedWriter writer =
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    50
            Files.newBufferedWriter(tmp, Charset.defaultCharset())) {
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    51
            for (int i = 0; i < SIZE; i++) {
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    52
                writer.write('1');
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    53
            }
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    54
        }
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    55
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    56
        File tempFile = tmp.toFile();
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    57
        try (FileInputStream fis = new FileInputStream(tempFile)) {
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    58
            if (tempFile.length() != SIZE) {
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    59
                throw new RuntimeException("unexpected file size = "
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 16716
diff changeset
    60
                                           + tempFile.length());
16716
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    61
            }
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    62
            long space = skipBytes(fis, SKIP, SIZE);
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 16716
diff changeset
    63
            space = skipBytes(fis, NEGATIVE_SKIP, space);
16716
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    64
            space = skipBytes(fis, SKIP, space);
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    65
            space = skipBytes(fis, SKIP, space);
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    66
            space = skipBytes(fis, SKIP, space);
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 16716
diff changeset
    67
            space = skipBytes(fis, NEGATIVE_SKIP, space);
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 16716
diff changeset
    68
            space = skipBytes(fis, NEGATIVE_SKIP, space);
16716
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    69
        }
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    70
        Files.deleteIfExists(tmp);
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    71
    }
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    72
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    73
    /**
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    74
     *  Skip toSkip number of bytes and return the remaining bytes of the file.
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    75
     */
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    76
    private static long skipBytes(FileInputStream fis, int toSkip, long space)
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    77
            throws IOException {
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    78
        long skip = fis.skip(toSkip);
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    79
        if (skip != toSkip) {
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    80
            throw new RuntimeException("skip() returns " + skip
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 16716
diff changeset
    81
                                       + " but expected " + toSkip);
16716
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    82
        }
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 16716
diff changeset
    83
        long newSpace = space - toSkip;
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 16716
diff changeset
    84
        long remaining = newSpace > 0 ? newSpace : 0;
16716
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    85
        int avail = fis.available();
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    86
        if (avail != remaining) {
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    87
            throw new RuntimeException("available() returns " + avail
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 16716
diff changeset
    88
                                       + " but expected " + remaining);
16716
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    89
        }
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    90
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    91
        System.out.println("Skipped " + skip + " bytes "
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 16716
diff changeset
    92
                           + " available() returns " + avail);
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 16716
diff changeset
    93
        return newSpace;
16716
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    94
    }
6d3f9ce0d776 8010837: FileInputStream.available() throw IOException when encountering negative available values
dxu
parents:
diff changeset
    95
}