1 /* 2 * Copyright (c) 2010, 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 27 * @summary Test if available returns correct value when reading 28 * a large file. 29 */ 30 31 import java.io.*; 32 import java.nio.ByteBuffer; 33 import java.nio.channels.*; 34 import static java.nio.file.StandardOpenOption.*; 35 36 public class LargeFileAvailable { 37 private static final long FILESIZE = 7405576182L; 38 public static void main(String args[]) throws Exception { 39 File file = createLargeFile(FILESIZE); 40 try (FileInputStream fis = new FileInputStream(file)) { 41 if (file.length() != FILESIZE) { 42 throw new RuntimeException("unexpected file size = " + file.length()); 43 } 44 45 long bigSkip = 3110608882L; 46 long remaining = FILESIZE; 47 remaining -= skipBytes(fis, bigSkip, remaining); 48 remaining -= skipBytes(fis, 10L, remaining); 49 remaining -= skipBytes(fis, bigSkip, remaining); 50 if (fis.available() != (int) remaining) { 51 throw new RuntimeException("available() returns " + 52 fis.available() + 53 " but expected " + remaining); 54 } 55 } finally { 56 file.delete(); 57 } 58 } 59 60 // Skip toSkip number of bytes and expect that the available() method 61 // returns avail number of bytes. 62 private static long skipBytes(InputStream is, long toSkip, long avail) 63 throws IOException { 64 long skip = is.skip(toSkip); 65 if (skip != toSkip) { 66 throw new RuntimeException("skip() returns " + skip + 67 " but expected " + toSkip); 68 } 69 long remaining = avail - skip; 70 int expected = remaining >= Integer.MAX_VALUE 71 ? Integer.MAX_VALUE 72 : (int) remaining; 73 74 System.out.println("Skipped " + skip + " bytes " + 75 " available() returns " + expected + 76 " remaining=" + remaining); 77 if (is.available() != expected) { 78 throw new RuntimeException("available() returns " + 79 is.available() + " but expected " + expected); 80 } 81 return skip; 82 } 83 84 private static File createLargeFile(long filesize) throws Exception { 85 // Create a large file as a sparse file if possible 86 File largefile = File.createTempFile("largefile", null); 87 // re-create as a sparse file 88 largefile.toPath().delete(); 89 try (FileChannel fc = 90 FileChannel.open(largefile.toPath(), 91 CREATE_NEW, WRITE, SPARSE)) { 92 ByteBuffer bb = ByteBuffer.allocate(1).put((byte)1); 93 bb.rewind(); 94 int rc = fc.write(bb, filesize-1); 95 if (rc != 1) { 96 throw new RuntimeException("Failed to write 1 byte to the large file"); 97 } 98 } 99 return largefile; 100 } 101 }