1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6402006 7030573 8011136
  27  * @key intermittent
  28  * @summary Test if available returns correct value when reading
  29  *          a large file.
  30  */
  31 
  32 import java.io.*;
  33 import java.nio.ByteBuffer;
  34 import java.nio.channels.*;
  35 import java.nio.file.Files;
  36 import static java.nio.file.StandardOpenOption.*;
  37 
  38 public class LargeFileAvailable {
  39     public static void main(String args[]) throws Exception {
  40         // Create a temporary file in the current directory.
  41         // Use it to check if we have 7G available for
  42         // a large sparse file test. As a fallback use whatever
  43         // space is available, so the test can proceed.
  44         File file = File.createTempFile("largefile", null, new File("."));
  45         long spaceavailable = file.getUsableSpace();
  46         long filesize = Math.min(spaceavailable,  7405576182L);
  47         if (spaceavailable == 0L) {
  48             // A full disk is considered fatal.
  49             throw new RuntimeException("No space available for temp file.");
  50         }
  51 
  52         createLargeFile(filesize, file);
  53 
  54         try (FileInputStream fis = new FileInputStream(file)) {
  55             if (file.length() != filesize) {
  56                 throw new RuntimeException("unexpected file size = "
  57                                            + file.length());
  58             }
  59 
  60             long bigSkip = Math.min(filesize/2, 3110608882L);
  61             long remaining = filesize;
  62             remaining -= skipBytes(fis, bigSkip, remaining);
  63             remaining -= skipBytes(fis, 10L, remaining);
  64             remaining -= skipBytes(fis, bigSkip, remaining);
  65             int expected = (remaining >= Integer.MAX_VALUE)
  66                            ? Integer.MAX_VALUE
  67                            : (remaining > 0 ? (int) remaining : 0);
  68             if (fis.available() != expected) {
  69                 throw new RuntimeException("available() returns "
  70                         + fis.available() + " but expected " + expected);
  71             }
  72         } finally {
  73             file.delete();
  74         }
  75 
  76         System.out.println("Test succeeded.");
  77         System.out.flush();
  78     }
  79 
  80     // Skip toSkip number of bytes and expect that the available() method
  81     // returns avail number of bytes.
  82     private static long skipBytes(InputStream is, long toSkip, long avail)
  83             throws IOException {
  84         long skip = is.skip(toSkip);
  85         if (skip != toSkip) {
  86             throw new RuntimeException("skip() returns " + skip
  87                                        + " but expected " + toSkip);
  88         }
  89         long remaining = avail - skip;
  90         int expected = (remaining >= Integer.MAX_VALUE)
  91                        ? Integer.MAX_VALUE
  92                        : (remaining > 0 ? (int) remaining : 0);
  93 
  94         System.out.println("Skipped " + skip + " bytes, available() returns "
  95                            + expected + ", remaining " + remaining);
  96         if (is.available() != expected) {
  97             throw new RuntimeException("available() returns "
  98                     + is.available() + " but expected " + expected);
  99         }
 100         return skip;
 101     }
 102 
 103     private static void createLargeFile(long filesize,
 104                                         File file) throws Exception {
 105         // Recreate a large file as a sparse file if possible
 106         Files.delete(file.toPath());
 107 
 108         try (FileChannel fc =
 109              FileChannel.open(file.toPath(),
 110                               CREATE_NEW, WRITE, SPARSE)) {
 111             ByteBuffer bb = ByteBuffer.allocate(1).put((byte)1);
 112             bb.rewind();
 113             int rc = fc.write(bb, filesize - 1);
 114 
 115             if (rc != 1) {
 116                 throw new RuntimeException("Failed to write 1 byte"
 117                                            + " to the large file");
 118             }
 119         }
 120         return;
 121     }
 122 }