Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/FileChannel/directio/DirectIOTest.java
41161 views
1
/*
2
* Copyright (c) 2017, 2021, 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 8164900
27
* @summary Test for ExtendedOpenOption.DIRECT flag
28
* @requires (os.family == "linux" | os.family == "aix")
29
* @library /test/lib
30
* @build jdk.test.lib.Platform
31
* @run main/native DirectIOTest
32
*/
33
34
import java.io.*;
35
import java.nio.ByteBuffer;
36
import java.nio.CharBuffer;
37
import java.nio.channels.*;
38
import java.nio.channels.FileChannel;
39
import java.nio.file.Paths;
40
import java.nio.file.Path;
41
import java.nio.file.Files;
42
import jdk.test.lib.Platform;
43
import java.nio.file.FileStore;
44
import java.nio.file.StandardOpenOption;
45
import com.sun.nio.file.ExtendedOpenOption;
46
47
public class DirectIOTest {
48
49
private static final int BASE_SIZE = 4096;
50
51
private static int testWrite(Path p, long blockSize) throws Exception {
52
try (FileChannel fc = FileChannel.open(p, StandardOpenOption.WRITE,
53
ExtendedOpenOption.DIRECT)) {
54
int bs = (int)blockSize;
55
int size = Math.max(BASE_SIZE, bs);
56
int alignment = bs;
57
ByteBuffer src = ByteBuffer.allocateDirect(size + alignment - 1)
58
.alignedSlice(alignment);
59
assert src.capacity() != 0;
60
for (int j = 0; j < size; j++) {
61
src.put((byte)0);
62
}
63
src.flip();
64
fc.write(src);
65
return size;
66
}
67
}
68
69
private static int testRead(Path p, long blockSize) throws Exception {
70
try (FileChannel fc = FileChannel.open(p, ExtendedOpenOption.DIRECT)) {
71
int bs = (int)blockSize;
72
int size = Math.max(BASE_SIZE, bs);
73
int alignment = bs;
74
ByteBuffer dest = ByteBuffer.allocateDirect(size + alignment - 1)
75
.alignedSlice(alignment);
76
assert dest.capacity() != 0;
77
fc.read(dest);
78
return size;
79
}
80
}
81
82
public static Path createTempFile() throws IOException {
83
return Files.createTempFile(
84
Paths.get(System.getProperty("test.dir", ".")), "test", null);
85
}
86
87
private static boolean isFileInCache(int size, Path p) {
88
String path = p.toString();
89
return isFileInCache0(size, path);
90
}
91
92
private static native boolean isFileInCache0(int size, String path);
93
94
public static void main(String[] args) throws Exception {
95
Path p = createTempFile();
96
long blockSize = Files.getFileStore(p).getBlockSize();
97
98
System.loadLibrary("DirectIO");
99
100
try {
101
int size = testWrite(p, blockSize);
102
if (isFileInCache(size, p)) {
103
throw new RuntimeException("DirectIO is not working properly with "
104
+ "write. File still exists in cache!");
105
}
106
size = testRead(p, blockSize);
107
if (isFileInCache(size, p)) {
108
throw new RuntimeException("DirectIO is not working properly with "
109
+ "read. File still exists in cache!");
110
}
111
} finally {
112
Files.delete(p);
113
}
114
}
115
}
116
117