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