3204N/A/*
3909N/A * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
3204N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3204N/A *
3204N/A * This code is free software; you can redistribute it and/or modify it
3204N/A * under the terms of the GNU General Public License version 2 only, as
3204N/A * published by the Free Software Foundation.
3204N/A *
3204N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3204N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3204N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3204N/A * version 2 for more details (a copy is included in the LICENSE file that
3204N/A * accompanied this code).
3204N/A *
3204N/A * You should have received a copy of the GNU General Public License version
3204N/A * 2 along with this work; if not, write to the Free Software Foundation,
3204N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3204N/A *
3204N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3204N/A * or visit www.oracle.com if you need additional information or have any
3204N/A * questions.
3204N/A */
3204N/A
3204N/A/*
3204N/A * @test
5758N/A * @bug 6402006 7030573
3204N/A * @summary Test if available returns correct value when reading
3204N/A * a large file.
3204N/A */
3204N/A
3204N/Aimport java.io.*;
3204N/Aimport java.nio.ByteBuffer;
3204N/Aimport java.nio.channels.*;
3471N/Aimport java.nio.file.Files;
3204N/Aimport static java.nio.file.StandardOpenOption.*;
3204N/A
3204N/Apublic class LargeFileAvailable {
3204N/A public static void main(String args[]) throws Exception {
5758N/A // Create a temporary file in the current directory.
5758N/A // Use it to check if we have 7G available for
5758N/A // a large sparse file test. As a fallback use whatever
5758N/A // space is available, so the test can proceed.
5758N/A File file = File.createTempFile("largefile", null, new File("."));
5758N/A long spaceavailable = file.getUsableSpace();
5758N/A long filesize = Math.min(spaceavailable, 7405576182L);
5758N/A if (spaceavailable == 0L) {
5758N/A // A full disk is considered fatal.
5758N/A throw new RuntimeException("No space available for temp file.");
5758N/A }
5758N/A
5758N/A createLargeFile(filesize, file);
5758N/A
3204N/A try (FileInputStream fis = new FileInputStream(file)) {
5758N/A if (file.length() != filesize) {
5758N/A throw new RuntimeException("unexpected file size = "
5758N/A + file.length());
3204N/A }
3204N/A
5758N/A long bigSkip = Math.min(filesize/2, 3110608882L);
5758N/A long remaining = filesize;
3204N/A remaining -= skipBytes(fis, bigSkip, remaining);
3204N/A remaining -= skipBytes(fis, 10L, remaining);
3204N/A remaining -= skipBytes(fis, bigSkip, remaining);
3204N/A if (fis.available() != (int) remaining) {
5758N/A throw new RuntimeException("available() returns "
5758N/A + fis.available() + " but expected " + remaining);
3204N/A }
3204N/A } finally {
3204N/A file.delete();
3204N/A }
3204N/A }
3204N/A
3204N/A // Skip toSkip number of bytes and expect that the available() method
3204N/A // returns avail number of bytes.
3204N/A private static long skipBytes(InputStream is, long toSkip, long avail)
3204N/A throws IOException {
3204N/A long skip = is.skip(toSkip);
3204N/A if (skip != toSkip) {
5758N/A throw new RuntimeException("skip() returns " + skip
5758N/A + " but expected " + toSkip);
3204N/A }
3204N/A long remaining = avail - skip;
3204N/A int expected = remaining >= Integer.MAX_VALUE
3204N/A ? Integer.MAX_VALUE
3204N/A : (int) remaining;
3204N/A
5758N/A System.out.println("Skipped " + skip + " bytes "
5758N/A + " available() returns " + expected +
3204N/A " remaining=" + remaining);
3204N/A if (is.available() != expected) {
5758N/A throw new RuntimeException("available() returns "
5758N/A + is.available() + " but expected " + expected);
3204N/A }
3204N/A return skip;
3204N/A }
3204N/A
5758N/A private static void createLargeFile(long filesize,
5758N/A File file) throws Exception {
5758N/A // Recreate a large file as a sparse file if possible
5758N/A Files.delete(file.toPath());
5758N/A
3204N/A try (FileChannel fc =
5758N/A FileChannel.open(file.toPath(),
5758N/A CREATE_NEW, WRITE, SPARSE)) {
3204N/A ByteBuffer bb = ByteBuffer.allocate(1).put((byte)1);
3204N/A bb.rewind();
5758N/A int rc = fc.write(bb, filesize - 1);
5758N/A
3204N/A if (rc != 1) {
5758N/A throw new RuntimeException("Failed to write 1 byte"
5758N/A + " to the large file");
3204N/A }
3204N/A }
5758N/A return;
3204N/A }
3204N/A}