LargeFileAvailable.java revision 3204
3204N/A/*
3204N/A * Copyright (c) 2010, 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
3204N/A * @bug 6402006
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.*;
3204N/Aimport static java.nio.file.StandardOpenOption.*;
3204N/A
3204N/Apublic class LargeFileAvailable {
3204N/A private static final long FILESIZE = 7405576182L;
3204N/A public static void main(String args[]) throws Exception {
3204N/A File file = createLargeFile(FILESIZE);
3204N/A try (FileInputStream fis = new FileInputStream(file)) {
3204N/A if (file.length() != FILESIZE) {
3204N/A throw new RuntimeException("unexpected file size = " + file.length());
3204N/A }
3204N/A
3204N/A long bigSkip = 3110608882L;
3204N/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) {
3204N/A throw new RuntimeException("available() returns " +
3204N/A fis.available() +
3204N/A " 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) {
3204N/A throw new RuntimeException("skip() returns " + skip +
3204N/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
3204N/A System.out.println("Skipped " + skip + " bytes " +
3204N/A " available() returns " + expected +
3204N/A " remaining=" + remaining);
3204N/A if (is.available() != expected) {
3204N/A throw new RuntimeException("available() returns " +
3204N/A is.available() + " but expected " + expected);
3204N/A }
3204N/A return skip;
3204N/A }
3204N/A
3204N/A private static File createLargeFile(long filesize) throws Exception {
3204N/A // Create a large file as a sparse file if possible
3204N/A File largefile = File.createTempFile("largefile", null);
3204N/A // re-create as a sparse file
3204N/A largefile.toPath().delete();
3204N/A try (FileChannel fc =
3204N/A FileChannel.open(largefile.toPath(),
3204N/A CREATE_NEW, WRITE, SPARSE)) {
3204N/A ByteBuffer bb = ByteBuffer.allocate(1).put((byte)1);
3204N/A bb.rewind();
3204N/A int rc = fc.write(bb, filesize-1);
3204N/A if (rc != 1) {
3204N/A throw new RuntimeException("Failed to write 1 byte to the large file");
3204N/A }
3204N/A }
3204N/A return largefile;
3204N/A }
3204N/A}