Path: blob/master/test/jdk/java/io/FileInputStream/LargeFileAvailable.java
41149 views
/*1* Copyright (c) 2010, 2017, 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* @run main/timeout=300 LargeFileAvailable29*/3031import java.io.*;32import java.nio.ByteBuffer;33import java.nio.channels.*;34import java.nio.file.Files;35import static java.nio.file.StandardOpenOption.*;36import java.util.concurrent.TimeUnit;3738public class LargeFileAvailable {39public 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 for42// a large sparse file test. As a fallback use whatever43// space is available, so the test can proceed.44File file = File.createTempFile("largefile", null, new File("."));45long spaceavailable = file.getUsableSpace();46long filesize = Math.min(spaceavailable, 7405576182L);47if (spaceavailable == 0L) {48// A full disk is considered fatal.49throw new RuntimeException("No space available for temp file.");50}5152createLargeFile(filesize, file);5354try (FileInputStream fis = new FileInputStream(file)) {55if (file.length() != filesize) {56throw new RuntimeException("unexpected file size = "57+ file.length());58}5960long bigSkip = Math.min(filesize/2, 3110608882L);61long remaining = filesize;62remaining -= skipBytes(fis, bigSkip, remaining);63remaining -= skipBytes(fis, 10L, remaining);64remaining -= skipBytes(fis, bigSkip, remaining);65int expected = (remaining >= Integer.MAX_VALUE)66? Integer.MAX_VALUE67: (remaining > 0 ? (int) remaining : 0);68if (fis.available() != expected) {69throw new RuntimeException("available() returns "70+ fis.available() + " but expected " + expected);71}72} finally {73file.delete();74}7576System.out.println("Test succeeded.");77System.out.flush();78}7980// Skip toSkip number of bytes and expect that the available() method81// returns avail number of bytes.82private static long skipBytes(InputStream is, long toSkip, long avail)83throws IOException {84long skip = is.skip(toSkip);85if (skip != toSkip) {86throw new RuntimeException("skip() returns " + skip87+ " but expected " + toSkip);88}89long remaining = avail - skip;90int expected = (remaining >= Integer.MAX_VALUE)91? Integer.MAX_VALUE92: (remaining > 0 ? (int) remaining : 0);9394System.out.println("Skipped " + skip + " bytes, available() returns "95+ expected + ", remaining " + remaining);96if (is.available() != expected) {97throw new RuntimeException("available() returns "98+ is.available() + " but expected " + expected);99}100return skip;101}102103private static void createLargeFile(long filesize,104File file) throws Exception {105// Recreate a large file as a sparse file if possible106Files.delete(file.toPath());107108try (FileChannel fc =109FileChannel.open(file.toPath(),110CREATE_NEW, WRITE, SPARSE)) {111ByteBuffer bb = ByteBuffer.allocate(1).put((byte)1);112bb.rewind();113System.out.println(" Writing large file...");114long t0 = System.nanoTime();115int rc = fc.write(bb, filesize - 1);116long t1 = System.nanoTime();117System.out.printf(" Wrote large file in %d ns (%d ms) %n",118t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));119120if (rc != 1) {121throw new RuntimeException("Failed to write 1 byte"122+ " to the large file");123}124}125return;126}127}128129130