Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/File/SetLastModified.java
41149 views
1
/*
2
* Copyright (c) 1998, 2019, 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
/* @test
25
@bug 4091757 6652379 8177809
26
@requires os.maxMemory >= 16G
27
@summary Basic test for setLastModified method
28
*/
29
30
import java.io.*;
31
import java.nio.ByteBuffer;
32
import java.nio.channels.FileChannel;
33
34
35
public class SetLastModified {
36
37
private static void ck(File f, long nt, long rt) throws Exception {
38
if (rt == nt) return;
39
if ((rt / 10 == nt / 10)
40
|| (rt / 100 == nt / 100)
41
|| (rt / 1000 == nt / 1000)
42
|| (rt / 10000 == (nt / 10000))) {
43
System.err.println(f + ": Time set to " + nt
44
+ ", rounded down by filesystem to " + rt);
45
return;
46
}
47
if ((rt / 10 == (nt + 5) / 10)
48
|| (rt / 100 == (nt + 50) / 100)
49
|| (rt / 1000 == (nt + 500) / 1000)
50
|| (rt / 10000 == ((nt + 5000) / 10000))) {
51
System.err.println(f + ": Time set to " + nt
52
+ ", rounded up by filesystem to " + rt);
53
return;
54
}
55
throw new Exception(f + ": Time set to " + nt
56
+ ", then read as " + rt);
57
}
58
59
public static void main(String[] args) throws Exception {
60
File d = new File(System.getProperty("test.dir", "."));
61
File d2 = new File(d, "x.SetLastModified.dir");
62
File f = new File(d2, "x.SetLastModified");
63
long ot, t;
64
65
/* New time: One week ago */
66
long nt = System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 7;
67
68
if (f.exists()) f.delete();
69
if (d2.exists()) d2.delete();
70
if (!d2.mkdir()) {
71
throw new Exception("Can't create test directory " + d2);
72
}
73
74
boolean threw = false;
75
try {
76
d2.setLastModified(-nt);
77
} catch (IllegalArgumentException x) {
78
threw = true;
79
}
80
if (!threw)
81
throw new Exception("setLastModified succeeded with a negative time");
82
83
ot = d2.lastModified();
84
if (ot != 0) {
85
if (d2.setLastModified(nt)) {
86
ck(d2, nt, d2.lastModified());
87
d2.setLastModified(ot);
88
} else {
89
System.err.println("Warning: setLastModified on directories "
90
+ "not supported");
91
}
92
}
93
94
if (f.exists()) {
95
if (!f.delete())
96
throw new Exception("Can't delete test file " + f);
97
}
98
if (f.setLastModified(nt))
99
throw new Exception("Succeeded on non-existent file: " + f);
100
101
// set/check last modified on files of size 1, 1GB+1, 2GB+1, ..
102
// On Windows we only test with a tiny file as that platform doesn't
103
// support sparse files by default and so the test takes too long.
104
final long G = 1024L * 1024L * 1024L;
105
final long MAX_POSITION =
106
System.getProperty("os.name").startsWith("Windows") ? 0L : 3L*G;
107
long pos = 0L;
108
while (pos <= MAX_POSITION) {
109
try (FileChannel fc = new FileOutputStream(f).getChannel()) {
110
fc.position(pos).write(ByteBuffer.wrap("x".getBytes()));
111
}
112
ot = f.lastModified();
113
System.out.format("check with file size: %d\n", f.length());
114
if (!f.setLastModified(nt))
115
throw new Exception("setLastModified failed on file: " + f);
116
ck(f, nt, f.lastModified());
117
pos += G;
118
}
119
120
if (!f.delete()) throw new Exception("Can't delete test file " + f);
121
if (!d2.delete()) throw new Exception("Can't delete test directory " + d2);
122
}
123
124
}
125
126