Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/FileInputStream/LargeFileAvailable.java
38812 views
1
/*
2
* Copyright (c) 2010, 2013, 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 7030573 8011136
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 java.nio.file.Files;
35
import static java.nio.file.StandardOpenOption.*;
36
37
public class LargeFileAvailable {
38
public static void main(String args[]) throws Exception {
39
// Create a temporary file in the current directory.
40
// Use it to check if we have 7G available for
41
// a large sparse file test. As a fallback use whatever
42
// space is available, so the test can proceed.
43
File file = File.createTempFile("largefile", null, new File("."));
44
long spaceavailable = file.getUsableSpace();
45
long filesize = Math.min(spaceavailable, 7405576182L);
46
if (spaceavailable == 0L) {
47
// A full disk is considered fatal.
48
throw new RuntimeException("No space available for temp file.");
49
}
50
51
createLargeFile(filesize, file);
52
53
try (FileInputStream fis = new FileInputStream(file)) {
54
if (file.length() != filesize) {
55
throw new RuntimeException("unexpected file size = "
56
+ file.length());
57
}
58
59
long bigSkip = Math.min(filesize/2, 3110608882L);
60
long remaining = filesize;
61
remaining -= skipBytes(fis, bigSkip, remaining);
62
remaining -= skipBytes(fis, 10L, remaining);
63
remaining -= skipBytes(fis, bigSkip, remaining);
64
int expected = (remaining >= Integer.MAX_VALUE)
65
? Integer.MAX_VALUE
66
: (remaining > 0 ? (int) remaining : 0);
67
if (fis.available() != expected) {
68
throw new RuntimeException("available() returns "
69
+ fis.available() + " but expected " + expected);
70
}
71
} finally {
72
file.delete();
73
}
74
}
75
76
// Skip toSkip number of bytes and expect that the available() method
77
// returns avail number of bytes.
78
private static long skipBytes(InputStream is, long toSkip, long avail)
79
throws IOException {
80
long skip = is.skip(toSkip);
81
if (skip != toSkip) {
82
throw new RuntimeException("skip() returns " + skip
83
+ " but expected " + toSkip);
84
}
85
long remaining = avail - skip;
86
int expected = (remaining >= Integer.MAX_VALUE)
87
? Integer.MAX_VALUE
88
: (remaining > 0 ? (int) remaining : 0);
89
90
System.out.println("Skipped " + skip + " bytes, available() returns "
91
+ expected + ", remaining " + remaining);
92
if (is.available() != expected) {
93
throw new RuntimeException("available() returns "
94
+ is.available() + " but expected " + expected);
95
}
96
return skip;
97
}
98
99
private static void createLargeFile(long filesize,
100
File file) throws Exception {
101
// Recreate a large file as a sparse file if possible
102
Files.delete(file.toPath());
103
104
try (FileChannel fc =
105
FileChannel.open(file.toPath(),
106
CREATE_NEW, WRITE, SPARSE)) {
107
ByteBuffer bb = ByteBuffer.allocate(1).put((byte)1);
108
bb.rewind();
109
int rc = fc.write(bb, filesize - 1);
110
111
if (rc != 1) {
112
throw new RuntimeException("Failed to write 1 byte"
113
+ " to the large file");
114
}
115
}
116
return;
117
}
118
}
119
120