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