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