test/jdk/java/io/FileInputStream/LargeFileAvailable.java
author tschatzl
Wed, 24 Jul 2019 11:49:39 +0200
changeset 57508 28ab01c06755
parent 47216 71c04702a3d5
permissions -rw-r--r--
8228388: Add information about dirty/skipped card for Merge HCC in G1 log Summary: Collect and print informatio about the number of processed cards during the Merge HCC phase to improve log output. Reviewed-by: kbarrett, sangheki
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
     1
/*
44266
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
     2
 * Copyright (c) 2010, 2017, Oracle and/or its affiliates. All rights reserved.
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
     4
 *
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
     8
 *
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    13
 * accompanied this code).
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    14
 *
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    18
 *
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    21
 * questions.
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    22
 */
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    23
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    24
/*
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    25
 * @test
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
    26
 * @bug 6402006 7030573 8011136
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    27
 * @summary Test if available returns correct value when reading
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    28
 *          a large file.
44266
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
    29
 * @run main/timeout=300 LargeFileAvailable
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    30
 */
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    31
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    32
import java.io.*;
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    33
import java.nio.ByteBuffer;
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    34
import java.nio.channels.*;
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7519
diff changeset
    35
import java.nio.file.Files;
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    36
import static java.nio.file.StandardOpenOption.*;
44266
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
    37
import java.util.concurrent.TimeUnit;
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    38
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    39
public class LargeFileAvailable {
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    40
    public static void main(String args[]) throws Exception {
11511
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    41
        // Create a temporary file in the current directory.
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    42
        // Use it to check if we have 7G available for
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    43
        // a large sparse file test. As a fallback use whatever
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    44
        // space is available, so the test can proceed.
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    45
        File file = File.createTempFile("largefile", null, new File("."));
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    46
        long spaceavailable = file.getUsableSpace();
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    47
        long filesize = Math.min(spaceavailable,  7405576182L);
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    48
        if (spaceavailable == 0L) {
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    49
            // A full disk is considered fatal.
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    50
            throw new RuntimeException("No space available for temp file.");
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    51
        }
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    52
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    53
        createLargeFile(filesize, file);
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    54
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    55
        try (FileInputStream fis = new FileInputStream(file)) {
11511
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    56
            if (file.length() != filesize) {
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    57
                throw new RuntimeException("unexpected file size = "
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    58
                                           + file.length());
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    59
            }
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    60
11511
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    61
            long bigSkip = Math.min(filesize/2, 3110608882L);
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    62
            long remaining = filesize;
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    63
            remaining -= skipBytes(fis, bigSkip, remaining);
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    64
            remaining -= skipBytes(fis, 10L, remaining);
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    65
            remaining -= skipBytes(fis, bigSkip, remaining);
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
    66
            int expected = (remaining >= Integer.MAX_VALUE)
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
    67
                           ? Integer.MAX_VALUE
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
    68
                           : (remaining > 0 ? (int) remaining : 0);
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
    69
            if (fis.available() != expected) {
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
    70
                throw new RuntimeException("available() returns "
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
    71
                        + fis.available() + " but expected " + expected);
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    72
            }
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    73
        } finally {
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    74
            file.delete();
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    75
        }
43817
bc04ec6a69e2 8175042: Add success message to java/io/FileInputStream/LargeFileAvailable.java
bpb
parents: 17691
diff changeset
    76
bc04ec6a69e2 8175042: Add success message to java/io/FileInputStream/LargeFileAvailable.java
bpb
parents: 17691
diff changeset
    77
        System.out.println("Test succeeded.");
bc04ec6a69e2 8175042: Add success message to java/io/FileInputStream/LargeFileAvailable.java
bpb
parents: 17691
diff changeset
    78
        System.out.flush();
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    79
    }
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    80
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    81
    // Skip toSkip number of bytes and expect that the available() method
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    82
    // returns avail number of bytes.
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    83
    private static long skipBytes(InputStream is, long toSkip, long avail)
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    84
            throws IOException {
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    85
        long skip = is.skip(toSkip);
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    86
        if (skip != toSkip) {
11511
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    87
            throw new RuntimeException("skip() returns " + skip
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
    88
                                       + " but expected " + toSkip);
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    89
        }
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    90
        long remaining = avail - skip;
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
    91
        int expected = (remaining >= Integer.MAX_VALUE)
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
    92
                       ? Integer.MAX_VALUE
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
    93
                       : (remaining > 0 ? (int) remaining : 0);
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    94
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
    95
        System.out.println("Skipped " + skip + " bytes, available() returns "
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
    96
                           + expected + ", remaining " + remaining);
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
    97
        if (is.available() != expected) {
11511
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
    98
            throw new RuntimeException("available() returns "
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
    99
                    + is.available() + " but expected " + expected);
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
   100
        }
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
   101
        return skip;
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
   102
    }
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
   103
11511
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
   104
    private static void createLargeFile(long filesize,
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
   105
                                        File file) throws Exception {
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
   106
        // Recreate a large file as a sparse file if possible
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
   107
        Files.delete(file.toPath());
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
   108
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
   109
        try (FileChannel fc =
11511
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
   110
             FileChannel.open(file.toPath(),
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
   111
                              CREATE_NEW, WRITE, SPARSE)) {
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
   112
            ByteBuffer bb = ByteBuffer.allocate(1).put((byte)1);
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
   113
            bb.rewind();
44266
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
   114
            System.out.println("  Writing large file...");
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
   115
            long t0 = System.nanoTime();
11511
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
   116
            int rc = fc.write(bb, filesize - 1);
44266
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
   117
            long t1 = System.nanoTime();
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
   118
            System.out.printf("  Wrote large file in %d ns (%d ms) %n",
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
   119
                t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
11511
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
   120
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
   121
            if (rc != 1) {
11511
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
   122
                throw new RuntimeException("Failed to write 1 byte"
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
   123
                                           + " to the large file");
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
   124
            }
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
   125
        }
11511
7981cc53b30a 7030573: test/java/io/FileInputStream/LargeFileAvailable.java fails when there is insufficient disk space
gadams
parents: 9035
diff changeset
   126
        return;
7519
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
   127
    }
1488bb354081 6402006: FileInputStream.available() returns negative values when reading a large file
mchung
parents:
diff changeset
   128
}