Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/MappedByteBuffer/ForceViews.java
41152 views
1
/*
2
* Copyright (c) 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 4833719
27
* @summary Verify MappedByteBuffer force on compact, duplicate, and slice views
28
* @run testng ForceViews
29
*/
30
import java.io.IOException;
31
import java.nio.ByteBuffer;
32
import java.nio.MappedByteBuffer;
33
import java.nio.ReadOnlyBufferException;
34
import java.nio.channels.FileChannel;
35
import static java.nio.channels.FileChannel.MapMode.*;
36
import java.nio.file.Path;
37
import static java.nio.file.StandardOpenOption.*;
38
import java.util.function.BiFunction;
39
40
import org.testng.Assert;
41
import org.testng.annotations.AfterTest;
42
import org.testng.annotations.BeforeTest;
43
import org.testng.annotations.DataProvider;
44
import org.testng.annotations.Test;
45
46
public class ForceViews {
47
48
static record Segment(int position, int length) {}
49
50
private FileChannel fc;
51
52
@BeforeTest(alwaysRun=true)
53
public void openChannel() throws IOException {
54
Path file = Path.of(System.getProperty("test.src", "."), "junk");
55
fc = FileChannel.open(file, CREATE_NEW, READ, WRITE, DELETE_ON_CLOSE);
56
ByteBuffer buf = ByteBuffer.wrap(new byte[1024]);
57
fc.write(buf);
58
fc.position(0);
59
}
60
61
@AfterTest(alwaysRun=true)
62
public void closeChannel() throws IOException {
63
fc.close();
64
}
65
66
@DataProvider
67
public Object[][] provider() throws IOException {
68
BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> absSlice =
69
(m, s) -> { return m.slice(s.position, s.length); };
70
BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> relSlice =
71
(m, s) -> { m.position(s.position); m.limit(s.position + s.length);
72
return m.slice(); };
73
BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> duplicate=
74
(m, s) -> { return m.duplicate(); };
75
BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> compact =
76
(m, s) -> { return m.compact(); };
77
78
Object[][] result = new Object[][] {
79
{"Absolute slice", fc, 256, 512, 128, 128, 32, 32, absSlice},
80
{"Relative slice", fc, 256, 512, 0, 128, 32, 32, relSlice},
81
{"Duplicate", fc, 256, 512, 0, 256, 32, 32, duplicate},
82
{"Compact", fc, 256, 512, 0, 256, 32, 32, compact}
83
};
84
85
return result;
86
}
87
88
@Test(dataProvider = "provider")
89
public void test(String tst, FileChannel fc, int mapPosition, int mapLength,
90
int sliceIndex, int sliceLength, int regionOffset, int regionLength,
91
BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> f)
92
throws Exception {
93
MappedByteBuffer mbb = fc.map(READ_WRITE, mapPosition, mapLength);
94
mbb = f.apply(mbb, new Segment(sliceIndex, sliceLength));
95
for (int i = regionOffset; i < regionOffset + regionLength; i++) {
96
mbb.put(i, (byte)i);
97
}
98
mbb.force(regionOffset, regionOffset + regionLength);
99
100
int fcPos = mapPosition + sliceIndex + regionOffset;
101
int mbbPos = regionOffset;
102
int length = regionLength;
103
104
ByteBuffer buf = ByteBuffer.allocate(length);
105
fc.position(fcPos);
106
fc.read(buf);
107
for (int i = 0; i < length; i++) {
108
int fcVal = buf.get(i);
109
int mbbVal = mbb.get(mbbPos + i);
110
int val = regionOffset + i;
111
Assert.assertTrue(fcVal == val && mbbVal == val,
112
String.format("%s: i %d, fcVal %d, mbbVal %d, val %d",
113
tst, i, fcVal, mbbVal, val));
114
}
115
}
116
}
117
118