Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/FileChannel/directio/WriteDirect.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 FileChannel write with DirectIO
28
* @library .. /test/lib
29
* @build DirectIOTest
30
* @run main/othervm WriteDirect
31
*/
32
33
import java.io.*;
34
import java.nio.*;
35
import java.nio.channels.*;
36
import java.nio.file.Files;
37
import java.nio.file.FileStore;
38
import java.nio.file.Path;
39
import java.nio.file.Paths;
40
import java.nio.file.StandardOpenOption;
41
import com.sun.nio.file.ExtendedOpenOption;
42
43
public class WriteDirect {
44
45
private static int charsPerGroup = -1;
46
47
private static int alignment = -1;
48
49
private static boolean initTests() throws Exception {
50
Path p = DirectIOTest.createTempFile();
51
try {
52
FileStore fs = Files.getFileStore(p);
53
alignment = (int)fs.getBlockSize();
54
charsPerGroup = alignment;
55
} finally {
56
Files.delete(p);
57
}
58
return true;
59
}
60
61
public static void main(String[] args) throws Exception {
62
if (initTests()) {
63
testWithNotAlignedBuffer();
64
testWithNotAlignedBufferOffset();
65
testWithArrayOfBuffer();
66
}
67
}
68
69
static void testWithNotAlignedBuffer() throws Exception {
70
Path p = DirectIOTest.createTempFile();
71
try (FileChannel fc = FileChannel.open(p,
72
StandardOpenOption.WRITE, StandardOpenOption.DELETE_ON_CLOSE,
73
ExtendedOpenOption.DIRECT)) {
74
int bufferSize = charsPerGroup - 1;
75
ByteBuffer src = ByteBuffer.allocate(bufferSize);
76
try {
77
fc.write(src);
78
throw new RuntimeException("Expected exception not thrown");
79
} catch (IOException e) {
80
if (!e.getMessage().contains("Number of remaining bytes ("
81
+ bufferSize + ") is not a multiple of the block size ("
82
+ alignment + ")"))
83
throw new Exception("Write failure");
84
}
85
}
86
}
87
88
private static void testWithNotAlignedBufferOffset() throws Exception {
89
int bufferSize = charsPerGroup * 2;
90
int pos = alignment - 1;
91
92
Path p = DirectIOTest.createTempFile();
93
94
try (FileChannel fc = FileChannel.open(p,
95
StandardOpenOption.WRITE, StandardOpenOption.DELETE_ON_CLOSE,
96
ExtendedOpenOption.DIRECT)) {
97
ByteBuffer block = ByteBuffer.allocateDirect(bufferSize);
98
block.position(pos);
99
block.limit(bufferSize - 1);
100
try {
101
fc.write(block);
102
throw new RuntimeException("Expected exception not thrown");
103
} catch (IOException e) {
104
if (!e.getMessage().contains("Current location of the bytebuffer "
105
+ "(" + pos + ") is not a multiple of the block size ("
106
+ alignment + ")"))
107
throw new Exception("Write test failed");
108
}
109
}
110
}
111
112
static void testWithArrayOfBuffer() throws Exception {
113
Path p = DirectIOTest.createTempFile();
114
ByteBuffer[] srcs = new ByteBuffer[4];
115
try (FileChannel fc = FileChannel.open(p,
116
StandardOpenOption.WRITE, ExtendedOpenOption.DIRECT)) {
117
for (int i = 0; i < 4; i++) {
118
srcs[i] = ByteBuffer.allocateDirect(charsPerGroup + alignment - 1)
119
.alignedSlice(alignment);
120
for (int j = 0; j < charsPerGroup; j++) {
121
srcs[i].put((byte)i);
122
}
123
srcs[i].flip();
124
}
125
126
fc.write(srcs, 1, 2);
127
}
128
129
try (FileChannel fc = FileChannel.open(p,
130
StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE)) {
131
ByteBuffer bb = ByteBuffer.allocateDirect(charsPerGroup * 2);
132
fc.read(bb);
133
bb.flip();
134
for (int k = 0; k < charsPerGroup; k++) {
135
if (bb.get() != 1)
136
throw new RuntimeException("Write failure");
137
}
138
for (int m = 0; m < charsPerGroup; m++) {
139
if (bb.get() != 2)
140
throw new RuntimeException("Write failure");
141
}
142
try {
143
bb.get();
144
throw new RuntimeException("Write failure");
145
} catch (BufferUnderflowException bufe) {
146
// correct result
147
}
148
}
149
}
150
}
151
152