Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/FileInputStream/LargeFileAvailable.java
38812 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6402006 7030573 801113626* @summary Test if available returns correct value when reading27* a large file.28*/2930import java.io.*;31import java.nio.ByteBuffer;32import java.nio.channels.*;33import java.nio.file.Files;34import static java.nio.file.StandardOpenOption.*;3536public class LargeFileAvailable {37public static void main(String args[]) throws Exception {38// Create a temporary file in the current directory.39// Use it to check if we have 7G available for40// a large sparse file test. As a fallback use whatever41// space is available, so the test can proceed.42File file = File.createTempFile("largefile", null, new File("."));43long spaceavailable = file.getUsableSpace();44long filesize = Math.min(spaceavailable, 7405576182L);45if (spaceavailable == 0L) {46// A full disk is considered fatal.47throw new RuntimeException("No space available for temp file.");48}4950createLargeFile(filesize, file);5152try (FileInputStream fis = new FileInputStream(file)) {53if (file.length() != filesize) {54throw new RuntimeException("unexpected file size = "55+ file.length());56}5758long bigSkip = Math.min(filesize/2, 3110608882L);59long remaining = filesize;60remaining -= skipBytes(fis, bigSkip, remaining);61remaining -= skipBytes(fis, 10L, remaining);62remaining -= skipBytes(fis, bigSkip, remaining);63int expected = (remaining >= Integer.MAX_VALUE)64? Integer.MAX_VALUE65: (remaining > 0 ? (int) remaining : 0);66if (fis.available() != expected) {67throw new RuntimeException("available() returns "68+ fis.available() + " but expected " + expected);69}70} finally {71file.delete();72}73}7475// Skip toSkip number of bytes and expect that the available() method76// returns avail number of bytes.77private static long skipBytes(InputStream is, long toSkip, long avail)78throws IOException {79long skip = is.skip(toSkip);80if (skip != toSkip) {81throw new RuntimeException("skip() returns " + skip82+ " but expected " + toSkip);83}84long remaining = avail - skip;85int expected = (remaining >= Integer.MAX_VALUE)86? Integer.MAX_VALUE87: (remaining > 0 ? (int) remaining : 0);8889System.out.println("Skipped " + skip + " bytes, available() returns "90+ expected + ", remaining " + remaining);91if (is.available() != expected) {92throw new RuntimeException("available() returns "93+ is.available() + " but expected " + expected);94}95return skip;96}9798private static void createLargeFile(long filesize,99File file) throws Exception {100// Recreate a large file as a sparse file if possible101Files.delete(file.toPath());102103try (FileChannel fc =104FileChannel.open(file.toPath(),105CREATE_NEW, WRITE, SPARSE)) {106ByteBuffer bb = ByteBuffer.allocate(1).put((byte)1);107bb.rewind();108int rc = fc.write(bb, filesize - 1);109110if (rc != 1) {111throw new RuntimeException("Failed to write 1 byte"112+ " to the large file");113}114}115return;116}117}118119120